序言
最近開發了一個語音輸入控制元件,UI效果和微信的保持基本一直,除了顏色不一樣,具體的功能如下
- 影片效果和微信一樣,都是通過聲音的分貝大小驅動影片的大小
- 如果聲音過小,會進入監聽模式,(一個小波浪從右到左移動)
- 錄音的氣泡會隨著時間不斷變大
- 最長支持60秒錄音,在最后十秒會震動提示用戶
- 最后十秒會有倒計時,如果超時會自動截取
- 支持轉MP3格式
- 使用簡單,一個回呼回傳語音檔案的地址和語音的時長
效果
這個demo 包含了錄音,播放,權限申請的所有功能,建議自己下載試一試效果,
影片效果

監聽模式(聲音太小的時候自動進入)

其他功能
上滑取消和超時提醒

使用
布局檔案中直接參考
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:id="@+id/recyclerView"
android:layout_height="match_parent"
android:layout_weight="1" />
<RelativeLayout
android:id="@+id/layout_voice"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#f5f5f5">
<com.example.wxsoundrecord.voice.RecordButton
android:id="@+id/btnAudio"
android:paddingStart="140dp"
android:paddingEnd="140dp"
style="?android:attr/borderlessButtonStyle"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_margin="10dp"
android:background="@drawable/dyps_shape_txt_bg"
android:drawableStart="@drawable/dypc_ic_voice_press"
android:gravity="center"
android:text="按住說話"
android:textSize="13dp"
android:visibility="visible" />
</RelativeLayout>
</LinearLayout>
java中 默認轉換MP3 是自動開啟的,
RecordButton recordButton = findViewById(R.id.btnAudio);
recordButton.setUseMP3(true);//使用mp3格式
recordButton.setOnFinishedRecordListener(new RecordButton.OnFinishedRecordListener() {
@Override
public void onFinishedRecord(String audioPath, int time) {
VoiceMsg msg = new VoiceMsg(audioPath, time, System.currentTimeMillis());
voiceMsgList.add(msg);
adapter.notifyDataSetChanged();
recyclerView.scrollToPosition(voiceMsgList.size() - 1);
}
});
感謝
wav 轉 mp3 使用的是下面這個庫
AndroidAudioConverter
需要在Application中初始化
public class MyApp extends Application {
@Override
public void onCreate() {
super.onCreate();
//wav轉mp3工具
AndroidAudioConverter.load(this, new ILoadCallback() {
@Override
public void onSuccess() {
// Great!
}
@Override
public void onFailure(Exception error) {
// FFmpeg is not supported by device
}
});
}
實作
還是挺復雜的,準備單獨弄一篇博客介紹
原始碼
WXSoundRecord
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/282138.html
標籤:其他
下一篇:iOS分配終端界面功能實作:1、拆分SN 2、計算SN個數( 號段用‘-’連接;每批號段請用‘,’|‘隔開或分行)
