Not much to say, directly on the code, with a specific gaze
Myaudiorecord.java
Import Java.io.file;import java.io.ioexception;import android.app.activity;import android.content.ContentValues; Import Android.content.intent;import Android.media.mediaplayer;import Android.media.mediarecorder;import Android.net.uri;import Android.os.bundle;import Android.os.environment;import Android.provider.MediaStore;import Android.view.view;import Android.widget.button;import android.widget.textview;/** * This is the use of the Mediarecorder class to implement their own audio recording program * * In order to be able to record audio we need record_audio permission * To be able to write sdcard, we need to write_external_storage permissions * @author Administrator * */public class Myaudiorecord extends Activity {private TextView stateview;private Button btnstart,btnstop , btnplay,btnfinish;private mediarecorder recorder;private MediaPlayer player;private File audiofile;private Uri fileuri;public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.my_audio_record); Stateview = (TextView) This.findviewbyid (r.id.view_state); Stateview.settext ("Ready to start"); btnstart = (Button) tHis.findviewbyid (r.id.btn_start); btnstop = (Button) This.findviewbyid (r.id.btn_stop); btnplay = (Button) This.findviewbyid (r.id.btn_play); btnfinish = (Button) This.findviewbyid (r.id.btn_finish); Btnstop.setenabled (False ); btnplay.setenabled (false);} public void OnClick (View v) {int id = v.getid (); switch (ID) {case r.id.btn_start://start recording//We need to instantiate a Mediarecorder object, Then make the corresponding settings recorder = new Mediarecorder (),//Specify Audiosource as MIC (microphone audio source), which is the longest used Recorder.setaudiosource ( MediaRecorder.AudioSource.MIC);//setaudiosource/setvediosource//Specify OutputFormat, we choose 3GP Format//Other format, MPEG-4: This specifies that the recorded file is in the mpeg-4 format//RAW_AMR: Recording the original file, which only supports audio recording, requires audio encoding to be AMR_NB//THREE_GPP at the same time: The file after recording is a 3gp file, Support audio and video recording Recorder.setoutputformat (MediaRecorder.OutputFormat.THREE_GPP);//Specify audio encoding, now only amr_ NB format Recorder.setaudioencoder (MediaRecorder.AudioEncoder.AMR_NB);//Next we need to specify the storage path of the recorded file Fpath = new files ( Environment.getexternalstoragedirectory (). GetAbsolutePath () + "/data/files/"); Fpath.mkdirs ();//Create directory try {// Create temporary file Audiofile = file.createtempFile ("Recording", ". 3gp", Fpath);} catch (IOException e) {//TODO auto-generated catch Blocke.printstacktrace ();} Recorder.setoutputfile (Audiofile.getabsolutepath ());//The following starts recording the try {Recorder.prepare ();} catch ( IllegalStateException e) {//Todo auto-generated catch Blocke.printstacktrace ();} catch (IOException e) {//Todo Auto-gene Rated catch Blocke.printstacktrace ();} Recorder.start (); Stateview.settext ("recording"); btnstart.setenabled (false); btnplay.setenabled (false); Btnstop.setenabled (true); Break;case r.id.btn_stop:recorder.stop (); Recorder.release ();// We can then store our recording files in Mediastore contentvalues values = new Contentvalues (); Values.put (MediaStore.Audio.Media.TITLE, " This is my first Record-audio "); Values.put (MediaStore.Audio.Media.DATE_ADDED, System.currenttimemillis ()); Values.put (MediaStore.Audio.Media.DATA, Audiofile.getabsolutepath ()); Fileuri = This.getcontentresolver (). Insert ( MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, values);//After recording is complete, we instantiate a MediaPlayer object and prepare to play player = new MediaPlayer () ;p LAYer.setoncompletionlistener (New Mediaplayer.oncompletionlistener () {@Overridepublic void oncompletion (MediaPlayer ARG0) {//Update status Stateview.settext ("Ready to Record"); btnplay.setenabled (true); btnstart.setenabled (true); Btnstop.setenabled ( false);}); /ready to play try {Player.setdatasource (Audiofile.getabsolutepath ());p layer.prepare ();} catch (IllegalArgumentException e) { TODO auto-generated catch Blocke.printstacktrace ();} catch (IllegalStateException e) {//Todo auto-generated catch Blocke.printstacktrace ();} catch (IOException e) {//Todo Au To-generated catch Blocke.printstacktrace ();} Update Status Stateview.settext ("Ready to play"); btnplay.setenabled (true); btnstart.setenabled (true); btnstop.setenabled (false); Break;case r.id.btn_play://Play Recording//note, we at the end of the recording, we have instantiated the MediaPlayer, ready to play the preparation player.start ();//Update Status Stateview.settext ("playing"); btnstart.setenabled (false); btnstop.setenabled (false); btnplay.setenabled (false);//update state break at the end of playback; Case r.id.btn_finish://is finished recording, return the recorded audio uriintent intent = new Intent (); Intent.setdata (Fileuri);Setresult (RESULT_OK, intent); This.finish (); break;}}}
Androidmanifest.xml
<uses-permission android:name= "Android.permission.RECORD_AUDIO"/>
Android Multimedia-mediarecorder recording audio and video