When using an Android phone, sometimes we use the recording function, this article simply describes how to use the Mediarecorder phone to bring a microphone to record.
First of all, since it is a recording, we need to record and write external memory permissions:
<uses-permission android:name= "Android.permission.WRITE_EXTERNAL_STORAGE"/> <uses-permission Android:name= "Android.permission.RECORD_AUDIO"/>
We then create a recording method Startrecord (), which calls this method when we click the Record button to make a recording. The recording process is:
(1) Determine where the recording files are stored
(2) Instantiate a Mediarecorder object and set its parameters
(3) Call Mediarecorder.prepare () to prepare the recording
(4) Call Mediarecorder.start () to start recording
public void Startrecord () {if (Mr = = NULL) {File FilePath = new File (Environment.getexternalstoragedi Rectory (), "Myrecord"); File FileName = new file (FilePath, System.currenttimemillis () + ". Amr"); try {if (!filepath.exists ()) {filepath.mkdirs (); } if (!filename.exists ()) {filename.createnewfile (); }} catch (IOException e) {e.printstacktrace (); } Mr = new Mediarecorder (); Mr.setaudiosource (MediaRecorder.AudioSource.MIC); Set the input source Mr.setoutputformat (MediaRecorder.OutputFormat.AMR_WB) of the recording; Set output format Mr.setaudioencoder (MediaRecorder.AudioEncoder.AMR_WB); Set the encoding format Mr.setoutputfile (Filename.getabsolutepath ()); Set output file name try{mr.prepare (); Mr.start (); Textview.settext ("File name:" +filename.getabsOlutepath ()); } catch (IOException e) {e.printstacktrace (); } } }
Then we create a method Stoprecord () to stop the recording, call Mediarecorder.stop () to stop the recording, call Mediarecorder.release () to release the recording object. The Mediarecorder pointer is then placed empty so that the next recording can instantiate the new Mediarecorder object.
public void Stoprecord () { if (Mr! = NULL) { mr.stop (); Mr.release (); Mr = NULL; } }
Finally, add a click event to the button in Mainactivity and call the above method to implement the recording. The complete code is as follows:
<?xml version= "1.0" encoding= "Utf-8"? ><manifest xmlns:android= "http://schemas.android.com/apk/res/ Android "package=" Com.example.doodle.myapplication "> <application android:allowbackup=" true "and roid:icon= "@mipmap/ic_launcher" android:label= "@string/app_name" android:supportsrtl= "true" android:t Heme= "@style/apptheme" > <activity android:name= ". Mainactivity "> <intent-filter> <action android:name=" Android.intent.action.MAIN "/&G T <category android:name= "Android.intent.category.LAUNCHER"/> </intent-filter> </activity& Gt </application> <uses-permission android:name= "Android.permission.WRITE_EXTERNAL_STORAGE"/> < Uses-permission android:name= "Android.permission.RECORD_AUDIO"/></manifest>
Import Android.app.activity;import Android.media.mediarecorder;import Android.os.bundle;import Android.os.environment;import Android.view.view;import Android.widget.button;import Android.widget.TextView; Import Java.io.file;import Java.io.ioexception;public class Mainactivity extends Activity {private button button; Private TextView TextView; Private Boolean isstart = false; Private Mediarecorder Mr = NULL; @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); Button = (button) Findviewbyid (R.id.button); TextView = (TextView) Findviewbyid (R.id.textview); Button.setonclicklistener (New View.onclicklistener () {@Override public void OnClick (View v) { if (Isstart) {Startrecord (); Button.settext ("Stop Recording"); Isstart = false; } else { Stoprecord (); Button.settext ("Start recording"); Isstart = true; } } }); public void Startrecord () {if (Mr = = NULL) {File FilePath = new File (environment.getexternalstoraged Irectory (), "Myrecord"); File FileName = new file (FilePath, System.currenttimemillis () + ". Amr"); try {if (!filepath.exists ()) {filepath.mkdirs (); } if (!filename.exists ()) {filename.createnewfile (); }} catch (IOException e) {e.printstacktrace (); } Mr = new Mediarecorder (); Mr.setaudiosource (MediaRecorder.AudioSource.MIC); Set the input source Mr.setoutputformat (MediaRecorder.OutputFormat.AMR_WB) of the recording; Set output format Mr.setaudioencoder (MediaRecorder.AudioEncoder.AMR_WB); Set the encoding format Mr.setoutputfile (Filename.getabsolutepath ()); Set output file name try{mr.prepare (); Mr.start (); Textview.settext ("FileName:" +filename.getabsolutepath ()); } catch (IOException e) {e.printstacktrace (); }}} public void Stoprecord () {if (Mr! = NULL) {mr.stop (); Mr.release (); Mr = NULL; } }}
Notes on Android Development (31) using Mediarecorder recording