Android Mediarecorder Recording Audio

Source: Internet
Author: User

To record audio steps using Mediarecorder:

Create a Mediarecorder object

Call Mediarecorder's Setaudiosource () method to set the sound source, the general incoming MediaRecorder.AudioSource.MIC parameter specifies that the sound is recorded from the microphone

Call the Setoutputformat () setting of the Mediarecorder object to format the recorded audio file

Call the Mediarecorder object's Setaudioencoder (), setaudioencodingbitrate (int bitrate), setaudiosamplingrate (int samplingRate) Set the encoded format of the recorded sound, encode the bit rate,

Sample rate, these parameters will be able to control the quality of the recorded sound, the size of the file, generally better quality, the larger the sound file

Call Mediarecorder's Setoutputfile (String path) method to set the location where the recorded audio file is saved

Call Mediarecorder's Prepare () method to prepare the recording

Call the Start () method of the Mediarecorder object to start recording

When recording is complete, call the Stop () method of the Mediarecorder object to stop recording and call the release () method to release the resource

As follows:

Mediarecorder Recorder = new Mediarecorder (); Recorder.setaudiosource (MediaRecorder.AudioSource.MIC); Recorder.setoutputformat (MediaRecorder.OutputFormat.THREE_GPP); Recorder.setaudioencoder (MediaRecorder.AudioEncoder.AMR_NB); Recorder.setoutputfile (path_name); Recorder.prepare (); Recorder.start ();   Recording is now started ... recorder.stop (); Recorder.reset ();   You can reuse the object by going back to Setaudiosource () step recorder.release (); Now the object cannot is reused


Example program:

Activity:

Import Java.io.file;import android.app.activity;import Android.media.mediarecorder;import Android.os.Bundle;import Android.os.environment;import Android.view.view;import Android.view.view.onclicklistener;import Android.widget.imagebutton;import Android.widget.toast;public class Recordsound extends Activityimplements onclicklistener{//defines two buttons on the interface ImageButton record, stop;//the system's audio file soundfile; Mediarecorder mrecorder; @Overridepublic void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate ); Setcontentview (r.layout.main);//Get two buttons in the program interface record = (ImageButton) Findviewbyid (r.id.record); stop = (ImageButton) Findviewbyid (r.id.stop);//Click event Binding Listener Record.setonclicklistener (this) for two buttons, Stop.setonclicklistener (this);} @Overridepublic void OnDestroy () {if (soundfile! = null && soundfile.exists ()) {//Stop recording mrecorder.stop ();// Release resource Mrecorder.release (); mrecorder = null;} Super.ondestroy ();} @Overridepublic void OnClick (View source) {switch (Source.getid ()) {//Click the Record button case R.id.record:iF (! Environment.getexternalstoragestate (). Equals (Android.os.Environment.MEDIA_MOUNTED)) {Toast.maketext ( Recordsound.this, "SD card does not exist, please insert SD card!" ", Toast.length_short). Show (); return;} try{//Create an audio file to save the recording soundfile = new file (Environment.getexternalstoragedirectory (). Getcanonicalfile () + "/sound.amr"); Mrecorder = new Mediarecorder ();//set sound source for recording mrecorder.setaudiosource (MediaRecorder.AudioSource.MIC);// Sets the output format of the recorded sound (must be set before setting the sound encoding format) Mrecorder.setoutputformat (MediaRecorder.OutputFormat.THREE_GPP);// Set the format of the sound encoding Mrecorder.setaudioencoder (MediaRecorder.AudioEncoder.AMR_NB); Mrecorder.setoutputfile (  Soundfile.getabsolutepath ()); Mrecorder.prepare ();//Start recording mrecorder.start (); ①}catch (Exception e) {e.printstacktrace ();}  break;//Click the Stop button case r.id.stop:if (soundfile! = null && soundfile.exists ()) {//Stop recording mrecorder.stop ();  ②//Release Resources mrecorder.release (); ③mrecorder = null;} Break;}}}

Manifest file:

<!--grant the program the right to record sound--><uses-permission android:name= "Android.permission.RECORD_AUDIO"/><!-- Grant the program permission to write data to external memory--><uses-permission android:name= "Android.permission.WRITE_EXTERNAL_STORAGE"/>


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.