There are three ways to record in Android:
First, the use of intent, the simplest but not flexible (this article only talk about the intention to record)
Second, the use of the MediaPlayer class, he is more difficult to achieve, but provides more flexibility
Third, the use of the Audiorecord class, which provides the greatest flexibility, but for our work to do the least.
Source:
<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools " android:layout_width=" match_parent " android:layout_height=" Match_parent " android:orientation= "vertical" > <button android:id= "@+id/button_createrecording" Android:layout_width= "Match_parent" android:layout_height= "wrap_content" android:text= "recording"/> <button android:id= "@+id/button_playrecording" android:layout_width= "Match_parent" android: layout_height= "Wrap_content" android:text= "play recording"/> </LinearLayout>
Package Com.multimediademo8intent;import Android.app.activity;import Android.content.intent;import Android.media.mediaplayer;import Android.media.mediaplayer.oncompletionlistener;import Android.net.Uri;import Android.os.bundle;import Android.provider.mediastore;import Android.view.view;import Android.view.view.onclicklistener;import Android.widget.button;import Com.multimediademo8audiorecord.r;public Class Mainactivity extends Activity implements Onclicklistener,oncompletionlistener {private Button Button_ Createrecording, button_playrecording;/** * Create a constant named Record_request and pass it to the Startactivityforresult function, * This allows you to determine the source of any Onactivityresult call (which will be called when the Sound Recorder ends operation) */public static int record_request = 0;private Uri audiofileuri;@ overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.activity_main) button_createrecording = (Button) Findviewbyid (r.id.button_createrecording); button_ playrecording = (Button) Findviewbyid (R.id.button_playrecording); Button_createrecording.setonclicklistener (this), Button_playrecording.setonclicklistener (this); Button_ Playrecording.setenabled (false);} @Overridepublic void OnClick (View v) {if (v = = button_createrecording) {Intent Intent = new Intent (MediaStore.Audio.Media. record_sound_action);/** * When triggered by the Startactivityforresult function, any activity will call the Onactivityresult method when returned, * by passing a unique constant along with the intent, We are able to differentiate them in the Onactivityresult method. **/startactivityforresult (Intent, record_request);} else if (v = = button_playrecording) {MediaPlayer MediaPlayer = Mediaplayer.create (this, Audiofileuri); Mediaplayer.setoncompletionlistener (this); Mediaplayer.start (); button_playrecording.setenabled (false);}} /** * Triggers the Onactivityresult method when the recorder activity is complete. The ResultCode should be equal to the RESULT_OK constant, * and requestcode should be equal to the value passed to the Startactivityforresult method *, that is, record_request, if two equations are true, You can get the audio recording file's URI *, that is, the intent of the GetData method to extract it out, once the above action is completed, you can enable the Playrecording button, so that the returned audio file will be played. */@Overrideprotected void onactivityresult (int requestcode, int resultcode, Intent data) {if (ResultCode = = RESULT_OK && Requestcode = = record_request) {Audiofileuri = Data.getdata (); Button_playrecording.setenabled (True );}} /** * Finally, when MediaPlayer finishes playing a file, the Oncompletion method is called, which re-enables the Playrecording button to enable the user to listen to the audio file again. */@Overridepublic void Oncompletion (MediaPlayer MP) {button_playrecording.setenabled (true);}}
Source code Download:
Click to download the source code