介紹
由于最近項(xiàng)目需要湾趾,需要做語音錄制和播放功能模塊,所以做了一個(gè)輕量級的語音錄制和播放庫陶舞,方便調(diào)用
這是一款參考環(huán)信的語音錄制和播放庫嗽测,輕量級,自帶語音錄制時(shí)動(dòng)畫效果肿孵,根據(jù)聲音大小進(jìn)行動(dòng)畫展示:
具體看圖和gif動(dòng)畫效果:
![voicerecorder.gif](https://github.com/wangshanhai/VoiceRecorder/blob/master/image/voicerecorder2.gif)
voicerecorder.gif
2.jpg
使用步驟:
注意唠粥,目前還在開發(fā)階段,有bug記得提issues哈
注意停做,6.0以上系統(tǒng)需要運(yùn)行時(shí)授權(quán)讀取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中添加如下代碼:
/**
* 設(shè)置文件存放目錄晤愧,存放路徑如:/Android/data/包名/chat/voice/
* 默認(rèn)不設(shè)置,路徑存放為:/Android/data/包名/chat/voice/
*/
PathUtil.getInstance().createDirs("chat", "voice", appContext);
/**
* 自定義命名文件
* 默認(rèn)不設(shè)置是用時(shí)間戳
*/
voiceRecorderView.setCustomNamingFile(true,"語音命名.mp3");
/**
* 自定義語音錄制過程中蛉腌,聲音大小的動(dòng)畫官份,默認(rèn)使用庫文件中的動(dòng)畫只厘,
* 目前默認(rèn)需要設(shè)置15張圖片,以后更新自定義動(dòng)畫幀數(shù)
*/
voiceRecorderView.setDrawableAnimation(Drawable[] animationDrawable)
/**
* 設(shè)置停止播放語音時(shí)舅巷,顯示的靜態(tài)icon
*/
VoicePlayClickListener.setStopPlayIcon(R.drawable.ease_chatto_voice_playing)
/**
* 設(shè)置播放語音的幀動(dòng)畫羔味,
*/
VoicePlayClickListener.setPlayingIconDrawableResoure(R.drawable.voice_to_icon)
-------------------------------------------------------
比如這樣的動(dòng)畫:
<?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 錄音文件的時(shí)長
*/
}
});
}
});
/**
* 播放SD卡本地語音路徑
* imageView顯示動(dòng)畫
* Context,上下文
* path 钠右,語音路徑
*/
new VoicePlayClickListener(imageView, Context).playVoice(path);
/**
* 播放網(wǎng)絡(luò)語音路徑
* imageView顯示動(dòng)畫
* Context赋元,上下文
* path ,語音路徑
*/
new VoicePlayClickListener(imageView, Context).playUrlVoice(path);
3.在布局文件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" />
![recordingView.jpg](https://github.com/wangshanhai/VoiceRecorder/blob/master/image/recordingView.jpg)
recordingView.jpg
最后奉上github地址:https://github.com/wangshanhai/VoiceRecorder