Android Mediarecorder Recording Audio

Source: Internet
Author: User
<span id="Label3"></p><p><p><span style="font-size:18px"></span></p></p><p><p><span style="font-size:18px"><strong>To record audio steps using Mediarecorder:</strong></span></p></p><p><p><span style="font-size:18px">Create a Mediarecorder object</span></p></p><p><p><span style="font-size:18px">Call Mediarecorder's Setaudiosource () method to set the sound source. General incoming MediaRecorder.AudioSource.MIC parameter specifies that the sound from the microphone is recorded</span></p></p><p><p><span style="font-size:18px">Call the Setoutputformat () setting of the Mediarecorder object to format the recorded audio file</span></p></p><p><p><span style="font-size:18px">Call the Mediarecorder object's setaudioencoder (), setaudioencodingbitrate (int bitrate), setaudiosamplingrate (int SamplingRate) Sets the encoding format for the recorded Sound. Encoded bit rate,</span></p></p><p><p><span style="font-size:18px">such as the sample Rate. These parameters will be able to control the quality of the recorded sound, the size of the file, generally better quality, the larger the sound file</span></p></p><p><p><span style="font-size:18px">Call Mediarecorder's setoutputfile (String Path) method to set the location where the recorded audio file is saved</span></p></p><p><p><span style="font-size:18px">Call Mediarecorder's Prepare () method to prepare the recording</span></p></p><p><p><span style="font-size:18px">Call the start () method of the Mediarecorder object to start recording</span></p></p><p><p><span style="font-size:18px">After recording, call the Stop () method of the Mediarecorder object to stop Recording. and call the release () method to release the resource</span></p></p><p><p><span style="font-size:18px">For example, the Following:</span></p></p><p><p><span style="font-size:18px"></span></p></p><pre code_snippet_id="324467" snippet_file_name="blog_20140503_1_4649894" name="code" class="java"><pre code_snippet_id="324467" snippet_file_name="blog_20140503_1_4649894" name="code" class="java">Mediarecorder recorder = new Mediarecorder (); Recorder.setaudiosource (MediaRecorder.AudioSource.MIC); Recorder.setoutputformat (MediaRecorder.OutputFormat.THREE_GPP); Recorder.setaudioencoder (MediaRecorder.AudioEncoder.AMR_NB); Recorder.setoutputfile (path_name); Recorder.prepare (); Recorder.start (); Recording is now started ... recorder.stop (); Recorder.reset (); You can reuse the object by going back to Setaudiosource () step recorder.release (); Now the object cannot is reused</pre></pre><p><p></p></p><p><p><span style="font-size:18px"><br>Sample Program:</span></p></p><p><p><span style="font-size:18px">Activity:</span></p></p><p><p><span style="font-size:18px"></span></p></p><pre code_snippet_id="324467" snippet_file_name="blog_20140503_2_9917574" name="code" class="java">Import Java.io.file;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;import Android.widget.imagebutton;import Android.widget.toast;public class Recordsound extends Activityimplements Onclicklistener{//defines the two Buttonimagebutton record on the interface, the Stop;//system's audio file soundfile; Mediarecorder mrecorder; @Overridepublic void onCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate ); Setcontentview (r.layout.main);//get Two buttonrecord = (ImageButton) Findviewbyid (r.id.record) in the program interface; stop = ( ImageButton) Findviewbyid (r.id.stop);//click Event binding listener Record.setonclicklistener (this) for two buttons; Stop.setonclicklistener (this);} @Overridepublic void OnDestroy () {if (soundfile! = null && soundfile.exists ()) {//stop recording Mrecorder.stop ();// Release resource mrecorder.release (); mrecorder = null;} Super.ondestroy ();} @Overridepublic void OnClick (View source) {switch (source.getid ()) {//click record ButtoncaSe r.id.record:if (! Environment.getexternalstoragestate (). equals (android.os.Environment.MEDIA_MOUNTED)) {toast.maketext ( recordsound.this, "SD card does not exist, please insert SD card!"<p><p></p></p><p><p>", toast.length_short). Show (); return;} Try{//create an audio file to save the recording soundfile = new file (environment.getexternalstoragedirectory (). getcanonicalfile () + "/sound.amr"); Mrecorder = new Mediarecorder ();//set sound source for recording Mrecorder.setaudiosource (MediaRecorder.AudioSource.MIC);// Sets the output format of the recorded sound (must be set before setting the sound encoding format) Mrecorder.setoutputformat (MediaRecorder.OutputFormat.THREE_GPP);// Set the format of the sound encoding Mrecorder.setaudioencoder (MediaRecorder.AudioEncoder.AMR_NB); mrecorder.setoutputfile ( Soundfile.getabsolutepath ()); mrecorder.prepare ();//start recording Mrecorder.start (); ①}catch (Exception E) {e.printstacktrace ();} Break;//click Stop buttoncase r.id.stop:if (soundfile! = null && soundfile.exists ()) {//stop recording Mrecorder.stop (); ②//release Resources Mrecorder.release (); ③mrecorder = null;} break;}}}</p></p></pre><br>Manifest file:<p><p></p></p><p><p><span style="font-size:18px"></span></p></p><pre code_snippet_id="324467" snippet_file_name="blog_20140503_3_4280184" name="code" class="html"><pre code_snippet_id="324467" snippet_file_name="blog_20140503_3_4280184" name="code" class="html"><!--grant the program the right to record sound--><uses-permission android:name= "android.permission.RECORD_AUDIO"/><!-- Grant the program permission to write data to external memory--><uses-permission android:name= "android.permission.WRITE_EXTERNAL_STORAGE"/></pre></pre><br><br><p><p></p></p> <p><p>Android Mediarecorder Recording Audio</p></p></span>

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.