国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android實現錄音方法(仿微信語音、麥克風錄音、發送語音、解決5.0以上BUG)

2019-10-22 18:12:59
字體:
來源:轉載
供稿:網友

先給大家展示下效果圖,如果大家感覺不錯,請參考使用方法,

效果圖如下所示:

android,錄音實現方法,微信語音

使用方法:

錄音工具類:AudioRecoderUtils.java,代碼如下:

public class AudioRecoderUtils { //文件路徑 private String filePath; //文件夾路徑 private String FolderPath; private MediaRecorder mMediaRecorder; private final String TAG = "fan"; public static final int MAX_LENGTH = 1000 * 60 * 10;// 最大錄音時長1000*60*10; private OnAudioStatusUpdateListener audioStatusUpdateListener; /**  * 文件存儲默認sdcard/record  */ public AudioRecoderUtils(){  //默認保存路徑為/sdcard/record/下  this(Environment.getExternalStorageDirectory()+"/record/"); } public AudioRecoderUtils(String filePath) {  File path = new File(filePath);  if(!path.exists())   path.mkdirs();  this.FolderPath = filePath; } private long startTime; private long endTime; /**  * 開始錄音 使用amr格式  *  錄音文件  * @return  */ public void startRecord() {  // 開始錄音  /* ①Initial:實例化MediaRecorder對象 */  if (mMediaRecorder == null)   mMediaRecorder = new MediaRecorder();  try {   /* ②setAudioSource/setVedioSource */   mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);// 設置麥克風   /* ②設置音頻文件的編碼:AAC/AMR_NB/AMR_MB/Default 聲音的(波形)的采樣 */   mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);   /*    * ②設置輸出文件的格式:THREE_GPP/MPEG-4/RAW_AMR/Default THREE_GPP(3gp格式    * ,H263視頻/ARM音頻編碼)、MPEG-4、RAW_AMR(只支持音頻且音頻編碼要求為AMR_NB)    */   mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);   filePath = FolderPath + TimeUtils.getCurrentTime() + ".amr" ;   /* ③準備 */   mMediaRecorder.setOutputFile(filePath);   mMediaRecorder.setMaxDuration(MAX_LENGTH);   mMediaRecorder.prepare();   /* ④開始 */   mMediaRecorder.start();   // AudioRecord audioRecord.   /* 獲取開始時間* */   startTime = System.currentTimeMillis();   updateMicStatus();   Log.e("fan", "startTime" + startTime);  } catch (IllegalStateException e) {   Log.i(TAG, "call startAmr(File mRecAudioFile) failed!" + e.getMessage());  } catch (IOException e) {   Log.i(TAG, "call startAmr(File mRecAudioFile) failed!" + e.getMessage());  } } /**  * 停止錄音  */ public long stopRecord() {  if (mMediaRecorder == null)   return 0L;  endTime = System.currentTimeMillis();  //有一些網友反應在5.0以上在調用stop的時候會報錯,翻閱了一下谷歌文檔發現上面確實寫的有可能會報錯的情況,捕獲異常清理一下就行了,感謝大家反饋!  try {   mMediaRecorder.stop();   mMediaRecorder.reset();   mMediaRecorder.release();   mMediaRecorder = null;   audioStatusUpdateListener.onStop(filePath);   filePath = "";  }catch (RuntimeException e){   mMediaRecorder.reset();   mMediaRecorder.release();   mMediaRecorder = null;   File file = new File(filePath);   if (file.exists())    file.delete();   filePath = "";  }  return endTime - startTime; } /**  * 取消錄音  */ public void cancelRecord(){  try {   mMediaRecorder.stop();   mMediaRecorder.reset();   mMediaRecorder.release();   mMediaRecorder = null;  }catch (RuntimeException e){   mMediaRecorder.reset();   mMediaRecorder.release();   mMediaRecorder = null;  }  File file = new File(filePath);  if (file.exists())   file.delete();  filePath = ""; } private final Handler mHandler = new Handler(); private Runnable mUpdateMicStatusTimer = new Runnable() {  public void run() {   updateMicStatus();  } }; private int BASE = 1; private int SPACE = 100;// 間隔取樣時間 public void setOnAudioStatusUpdateListener(OnAudioStatusUpdateListener audioStatusUpdateListener) {  this.audioStatusUpdateListener = audioStatusUpdateListener; } /**  * 更新麥克狀態  */ private void updateMicStatus() {  if (mMediaRecorder != null) {   double ratio = (double)mMediaRecorder.getMaxAmplitude() / BASE;   double db = 0;// 分貝   if (ratio > 1) {    db = 20 * Math.log10(ratio);    if(null != audioStatusUpdateListener) {     audioStatusUpdateListener.onUpdate(db,System.currentTimeMillis()-startTime);    }   }   mHandler.postDelayed(mUpdateMicStatusTimer, SPACE);  } } public interface OnAudioStatusUpdateListener {  /**   * 錄音中...   * @param db 當前聲音分貝   * @param time 錄音時長   */  public void onUpdate(double db,long time);  /**   * 停止錄音   * @param filePath 保存路徑   */  public void onStop(String filePath); }}

使用很簡單,主要就是開始錄音startRecord()、取消錄音cancelRecord()、結束錄音stopRecord()和錄音監聽setOnAudioStatusUpdateListener(),注意,取消錄音不保存文件,結束錄音會保存文件!

在布局文件中添加一個控件(任意一個都行)

<Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="按住說話" android:textColor="@android:color/white" android:id="@+id/button" android:background="@color/colorPrimary" />

在Activity中使用:    

//當前布局文件的根layout  final RelativeLayout rl = (RelativeLayout) findViewById(R.id.rl);  mButton = (Button) findViewById(R.id.button);  //PopupWindow的布局文件  final View view = View.inflate(this, R.layout.layout_microphone, null);  final PopupWindowFactory mPop = new PopupWindowFactory(this,view);  //PopupWindow布局文件里面的控件  mImageView = (ImageView) view.findViewById(R.id.iv_recording_icon);  mTextView = (TextView) view.findViewById(R.id.tv_recording_time);  mAudioRecoderUtils = new AudioRecoderUtils();  //錄音回調  mAudioRecoderUtils.setOnAudioStatusUpdateListener(new AudioRecoderUtils.OnAudioStatusUpdateListener() {   //錄音中....db為聲音分貝,time為錄音時長   @Override   public void onUpdate(double db, long time) {    //根據分貝值來設置錄音時話筒圖標的上下波動,下面有講解    mImageView.getDrawable().setLevel((int) (3000 + 6000 * db / 100));    mTextView.setText(TimeUtils.long2String(time));   }   //錄音結束,filePath為保存路徑   @Override   public void onStop(String filePath) {    Toast.makeText(MainActivity.this, "錄音保存在:" + filePath, Toast.LENGTH_SHORT).show();    mTextView.setText(TimeUtils.long2String(0));   }  });  //Button的touch監聽  mButton.setOnTouchListener(new View.OnTouchListener() {   @Override   public boolean onTouch(View v, MotionEvent event) {    switch (event.getAction()){     case MotionEvent.ACTION_DOWN:      mPop.showAtLocation(rl,Gravity.CENTER,0,0);      mButton.setText("松開保存");      mAudioRecoderUtils.startRecord();      break;     case MotionEvent.ACTION_UP:      mAudioRecoderUtils.stopRecord();  //結束錄音(保存錄音文件)//      mAudioRecoderUtils.cancelRecord(); //取消錄音(不保存錄音文件)      mPop.dismiss();      mButton.setText("按住說話");      break;    }    return true;   }  });

總結

以上所述是小編給大家介紹的Android實現錄音方法(仿微信語音、麥克風錄音、發送語音、解決5.0以上BUG),希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對VEVB武林網網站的支持!


注:相關教程知識閱讀請移步到Android開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 呼玛县| 桃源县| 碌曲县| 库伦旗| 长春市| 航空| 龙口市| 磴口县| 鹤山市| 佳木斯市| 南充市| 庆元县| 贡觉县| 宜君县| 黄大仙区| 汉沽区| 保德县| 齐齐哈尔市| 罗田县| 定兴县| 哈密市| 岚皋县| 遵义市| 东乌珠穆沁旗| 抚顺市| 唐山市| 镇赉县| 阳江市| 改则县| 顺义区| 永康市| 万年县| 韶关市| 十堰市| 宜昌市| 桦甸市| 谷城县| 资兴市| 基隆市| 曲周县| 阜南县|