Android Multimedia-mediarecorder recording audio and video

Source: Internet
Author: User

Not much to say, directly on the code, with detailed comments

Myaudiorecord.java

Import Java.io.file;import java.io.ioexception;import android.app.activity;import android.content.ContentValues; Import Android.content.intent;import Android.media.mediaplayer;import Android.media.mediarecorder;import Android.net.uri;import Android.os.bundle;import Android.os.environment;import Android.provider.MediaStore;import Android.view.view;import Android.widget.button;import android.widget.textview;/** * This is the use of the Mediarecorder class to implement their own audio recording program * * In order to record audio we need Record_audio permissions * In order to write sdcard, we need write_external_storage permissions * @author Administrator * */public class Myaudiorecord extends Activity {private TextView stateview;private Button btnstart,btnstop , btnplay,btnfinish;private mediarecorder recorder;private MediaPlayer player;private File audiofile;private Uri fileuri;public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.my_audio_record); Stateview = (TextView) This.findviewbyid (r.id.view_state); Stateview.settext ("Ready to start"); btnstart = (Button) tHis.findviewbyid (r.id.btn_start); btnstop = (Button) This.findviewbyid (r.id.btn_stop); btnplay = (Button) This.findviewbyid (r.id.btn_play); btnfinish = (Button) This.findviewbyid (r.id.btn_finish); Btnstop.setenabled (False ); btnplay.setenabled (false);} public void OnClick (View v) {int id = v.getid (); switch (ID) {case r.id.btn_start://start recording//We need to instantiate a Mediarecorder object, Then make the appropriate settings recorder = new Mediarecorder (),//Specify Audiosource as MIC (microphone audio source), which is the longest used Recorder.setaudiosource ( MediaRecorder.AudioSource.MIC);//setaudiosource/setvediosource//Specify OutputFormat, we choose 3GP Format//Other format, MPEG-4: This specifies that the recorded file is in mpeg-4 format//RAW_AMR: Recording the original file, which only supports audio recording, and requires an audio encoding of AMR_NB//THREE_GPP: After the recording is a 3GP file, Support audio and video recording Recorder.setoutputformat (MediaRecorder.OutputFormat.THREE_GPP);//Specify audio encoding method, currently only Amr_ NB format Recorder.setaudioencoder (MediaRecorder.AudioEncoder.AMR_NB);//Next we need to specify the storage path of the recorded file Fpath = new files ( Environment.getexternalstoragedirectory (). GetAbsolutePath () + "/data/files/"); Fpath.mkdirs ();//Create folder try {// Create temporary file Audiofile = File.createtempfilE ("Recording", ". 3gp", Fpath);} catch (IOException e) {//TODO auto-generated catch Blocke.printstacktrace ();} Recorder.setoutputfile (Audiofile.getabsolutepath ());//start recording the try {Recorder.prepare ();} catch ( IllegalStateException e) {//Todo auto-generated catch Blocke.printstacktrace ();} catch (IOException e) {//Todo Auto-gene Rated catch Blocke.printstacktrace ();} Recorder.start (); Stateview.settext ("recording"); btnstart.setenabled (false); btnplay.setenabled (false); Btnstop.setenabled (true); Break;case r.id.btn_stop:recorder.stop (); Recorder.release ();// We can then store our recording files in Mediastore contentvalues values = new Contentvalues (); Values.put (MediaStore.Audio.Media.TITLE, " This is my first Record-audio "); Values.put (MediaStore.Audio.Media.DATE_ADDED, System.currenttimemillis ()); Values.put (MediaStore.Audio.Media.DATA, Audiofile.getabsolutepath ()); Fileuri = This.getcontentresolver (). Insert ( MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, values);//After recording is complete, we instantiate a MediaPlayer object and prepare to play player = new MediaPlayer () ;p Layer. Setoncompletionlistener (New Mediaplayer.oncompletionlistener () {@Overridepublic void oncompletion (MediaPlayer arg0 {//Update status Stateview.settext ("Ready to Record"); btnplay.setenabled (true); btnstart.setenabled (true); btnstop.setenabled (false) ;}});/ /ready to play try {Player.setdatasource (Audiofile.getabsolutepath ());p layer.prepare ();} catch (IllegalArgumentException e) { TODO auto-generated catch Blocke.printstacktrace ();} catch (IllegalStateException e) {//Todo auto-generated catch Blocke.printstacktrace ();} catch (IOException e) {//Todo Au To-generated catch Blocke.printstacktrace ();} Update Status Stateview.settext ("Ready to play"); btnplay.setenabled (true); btnstart.setenabled (true); btnstop.setenabled (false); Break;case r.id.btn_play://Play Recording//note, we at the end of the recording, we have instantiated the MediaPlayer, ready to play the preparation player.start ();//Update Status Stateview.settext ("playing"); btnstart.setenabled (false); btnstop.setenabled (false); btnplay.setenabled (false);//update state break at the end of playback; Case r.id.btn_finish://completes the recording, returning the recorded audio to uriintent intent = new Intent (); Intent.setdata (Fileuri); this.setResult (RESULT_OK, intent); This.finish (); break;}}} 
Androidmanifest.xml

<uses-permission android:name= "Android.permission.RECORD_AUDIO"/>     



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: info-contact@alibabacloud.com 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.