65th: Android Multimedia (media) (iii)

Source: Internet
Author: User

Knowledge use only value, intelligent sharing party display strength!


The content of this lecture:The use of Mediarecoder class recording


First, recording audio using Mediarecorder implementation steps:

1. Create a Mediarecorder object

2. set the source of the recording through the Setaudiosource () method of the Mediarecoder object

3. set the output format via the Mediarecoder object's Setoutputformat ()

4, through the Mediarecoder object's Setaudioencoder (), setaudioencodeingbitrate (int bitrate), setaudioaudiosamling (int Samplingrate) Set the encoding format, encoding bitrate, and sample rate of the recorded sound

5. set the location where recorded audio files are saved through the Setoutputfile (String Path) method of the Mediarecorder object

6, Meidarecorder object Prepare () is expected to prepare for recording

7, call Start () of the Mediarecorder object to begin recording a sound


Let's take a look at an example, and the code is written in comments.

Here is the Mainactivity.java main interface file:

public class Mainactivity extends Activity implements Onclicklistener {Private button start;private button stop;private Me Diarecorder mediarecorder; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate ( Savedinstancestate); Setcontentview (r.layout.activity_main); start = (Button) Findviewbyid (r.id.start); stop = (button ) Findviewbyid (r.id.stop); Start.setonclicklistener (this); Stop.setonclicklistener (this);} @Overridepublic void OnClick (View v) {switch (V.getid ()) {Case r.id.start://first detects if it contains SDCARDIF (! Environment.getexternalstoragestate (). Equals (environment.media_mounted)) {Toast.maketext (Mainactivity.this, " SD card does not exist, please insert SD card ", Toast.length_long). Show (); return;} The file path of the audio output is created by the files = new file ("/sdcard/" + "Dan" + New DateFormat (). Format ("Yyyymmdd_hhmmss", Calendar.getinstance ( Locale.china)) + ". Amr"); Toast.maketext (Getapplicationcontext (), "recording, recording files in" + File.getabsolutepath (), Toast.length_long). Show ();// Create a Recording object Mediarecorder = new Mediarecorder ();//recording from microphone source Mediarecorder.setAudiosource (MediaRecorder.AudioSource.DEFAULT);//Set the recorded sound output format Mediarecorder.setoutputformat ( MediaRecorder.OutputFormat.DEFAULT);//Set the encoding format of the sound Mediarecorder.setaudioencoder ( MediaRecorder.AudioEncoder.DEFAULT)///Set the file path of the recording output (save) Mediarecorder.setoutputfile (File.getabsolutepath ()); try { Create file File.createnewfile ();//Prepare to record Mediarecorder.prepare ();} catch (Exception e) {///Todo:handle exception}//start recording Mediarecorder.start (); Start.settext ("Recording ..."); break;case R.id.stop:if (Mediarecorder! = null) {mediarecorder.stop ();//Release Resource mediarecorder.release (); mediarecorder = null; Start.settext ("recording"); Toast.maketext (Getapplicationcontext (), "recording Complete", Toast.length_long). Show (); Break;}} @Overrideprotected void OnDestroy () {///More Perfect can write if (mediarecorder! = null) {mediarecorder.stop ();// Release resources mediarecorder.release (); mediarecorder = Null;start.settext ("recording"); Toast.maketext (Getapplicationcontext (), "recording Complete", Toast.length_long). Show (); Super.ondestroy ();}}


Since recording and writing memory cards require a permission statement, register at Androidmanifest.xml:

    <uses-permission android:name= "Android.permission.RECORD_AUDIO"/>    <uses-permission android:name= " Android.permission.WRITE_EXTERNAL_STORAGE "/>

here is the result of the operation:



Recording files at the root of the memory card several AMR files starting with Dan




This is where we go, take your time and enjoy it

65th: Android Multimedia (media) (iii)

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.