Android Development using Mediarecorder recording sound

Source: Internet
Author: User

In order to record sound in Android apps, Android provides Mediarecorder class, about Mediarecorder, you can refer to theAndroid Development Mediarecorder type of detail.

Use Mediarecorder steps to record a sound:

1) Create the Mediarecorder object.

2) call the Setaudiosource () method of the Mediarecorder object to set the sound source , generally incoming mediarecorder. The audiosource.mic parameter specifies that the sound from the microphone is recorded.

3) Call the Mediarecorder object's Setoutputformat () setting to format the recorded audio file .

4) Call the Mediarecorder object's Setaudioencoder (), Setaudioencodingbitrate (intbitrate), setaudiosamplingrate (int Samplingrate) sets the encoding format , encoding bitrate, sample rate , and so on for the recorded sound, which will control the quality of the recorded sound and the size of the file. Generally speaking, the better the sound quality, the larger the sound file.

5) call Mediarecorder's Setoutputfile (Stringpath) method to set the location where the recorded audio files are saved .

6) call Mediarecorder's Prepare () method to prepare the recording .

7) call the Start () method of the Mediarecorder object to begin recording .

8) Recording is complete, call the Stop () method of the Mediarecorder object to stop recording , and call the release () method to release the resource .

Tips: 1. The steps 3rd and 42 above must not be reversed, or the program will throw a Lllegalstateexception exception.

2. Set the sound encoding format to correspond to the output format of the sound, otherwise the recorded audio file is not standard. If the encoding format and output format do not correspond, the recorded audio file may play, but after merging multiple such audio files, only some of the files in the merged file will appear.

Application Examples

Use Mediarecorder Recording Sound:

Instance code:

<span style= "FONT-SIZE:18PX;" >package com.jph.recordsound;import;import Org.crazyit.sound.r;import;import;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{//define two buttons on the interface ImageButton record, stop;//system audio file Soundfile; Mediarecorder mrecorder; @Overridepublic void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate ); Setcontentview (r.layout.main);//Get two buttons in the program interface record = (ImageButton) Findviewbyid (; stop = (ImageButton) Findviewbyid (;//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 the Record button Case (! Environment.getexternalstoragestate (). Equals (Android.os.Environment.MEDIA_MOUNTED)) {Toast.maketext ( Recordsound.this, "SD card does not exist, please insert SD card!" ", 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.AMR_NB);// 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 the Stop button case (soundfile! = null && soundfile.exists ()) {//Stop recording mrecorder.stop ();  ②//Release Resources mrecorder.release (); ③mrecorder = null;} Break;}}} </span>

Program run:

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: 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.