Android仿微信语音录制和播放

来源:互联网 发布:10nba总决赛数据 编辑:程序博客网 时间:2024/06/05 09:05

介绍

这是一款参考环信的语音录制和播放库,轻量级,自带语音录制时动画效果,根据声音大小进行动画展示:

具体看图和gif动画效果:
这里写图片描述

使用步骤:

注意,目前还在开发阶段,有bug记得提issues哈

注意,6.0以上系统需要运行时授权读取sd卡和音频

1.添加Jcenter仓库 Gradle依赖:

compile 'com.ilike:voicerecorder:1.0.0'

或者

Maven

<dependency>  <groupId>com.ilike</groupId>  <artifactId>voicerecorder</artifactId>  <version>1.0.0</version>  <type>pom</type></dependency>

2.在Activity中添加如下代码:

/*** 设置文件存放目录,存放路径如:/Android/data/包名/chat/voice/* 默认不设置,路径存放为:/Android/data/包名/chat/voice/*/PathUtil.getInstance().createDirs("chat", "voice", appContext);
/*** 自定义命名文件* 默认不设置是用时间戳*/voiceRecorderView.setCustomNamingFile(true,"语音命名.mp3");
/*** 自定义语音录制过程中,声音大小的动画,默认使用库文件中的动画,* 目前默认需要设置15张图片,以后更新自定义动画帧数*/voiceRecorderView.setDrawableAnimation(Drawable[] animationDrawable)
/*** 设置停止播放语音时,显示的静态icon*/VoicePlayClickListener.setStopPlayIcon(R.drawable.ease_chatto_voice_playing)
/*** 设置播放语音的帧动画,*/VoicePlayClickListener.setPlayingIconDrawableResoure(R.drawable.voice_to_icon)-------------------------------------------------------比如这样的动画:<?xml version="1.0" encoding="utf-8"?>   <animation-list xmlns:android="http://schemas.android.com/apk/res/android"       android:oneshot="false">       <item           android:drawable="@drawable/ease_chatto_voice_playing_f1"           android:duration="200" />       <item           android:drawable="@drawable/ease_chatto_voice_playing_f2"           android:duration="200" />       <item           android:drawable="@drawable/ease_chatto_voice_playing_f3"           android:duration="200" /></animation-list>-------------------------------------------------------
/*** 录制语音*/tvRecorder.setOnTouchListener(new View.OnTouchListener() {            @Override            public boolean onTouch(View v, MotionEvent event) {                return  voiceRecorderView.onPressToSpeakBtnTouch(v, event, new VoiceRecorderView.EaseVoiceRecorderCallback() {                    @Override                    public void onVoiceRecordComplete(String voiceFilePath, int voiceTimeLength) {                        Log.e("voiceFilePath=", voiceFilePath + "  time = " + voiceTimeLength);                        /**                        *voiceFilePath 为录音文件存放在sd的路径                        * voiceTimeLength 录音文件的时长                        */                    }                });            }        });
/*** 播放SD卡本地语音路径* imageView显示动画* Context,上下文* path ,语音路径*/new VoicePlayClickListener(imageView, Context).playVoice(path);/*** 播放网络语音路径* imageView显示动画* Context,上下文* path ,语音路径*/new VoicePlayClickListener(imageView, Context).playUrlVoice(path);

3.在布局文件xml中添加如下:

“`xml

    <com.ilike.voicerecorder.widget.VoiceRecorderView        android:id="@+id/voice_recorder"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_centerInParent="true"        android:visibility="invisible" />

“`
这里写图片描述

喜欢记得start一下哈

https://github.com/wangshanhai/VoiceRecorder