Android project Development Five-"Star Life Chronicles" 1. Recording audio using Mediarecorder

Source: Internet
Author: User

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


Related Article

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.