Android Recording (Mediarecorder) with playback (MediaPlayer)

Source: Internet
Author: User

Through Mediarecorder and MediaPlayer to achieve sound recording and playback, the code is relatively simple, directly paste code. There are only four buttons in the XML file that are not posted.

Ui


Code:

Voiceactivity.class

Package Com.zy.ione;import Com.zy.media.uplayer;import Com.zy.media.urecorder;import android.app.activity;import Android.os.bundle;import Android.os.environment;import Android.view.view;import Android.view.View.OnClickListener Import Android.widget.toast;public class Voiceactivity extends Activity {private String path = Null;private Urecorder rec Order;private uplayer player; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate ( Savedinstancestate); This.setcontentview (r.layout.activity_voice);p ath = environment.getexternalstoragedirectory (  ). GetAbsolutePath ();  Path + = "/IONE.PCM"; Recorder = new Urecorder (path);p layer = new Uplayer (path);//Start Recording Findviewbyid (R.ID.IONE_RECORD_START_BTN). Setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick (View arg0) {Toast.maketext (voiceactivity.this , "Start record", Toast.length_short). Show (); Recorder.start ();}); /Stop Recording Findviewbyid (R.ID.IONE_RECORD_STOP_BTN). Setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick (View arg0) {toast.maketext (Voiceactivity.this, "Stop Record", Toast.length_short). Show ();               Recorder.stop ();}}); Start playing Findviewbyid (R.ID.IONE_PLAY_START_BTN). Setonclicklistener (New Onclicklistener () {@Override PU      Blic void OnClick (View arg0) {toast.maketext (Voiceactivity.this, "Start play", Toast.length_short). Show ();      Player.start ();            }      });      Stop playing Findviewbyid (R.ID.IONE_PLAY_STOP_BTN). Setonclicklistener (New Onclicklistener () {@Override      public void OnClick (View arg0) {toast.maketext (Voiceactivity.this, "Stop Play", Toast.length_short). Show ();      Player.stop (); }      });}}
Operating interface
Package Com.zy.media;public Interface Ivoicemanager {public boolean start ();p ublic boolean Stop ();}

Recording

Package Com.zy.media;import Java.io.ioexception;import Android.media.mediarecorder;import android.util.Log;public Class Urecorder implements Ivoicemanager{private final String TAG = URecorder.class.getName ();p rivate String path;  Private Mediarecorder Mrecorder; Public Urecorder (String path) {This.path = Path;mrecorder = new Mediarecorder ();} /* Start Recording * @return Boolean/@Overridepublic Boolean start () {//Set the source to Micphone Mrecorder.setaudiosource (Mediareco Rder.          Audiosource.mic);          Set the encapsulation Format Mrecorder.setoutputformat (MediaRecorder.OutputFormat.THREE_GPP);          Mrecorder.setoutputfile (path);            Set encoding format mrecorder.setaudioencoder (MediaRecorder.AudioEncoder.AMR_NB);          try {mrecorder.prepare ();          } catch (IOException e) {log.e (TAG, "prepare () failed");  }//Recording Mrecorder.start (); return false;}          /* * Stop Recording * @return Boolean/@Overridepublic Boolean stop () {mrecorder.stop (); MrecorDer.release (); Mrecorder = null; return false;}}

Play

Package Com.zy.media;import Android.media.mediaplayer;import Android.util.log;public class Uplayer implements Ivoicemanager {    private final String TAG = UPlayer.class.getName ();p rivate String path;private MediaPlayer mPlayer; Public Uplayer (String path) {This.path = Path;mplayer = new MediaPlayer ();} @Overridepublic Boolean start () {        try {   //Set the file to play Mplayer.setdatasource (path); Mplayer.prepare ();//Play Mplayer.start (); }catch (Exception e) {log.e (TAG, "prepare () failed");   } return false;} @Overridepublic Boolean Stop () {mplayer.stop (); Mplayer.release (); mPlayer = Null;return false;}}


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.