First, list a question, and move it here after finishing the sorting! I am studying recently. I am here to help you!
The Android Multimedia Framework supports obtaining and encoding multiple audio formats, so you can easily merge the audio into your application. If the device supports this, you can use MediaRecorder APIs for recording.
This chapter shows you how to write an application to obtain audio from the microphone on the device, and then save and play back the audio.
Note: Android simulators do not have the recording capability, but real devices generally have this function.
Execute Audio Acquisition
Obtaining audio from a device is a little more complicated than playing back audio or video, but it is still relatively simple:
Create a new android. media. MediaRecorder instance.
Use MediaRecorder. setAudioSource () to set the audio source. Generally, use MediaRecorder. AudioSource. MIC.
Use MediaRecorder. setOutputFormat () to set the output file format.
Use MediaRecorder. setOutputFile () to set the name of the output file.
Use MediaRecorder. setAudioEncoder () to set the audio encoding.
Call MediaRecorder. prepare () of the MediaRecorder instance ().
MediaRecorder. start () to get the audio.
Call MediaRecorder. stop (). stop.
After you use the MediaRecorder instance, call MediaRecorder. release () to release the resource immediately.
Example: Recording
Package com. ppmeet; import java. io. IOException; import Android. app. activity; import android. graphics. pixelFormat; import android. media. mediaRecorder; import android. OS. bundle; import android. view. view; import android. view. view. onClickListener; import android. view. window; import android. view. windowManager; import android. widget. button;/*** class name: TestBasicAudio <BR> * class description: Basic Record Audio De Mo <BR> ** @ version 1.00 2011/12/01 * @ author CODYY) peijiangping */public class TestBasicAudio extends Activity {private Button button_start; private Button button_stop; private MediaRecorder recorder; public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); getWindow (). setFormat (PixelFormat. TRANSLUCENT); // enables the requestWindowFeature (Window. FEATURE_NO_TITLE); // remove the interface title getWindow (). SetFlags (WindowManager. layoutParams. FLAG_FULLSCREEN, WindowManager. layoutParams. FLAG_FULLSCREEN); // reset the page size setContentView (R. layout. main); init ();} private void init () {button_start = (Button) this. findViewById (R. id. start); button_stop = (Button) this. findViewById (R. id. stop); button_stop.setOnClickListener (new AudioListerner (); button_start.setOnClickListener (new AudioListerner ();} class AudioListerne R implements OnClickListener {@ Overridepublic void onClick (View v) {if (v = button_start) {initializeAudio ();} if (v = button_stop) {recorder. stop (); // stop burning // recorder. reset (); // restart MediaRecorder. recorder. release (); // resources must be released after burning. // recorder = null;} private void initializeAudio () {recorder = new MediaRecorder (); // new output MediaRecorder object recorder. setAudioSource (MediaRecorder. audioSource. MIC); // set M The audio source of ediaRecorder is the microphone recorder. setOutputFormat (MediaRecorder. outputFormat. RAW_AMR); // set the audio format recorder recorded by MediaRecorder. setAudioEncoder (MediaRecorder. audioEncoder. AMR_NB); // sets MediaRecorder to encode the recorded audio TO amr. android supports amr encoding. Recorder. setOutputFile ("/sdcard/peipei. amr "); // set the recording audio file storage path try {recorder. prepare (); // prepare the recorder for recording. start (); // start recording} catch (IllegalStateException e) {e. printStackTrace ();} catch (IOException e) {e. printStackTrace () ;}}} AndroidMainfest. xml <? Xml version = "1.0" encoding = "UTF-8"?> <Manifest xmlns: android =" http://schemas.android.com/apk/res/android "Package =" com. ppmeet "android: versionCode =" 1 "android: versionName =" 1.0 "> <uses-sdk android: minSdkVersion =" 8 "/> <applicationandroid: icon = "@ drawable/ic_launcher" android: label = "@ string/app_name"> <activityandroid: name = ". testBasicAudio "android: screenOrientation =" landscape "> <intent-filter> <action android: name =" android. intent. action. MAIN "/> <category android: name =" android. intent. category. LAUNCHER "/> </in Tent-filter> </activity> </application> <! -- INTERNET permission --> <uses-permission android: name = "android. permission. INTERNET"/> <! -- Write data permission to SDCard --> <uses-permission android: name = "android. permission. WRITE_EXTERNAL_STORAGE"/> <! -- Recording permission --> <uses-permission android: name = "android. permission. RECORD_AUDIO"/> <! -- Create and delete file permissions in SDCard --> <uses-permission android: name = "android. permission. MOUNT_UNMOUNT_FILESYSTEMS"/> </manifest>
Example: Recording and Playback
The following example shows how to set, start and stop Audio Acquisition, and play back the recorded file.
/** If the output file is written to external storage, * This application requires the write permission for external storage and * The recording permission. these permissions must be * In AndroidManifest. the declaration in the xml file, like this: ** <uses-permission android: name = "android. permission. WRITE_EXTERNAL_STORAGE "/> * <uses-permission android: name =" android. permission. RECORD_AUDIO "/> **/package com. android. audiorecordtest; import android. app. activity; import android. widget. linearLayout; import android. OS. bundle; import android. OS. environment; import and Roid. view. viewGroup; import android. widget. button; import android. view. view; import android. view. view. onClickListener; import android. content. context; import android. util. log; import android. media. mediaRecorder; import android. media. mediaPlayer; import java. io. IOException; public class AudioRecordTest extends Activity {private static final String LOG_TAG = "AudioRecordTest"; private static String mFileName = Null; // recording button private RecordButton mRecordButton = null; private MediaRecorder mRecorder = null; // playback button private PlayButton mPlayButton = null; private MediaPlayer mPlayer = null; // call this method when the recording button is clicked to start or stop the recording private void onRecord (boolean start) {if (start) {startRecording ();} else {stopRecording () ;}// this method is called when the play button is clicked to start or stop playing private void onPlay (boolean start) {if (start) {startPlaying ();} else {stopPl Aying () ;}} private void startPlaying () {mPlayer = new MediaPlayer (); try {// set the file mPlayer to be played. setDataSource (mFileName); mPlayer. prepare (); // playback of mPlayer. start ();} catch (IOException e) {Log. e (LOG_TAG, "prepare () failed") ;}// stop playing private void stopPlaying () {mPlayer. release (); mPlayer = null;} private void startRecording () {mRecorder = new MediaRecorder (); // set the audio source to MicphonemRecorder. setAudioSource (MediaRecord Er. audioSource. MIC); // set the Encapsulation Format mRecorder. setOutputFormat (MediaRecorder. outputFormat. THREE_GPP); mRecorder. setOutputFile (mFileName); // sets the encoding format mRecorder. setAudioEncoder (MediaRecorder. audioEncoder. AMR_NB); try {mRecorder. prepare ();} catch (IOException e) {Log. e (LOG_TAG, "prepare () failed");} mRecorder. start ();} private void stopRecording () {mRecorder. stop (); mRecorder. release (); mRecorder = null;} // defines the recording button class RecordButton extends Button {boolean mStartRecording = true; OnClickListener clicker = new OnClickListener () {public void onClick (View v) {onRecord (mStartRecording); if (mStartRecording) {setText ("Stop recording");} else {setText ("Start recording");} mStartRecording =! MStartRecording ;}}; public RecordButton (Context ctx) {super (ctx); setText ("Start recording"); setOnClickListener (clicker );}} // define the play Button class PlayButton extends Button {boolean mStartPlaying = true; OnClickListener clicker = new OnClickListener () {public void onClick (View v) {onPlay (mStartPlaying); if (mStartPlaying) {setText ("Stop playing");} else {setText ("Start playing");} mStartPlaying =! MStartPlaying ;}}; public PlayButton (Context ctx) {super (ctx); setText ("Start playing"); setOnClickListener (clicker) ;}// constructor public AudioRecordTest () {mFileName = Environment. getExternalStorageDirectory (). getAbsolutePath (); mFileName + = "/audiorecordtest.3gp" ;}@ Overridepublic void onCreate (Bundle icicle) {super. onCreate (icicle); // constructor LinearLayout ll = new LinearLayout (this); mRecordButton = new RecordBut Ton (this); ll. addView (mRecordButton, new LinearLayout. layoutParams (ViewGroup. layoutParams. WRAP_CONTENT, ViewGroup. layoutParams. WRAP_CONTENT, 0); mPlayButton = new PlayButton (this); ll. addView (mPlayButton, new LinearLayout. layoutParams (ViewGroup. layoutParams. WRAP_CONTENT, ViewGroup. layoutParams. WRAP_CONTENT, 0); setContentView (ll) ;}@ Overridepublic void onPause () {super. onPause (); // release the recording and playing object when the Activity is paused if (m Recorder! = Null) {mRecorder. release (); mRecorder = null;} www.2cto. comif (mPlayer! = Null) {mPlayer. release (); mPlayer = null ;}}}
Next, let's take a deeper look at Android SoundPool and MediaPlayer.
Below