Mobile phones typically offer microphone hardware, and the Android system can use that hardware to record audio.
To record audio in Android apps, Android provides the Mediarecorder class and the process of recording audio using Mediarecorder is as follows:
1. Create a Mediarecorder object.
2. Call the Setaudiosource () method of the Mediarecorder object to set the sound source, and the general incoming MediaRecorder.AudioSource.MIC parameter specifies the sound to be recorded from the microphone.
3. Call the Mediarecorder object's Setoutputformat () to set the format of the recorded audio file.
4. Call the Mediarecorder object's Setaudioencoder (), setaudioencodingbitrate (int bitrate), 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 (String path) 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
When recording is complete, call the Stop () method of the Mediarecorder object to stop recording and call the release () method to release the resource.
Note: Steps 3, 42 steps must not be reversed, otherwise the program will throw IllegalStateException exception.
Import Java.io.File;
Import java.io.IOException;
Import Android.media.MediaRecorder;
Import Android.os.Bundle;
Import android.os.Environment;
Import android.app.Activity;
Import Android.view.Menu;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.widget.ImageButton;
Import Android.widget.Toast;
public class Recodesound extends Activity implements onclicklistener{
Buttons in the program
ImageButton Recode;
ImageButton stop;
Audio files for the system
File Soundfile;
Mediarecorder Mrecorder;
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_recode_sound);
Get two buttons in the program interface
Recode = (ImageButton) Findviewbyid (R.id.recode);
Stop = (ImageButton) Findviewbyid (r.id.stop);
A Click event binding listener for two buttons
Recode.setonclicklistener (this);
Stop.setonclicklistener (this);
}
@Override
protected void OnDestroy () {
if (soundfile! = null && soundfile.exists ()) {
Stop Recording
Mrecorder.stop ();
Freeing resources
Mrecorder.release ();
Mrecorder = null;
}
Super.ondestroy ();
}
@Override
public void OnClick (View source) {
Switch (Source.getid ()) {
Click the Record button
Case R.id.recode:
if (! Environment.getexternalstoragestate ()
. Equals (Android.os.Environment.MEDIA_MOUNTED)) {
Toast.maketext (Recodesound.this, "SD card does not exist, please insert SD card", ""). Show ();
Return
}
try {
Create an audio file to save the recording
Soundfile = new File (Environment.getexternalstoragedirectory (). Getcanonicalfile () + "/sound.amr");
Mrecorder = new Mediarecorder ();
Set the sound source for recording
Mrecorder.setaudiosource (MediaRecorder.AudioSource.MIC);
Set the output format of the recorded sound (must be set before you set the sound encoding format)
Mrecorder.setoutputformat (MediaRecorder.OutputFormat.THREE_GPP);
Format the sound encoding
Mrecorder.setaudioencoder (MediaRecorder.AudioEncoder.AMR_NB);
Mrecorder.setoutputfile (Soundfile.getabsolutepath ());
Mrecorder.prepare ();
Start recording
Mrecorder.start ();
} catch (IOException e) {
E.printstacktrace ();
}
Break
Click the Stop button
Case R.id.stop:
if (soundfile! = null && soundfile.exists ()) {
Stop Recording
Mrecorder.stop ();
Freeing resources
Mrecorder.release ();
Mrecorder = null;
}
Break
}
}
}
Recording Audio using Mediarecorder