Android錄音時擷取分貝值的方法代碼執行個體_Android

來源:互聯網
上載者:User

參考文章Android中即時擷取音量分貝值詳解:http://www.jb51.net/article/64806.htm

public class MediaRecorderDemo {   private final String TAG = "MediaRecord";   private MediaRecorder mMediaRecorder;   public static final int MAX_LENGTH = 1000 * 60 * 10;// 最大錄音時間長度1000*60*10;   private String filePath;    public MediaRecorderDemo(){     this.filePath = "/dev/null";   }    public MediaRecorderDemo(File file) {     this.filePath = file.getAbsolutePath();   }    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);        /* ③準備 */       mMediaRecorder.setOutputFile(filePath);       mMediaRecorder.setMaxDuration(MAX_LENGTH);       mMediaRecorder.prepare();       /* ④開始 */       mMediaRecorder.start();       // AudioRecord audioRecord.       /* 擷取開始時間* */       startTime = System.currentTimeMillis();       updateMicStatus();       Log.i("ACTION_START", "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();     Log.i("ACTION_END", "endTime" + endTime);     mMediaRecorder.stop();     mMediaRecorder.reset();     mMediaRecorder.release();     mMediaRecorder = null;     Log.i("ACTION_LENGTH", "Time" + (endTime - startTime));     return endTime - startTime;   }    private final Handler mHandler = new Handler();   private Runnable mUpdateMicStatusTimer = new Runnable() {     public void run() {       updateMicStatus();     }   };    /**    * 更新話筒狀態    *    */   private int BASE = 1;   private int SPACE = 100;// 間隔取樣時間    private void updateMicStatus() {     if (mMediaRecorder != null) {       double ratio = (double)mMediaRecorder.getMaxAmplitude() /BASE;       double db = 0;// 分貝       if (ratio > 1)         db = 20 * Math.log10(ratio);       Log.d(TAG,"分貝值:"+db);       mHandler.postDelayed(mUpdateMicStatusTimer, SPACE);     }   } }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.