Steps to record audio:
1. Create a Recording object
2. Specify the recording device (initialization status)
3. Set the bitrate of recorded audio
4. Set the encoding format for recording audio
5, set the location of recording audio storage
6. Prepare for recording (ready status)
7. Start recording
8. Stop Recording
9. Releasing Resources
Permission to record a sound
<uses-permission android:name= "Android.permission.RECORD_AUDIO"/>
State diagram for recording audio
public class Mediarecordertest extends Activity {private Mediarecorder recorder;private MediaPlayer player;@ overrideprotected void OnCreate (Bundle savedinstancestate) {//TODO auto-generated method Stubsuper.oncreate ( Savedinstancestate); Setcontentview (R.layout.recorder);//1, creating the Recording object recorder = new Mediarecorder ();//2, Specifies the recording device (initialization state) Recorder.setaudiosource (MediaRecorder.AudioSource.MIC),//3, sets the bitrate of the recorded audio Recorder.setoutputformat ( MediaRecorder.OutputFormat.DEFAULT);//4, sets the encoded format of the recorded audio Recorder.setaudioencoder (MediaRecorder.AudioEncoder.DEFAULT );//5, set the location where recording audio is stored (specify the root directory of the phone sdcard, the file name is Music.mp3) Recorder.setoutputfile ( Environment.getexternalstoragedirectory (). GetAbsolutePath () + "Music.mp3"), try {//6, prepare for recording (ready state) Recorder.prepare ();} catch (IllegalStateException e) {//Todo auto-generated catch Blocke.printstacktrace ();} catch (IOException e) {//Todo Au To-generated catch Blocke.printstacktrace ();} To play the audio file just now, create the player = new MediaPlayer (), try {//Set the audio file to play Player.setdatasource (environment.getexternalstOragedirectory (). GetAbsolutePath () + "/music.mp3");//Prepare to play audio (ready State) Player.prepare ();} catch (IllegalArgumentException e) {//TODO auto-generated catch Blocke.printstacktrace ();} catch ( IllegalStateException e) {//Todo auto-generated catch Blocke.printstacktrace ();} catch (IOException e) {//Todo Auto-gene Rated catch Blocke.printstacktrace ();}} public void Start (view view) {Recorder.start ();//7, start recording toast.maketext (this, "recording", 0). Show (); public void Stop (view view) {recorder.stop ();//8, stop recording toast.maketext (this, "End recording, record file to save to SDcard. ", 0). Show (); Recorder.release ();//9, release resources}public void Play (view view) {Player.start ();//start playing the audio you just recorded}}
XML file:
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android " android:layout_width=" match_parent " android:layout_height=" match_parent " android:o rientation= "vertical" > <button android:id= "@+id/button1" android:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:text= "recording" android:onclick= "Start"/> < Button android:id= "@+id/button2" android:layout_width= "wrap_content" android:layout_height= " Wrap_content " android:text=" stops " android:onclick=" Stop "/> <button android:id= " @+id/ Button3 " android:layout_width=" wrap_content " android:layout_height=" wrap_content " android:text= "Play" android:onclick= "Play"/></linearlayout>
Audio recording of Android multimedia