Android devices enable simple demo of recording audio
Reprint please specify;
http://blog.csdn.net/u013670933/article/details/26089487
The code is as follows:
public class Mainactivity extends Activity {file soundfile;//output files Mediarecorder mrecorder; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); Inintview ();//Initialize}//to initialize private void Inintview () {mrecorder = new Mediarecorder ();//Set Audio source Mrecorder.setaudiosource ( MediaRecorder.AudioSource.MIC)///Set the sound output format----Be sure to set Mrecorder.setoutputformat before encoding format ( MediaRecorder.OutputFormat.THREE_GPP);//Set the encoding format of the sound Mrecorder.setaudioencoder (MediaRecorder.AudioEncoder.AMR_NB);} eventofclickpublic void OnClick (view view) {switch (View.getid ()) {case r.id.record:try{//set file to save music soundfile = new File (Environment.getexternalstoragedirectory (). Getcanonicalfile () + "/sound.amr"); Mrecorder.setoutputfile ( Soundfile.getabsolutepath ()); Mrecorder.prepare ();//start Mrecorder.start ();} catch (Exception e) {e.printstacktrace ();} Break;case r.id.stop:if (soundfile!=null && soundfile.exists ()) {//Avoid unused to cause null pointer mrecorder.stop (); Mrecorder.release (); MrecordER = null;} Break;default:break;}} Release resource @overrideprotected void OnDestroy () {if (Soundfile!=null && soundfile.exists ()) {mrecorder.stop (); Mrecorder.release (); mrecorder = null;} Super.ondestroy ();}}
the layout file has only two button, which is skipped here.
Permission settings:
<uses-permission android:name= "Android.permission.RECORD_AUDIO"/><uses-permission android:name= " Android.permission.WRITE_EXTERNAL_STORAGE "/>
Demo
Demo sample code:
http://download.csdn.net/detail/u013670933/7362171