How Android implements the recording (most importantly the trial method for Mediarecorder)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.