Package Cn.eoe.record;import Java.io.file;import Java.io.ioexception;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;public class Mainactivity extends Activity {private Onclicklistener Btnclicklistener = new Onclicklistener () {@Overridepublic void OnClick (View v) {switch (V.getid ()) {case R.id.btnstartrec Ord:startrecord (); break;case R.id.btnstoprecord:stoprecord (); break;}}; private void Startrecord () {if (MP = = null) {file DIR = new File (Environment.getexternalstoragedirectory (), "sounds"); !dir.exists ()) {dir.mkdirs ();} File Soundfile = new file (dir, System.currenttimemillis () + ". Amr"); if (!soundfile.exists ()) {try { Soundfile.createnewfile ();} catch (IOException e) {e.printstacktrace ();}} MP = new Mediarecorder (); Mp.setaudiosource (MediaRecorder.AudioSource.MIC); Mp.setoutputformat ( MediaRecorder.OutputFormat.MPEG_4); Mp.setaudioencoder (Mediarecorder.Audioencoder.default); Mp.setoutputfile (Soundfile.getabsolutepath ()); try {mp.prepare (); Mp.start ();} catch ( IllegalStateException e) {e.printstacktrace ();} catch (IOException e) {e.printstacktrace ();}}} private void Stoprecord () {if (MP! = null) {mp.stop (); Mp.release (); MP = NULL;}} Private Mediarecorder MP = null, @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate ( Savedinstancestate); Setcontentview (R.layout.activity_main); Findviewbyid (R.id.btnstartrecord). Setonclicklistener (Btnclicklistener); Findviewbyid (R.id.btnstoprecord). Setonclicklistener (BtnClickListener);}}
The most important thing is the operation of the Mediarecorder. How to release Ah, how to initialize, and then can play, in fact, very simple.
Key codes are here:
MP = new Mediarecorder (); Mp.setaudiosource (MediaRecorder.AudioSource.MIC); Mp.setoutputformat ( MediaRecorder.OutputFormat.MPEG_4); Mp.setaudioencoder (MediaRecorder.AudioEncoder.DEFAULT); Mp.setoutputfile ( Soundfile.getabsolutepath ());
Stop recording the end of the paragraph, you need to stop directly on it. Release the Mediarecorder () object and release the empty one.
Stop first, then release (), then the value is empty!
The latest speech: Technology this thing, need constant learning, continuous efforts! Do not stop, only science and technology is the first productive force. These 2 years have not continued to excavate technology, resulting in poor technology. Have to work hard!!!
Technology is not how to do, was led to talk a few times, deeply deepened my thinking about myself. I can't stop trying, refueling is the hard truth!!!!
How Android implements the recording (most importantly the trial method for Mediarecorder)