The method code instance that gets DB value when Android recording _android

Source: Internet
Author: User
Tags getmessage set set

Reference article Android Real-time fetch volume decibel value detailed: 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;//maximum recording length 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; /** * Start recording using AMR format * * Audio file * @return/public void Startrecord () {//Start recording/*①ini 
    Tial: Instantiate Mediarecorder Object */if (Mmediarecorder = null) Mmediarecorder = new Mediarecorder (); try {/*②setaudiosource/setvediosource */Mmediarecorder.setaudiosource (MediaRecorder.AudioSource.MIC);//Set Set the microphone/*② the encoding of the audio file: Aac/amr_nb/amr_mb/default (waveform) Sampling/Mmediarecorder.setoutputformat (MEDIARECORDER.OUTP 
            Utformat.default); /* *② format of output file: Three_gpp/mpeg-4/raw_amr/default THREE_GPP (3gp format *, H263 video/arm audio encoding), MPEG-4, RAW_AMR (audio only supported and audio coding required for AMR_NB)/Mmediarecord 
 
      Er.setaudioencoder (MediaRecorder.AudioEncoder.AMR_NB); 
      /*③ Preparation * * * Mmediarecorder.setoutputfile (FilePath); 
      Mmediarecorder.setmaxduration (max_length); 
      Mmediarecorder.prepare (); 
      /*④ Start * * Mmediarecorder.start (); 
      Audiorecord Audiorecord. 
      /* Get start time * */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 ()); 
    }/** * Stop recording */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 (); 
 
  } 
  }; 
  /** * Update Microphone status */private int BASE = 1; private int space = 100;//interval sampling time private void Updatemicstatus () {if (Mmediarecorder!= null) {double R 
      Atio = (double) mmediarecorder.getmaxamplitude ()/base; 
      Double db = 0;//db if (ratio > 1) db = * MATH.LOG10 (ratio); 
      LOG.D (TAG, "decibel value:" +db); 
    Mhandler.postdelayed (Mupdatemicstatustimer, space); } 
  } 
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.