The corresponding permission must be set in androidmanifest: Android: Name = "android. Permission. record_audio"
1. first determine whether an SD card exists and obtain the corresponding path
/* Check whether an SD card exists */
If (environment. getexternalstoragestate (). Equals (Android. OS. environment. media_mounted ))
{
/* Get the path of the SD card */
Mrecaudiopath = environment. getexternalstoragedirectory ();
/* Update all Recording files to the list */
Musiclist ();}
2. Start recording
/* Create a recording file. The first parameter is the prefix of the file name, the second parameter is the suffix, and the third parameter is the SD path */
Mrecaudiofile = file. createtempfile (strtempfile, ". Amr", mrecaudiopath );
/* Instantiate the mediarecorder object */
Mmediarecorder = new mediarecorder ();
/* Set the microphone */
Mmediarecorder. setaudiosource (mediarecorder. audiosource. Mic );
/* Set the output file format */
Mmediarecorder. setoutputformat (mediarecorder. outputformat. Default );
/* Set the encoding of the audio file */
Mmediarecorder. setaudioencoder (mediarecorder. audioencoder. Default );
/* Set the path of the output file */
Mmediarecorder. setoutputfile (mrecaudiofile. getabsolutepath ());
/* Prepare */
Mmediarecorder. Prepare ();
/* Start */
Mmediarecorder. Start ();
3. Disable recording
Mmediarecorder. Stop ();
4. Play the recording file
Intent intent = new intent ();
Intent. addflags (intent. flag_activity_new_task); intent. setaction (Android. content. intent. action_view);/* set the file type */intent. setdataandtype (URI. fromfile (file), "audio"); startactivity (intent );
5. filter file types to implement filenamefilter
Class musicfilter implements filenamefilter {public Boolean accept (File Dir, string name) {return (name. endswith (". Amr "));}
}
Download the code:/files/lee0oo0/examples_07_05.rar