Android development: Using MediaRecorder to record sound, androidrecorder
Android provides the MediaRecorder class for recording sound in Android applications. For details about MediaRecorder, refer to the MediaRecorder class in Android development.
To use MediaRecorder to record a sound:
1) create a MediaRecorder object.
2) Call the setAudioSource () method of the MediaRecorder object to setSound SourceIn general, the MediaRecorder. AudioSource. MIC parameter is input to specify the recording sound from the microphone.
3) Call setOutputFormat () of the MediaRecorder object to set the recordedAudio File Format.
4) Call setAudioEncoder (), setAudioEncodingBitRate (intbitRate), and setAudioSamplingRate (int samplingRate) of the MediaRecorder object to set the recorded soundEncoding format,Encoding bit rate,Sampling RateThese parameters can control the recorded sound quality and file size. Generally, the better the sound quality, the larger the sound file.
5) Call the setOutputFile (Stringpath) method of MediaRecorder to set the recordedStorage location of audio files.
6) Call MediaRecorder's prepare () methodPrepare recording.
7) Call the start () method of the MediaRecorder objectStart recording.
8) after the recording is completed, call the stop () method of the MediaRecorder object.Stop recordingAnd call the release () method.Release resources.
Tip: 1. In the above steps, steps 3rd and 4th must not be reversed. Otherwise, the program will throw an lllegalStateException.
2. Set the audio encoding format to match the audio output format. Otherwise, the recorded audio file is not standard. If the encoding format does not match the output format, although the recorded audio file can be played, after merging multiple audio files, only some files in the merged file will be played.
Application Instance
Use MediaRecorder to record sound:
Instance code:
<Span style = "font-size: 18px;"> package com. jph. recordsound; import java. io. file; import org. crazyit. sound. r; 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 Activityimplement S 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 the two buttons in the program interface record = (ImageButton) findViewById (R. id. record); stop = (ImageButton) findViewById (R. id. stop); // bind the listener record to the Click Event of the two buttons. setOnClickListener (this); stop. setOnClic KListener (this) ;}@ Overridepublic void onDestroy () {if (soundFile! = Null & soundFile. exists () {// stop recording mRecorder. stop (); // release the resource mRecorder. release (); mRecorder = null;} super. onDestroy () ;}@ Overridepublic void onClick (View source) {switch (source. getId () {// click the recording button case R. id. record: if (! Environment. getExternalStorageState (). equals (android. OS. Environment. MEDIA_MOUNTED) {Toast. makeText (RecordSound. this, "the SD card does not exist. Please insert the SD card! ", Toast. LENGTH_SHORT ). show (); return;} try {// create the audio File soundFile that saves the recording = new File (Environment. getExternalStorageDirectory (). getCanonicalFile () + "/sound. amr "); mRecorder = new MediaRecorder (); // set the sound source of the recording to mRecorder. setAudioSource (MediaRecorder. audioSource. MIC); // set the output format of the recorded sound (which must be set before the audio encoding format. setOutputFormat (MediaRecorder. outputFormat. AMR_NB); // sets the audio encoding format mRecorder. setAudioEncoder (MediaRecorder. audi OEncoder. AMR_NB); mRecorder. setOutputFile (soundFile. getAbsolutePath (); mRecorder. prepare (); // start recording mRecorder. start (); // ①} catch (Exception e) {e. printStackTrace ();} break; // click the stop button case R. id. stop: if (soundFile! = Null & soundFile. exists () {// stop recording mRecorder. stop (); // ② // release the resource mRecorder. release (); // ③ mRecorder = null;} break ;}}</span>
Program running:
How to Use Android API to disable sound in MediaRecorder
Let's take a look at the setAudioEncoder method in MediaRecorder. This is obviously blocked.
Android MediaRecorder class call problems, video monitoring system development problems, Android system video recording methods
MeidaRecord must first apply for a space, and cannot be recorded if the application fails.