欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android录音时获取分贝值的方法代码实例

程序员文章站 2022-03-08 17:45:15
参考文章android中实时获取音量分贝值详解: public class mediarecorderdemo { private final strin...

参考文章android中实时获取音量分贝值详解:

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); 
    } 
  } 
}