Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android錄音時獲取分貝值的方法代碼實例

Android錄音時獲取分貝值的方法代碼實例

編輯:關於Android編程

參考文章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); 
    } 
  } 
}

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved