Android audio recording and Android audio recording

Source: Internet
Author: User

Android audio recording and Android audio recording
MediaRecorder class details
Mobile phones generally have microphones and cameras, and Android systems can use these hardware to record audio and video.
Android provides a MediaRecorder class to support audio and video recording. This class is also very simple to use. Let's take a look at this class.

1. Structure of java. lang. Object-> android. media. MediaRecorder


Ii. Category Overview: A class used to record audio and video.

Iii. status chart:



Note:


Similar to the MediaPlayer class, MediaRecorder also has its own status chart. The following describes the status of MediaRecorder:


1. Initial: Initial state. When a MediaRecorder object is created or the reset () method is called, The MediaRecorder object is in the Initial state. After the video or audio source is set, it is converted to the Initialized status. Besides the Released status, you can call the reset () method to enable MediaRecorder to enter this status.


2. Initialized: the status has been Initialized. You can call setAudioSource () or setVideoSource () in the Initial status to enter this status. In this status, you can use the setOutputFormat () method to set the output format. In this case, the MediaRecorder is converted to the performanceconfigured status. In addition, use the reset () method to enter the Initial state.


3. performanceconfigured: data source configuration status. During this period, you can set the encoding mode, output file, screen rotation, Preview display, and so on. You can use the setOutputFormat () method to enter the Initialized status. In addition, you can use the reset () method to return to the Initial state, or use the prepare () method to reach the Prepared state.


4. Prepared: Ready. In the performanceconfigured status, the prepare () method is used to enter this status. In this status, start () can be used to enter the recording status. In addition, you can return to the Initialized state through the reset () method.


5. Recording: Specifies the Recording status. You can call the start () method in the Prepared status to enter this status. In addition, it can return to the Initial state through the stop () method or reset () method.


6. Released: release status (the term "Idle state" in the official document). You can call the release () method in the Initial status to enter this status, all resources bound to the MediaRecorder object will be released.


7. Error: indicates the Error status. When an Error occurs, it enters the Initial status through the reset () method.


Tip: similar to MediaPlayer, when using MediaRecorder for recording videos, you must strictly abide by the sequence of function calls in the state chart description. Different functions are called in different States. Otherwise, an exception occurs.

The following instance Code describes the creation process:


/*** Start recording */private void startRecording () {BaseUtils. printd (TAG, "startRecording"); mParams. mFileName = mParams. mFileDir + System. currentTimeMillis () + "amr"; mRecorder = new MediaRecorder (); mRecorder. setAudioSource (MediaRecorder. audioSource. MIC); mRecorder. setOutputFormat (MediaRecorder. outputFormat. RAW_AMR); mRecorder. setAudioEncoder (MediaRecorder. audioEncoder. AMR_NB); mRecorder. setOutputFile (mParams. mFileName); mRecorder. setOnInfoListener (mRecorderOnInfoListener); if (mParams. mMaxFileSize> 0) {mRecorder. setMaxFileSize (mParams. mMaxFileSize);} if (mParams. mMaxDurationSize> 0) {mRecorder. setMaxDuration (mParams. mMaxDurationSize);} try {mRecorder. prepare (); mRecorder. start (); mObtainDecibelThread = new ObtainDecibelThread (); mObtainDecibelThread. start ();} catch (Exception e) {BaseUtils. printe ("Exception", "Exception:" + e );}}



Related Article

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.