Android MediaRecorder recording and Playback

Source: Internet
Author: User

The previous article talked about the use of intent recording. This article will use the MediaRecorder class for recording to provide more flexibility.

:


Source code:


     
      
      
      
      
  
 


Package com. multimediademo9mediarecorder; import java. io. file; import android. app. activity; import android. media. mediaPlayer; import android. media. mediaPlayer. onCompletionListener; import android. media. mediaRecorder; import android. OS. bundle; import android. OS. environment; import android. view. view; import android. view. view. onClickListener; import android. widget. button; import android. widget. textView; public cl Ass MainActivity extends Activity implements OnClickListener, OnCompletionListener {private TextView statusTextView; private Button button_startRecording, expiration, expiration, button_finish; private MediaRecorder recorder; private MediaPlayer player; private File audioFile; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentVie W (R. layout. activity_main); init ();}/*** instantiate component */private void init () {statusTextView = (TextView) findViewById (R. id. statusTextView); // when running the Activity, set the text of statusTextView to "Ready ". StatusTextView. setText ("Ready"); button_startRecording = (Button) findViewById (R. id. button_startRecording); button_playRecording = (Button) findViewById (R. id. button_playRecording); button_stopRecording = (Button) findViewById (R. id. button_stopRecording); button_finish = (Button) findViewById (R. id. button_finish); button_startRecording.setOnClickListener (this); button_playRecording.setOnClickListener (this ); Listener (this); button_finish.setOnClickListener (this); button_playRecording.setEnabled (false); listener (false); player = new MediaPlayer () ;}@ Overridepublic void onClick (View v) {switch (v. getId () {case R. id. button_startRecording: try {/*** when you click the Start recording button, a new MediaRecorder is constructed and the setAudioSource, * setOutputFormat, and setAudioEncoder methods are called. */Recorder = new MediaRecorder (); recorder. setAudioSource (MediaRecorder. audioSource. MIC); recorder. setOutputFormat (MediaRecorder. outputFormat. THREE_GPP); recorder. setAudioEncoder (MediaRecorder. audioEncoder. AMR_NB);/*** then create a new File object on the SD card and call the setOutputFile method on the MediaRecorder object. */File path = new File (Environment. getExternalStorageDirectory (). getAbsoluteFile () + "/files/"); path. mkdir (); audioFile = File. createTempFile ("recording ",". 3gp ", path); recorder. setOutputFile (audioFile. getAbsolutePath ();/*** call the prepare method on MediaRecorder and start recording. */Recorder. prepare (); recorder. start ();/*** last update statusTextView and change the buttons to be enabled or disabled. */StatusTextView. setText ("Recording"); button_playRecording.setEnabled (false); button_stopRecording.setEnabled (true); button_startRecording.setEnabled (false);} catch (Exception e) {e. printStackTrace ();} break; case R. id. button_playRecording:/*** play the recording. The MediaPlayer object player */player is used. start (); statusTextView. setText ("playing"); button_playRecording.setEnabled (false); button_stopRecording.setEnabled (false); Button_startRecording.setEnabled (false); break; case R. id. button_stopRecording:/*** stop recording and release the MediaRecorder object. */Try {recorder. stop (); recorder. release (); player. setOnCompletionListener (this); player. setDataSource (audioFile. getAbsolutePath (); player. prepare (); statusTextView. setText ("Ready to Play !! "); Button_playRecording.setEnabled (true); button_stopRecording.setEnabled (false);} catch (Exception e) {e. printStackTrace ();} break; case R. id. button_finish: finish (); break; default: break; }}@ Overridepublic void onCompletion (MediaPlayer mp) {encrypt (true); button_startRecording.setEnabled (true); terminate (false); statusTextView. setText ("Ready... ");}}


Source code download:

Click to download source code

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.