Explanation of MediaRecorder class in Android Development
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:
I. Class Structure:
Java. lang. Object |
? |
Android. media. MediaRecorder |
Ii. Category Overview:
A class used to record audio and video.
Iii. status chart:
VcmRlcl9zdGF0ZV9kaWFncmFt ">
Note:
Similar to the MediaPlayer class, MediaRecorder also has its own status chart. The following describes the status of MediaRecorder:
Initial: Initial state. When a MediaRecorder object is created using the new () method 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.
Initialized: the initialization status. 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.
DataSourceConfigured: 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.
Prepared: Ready. In the performanceconfigured status, you can use the prepare () method 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.
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.
Released: release status (the term "Idle state Idle" in the official documentation). You can call the release () method in the Initial status to enter this status, all resources bound to the MediaRecorder object will be released.
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 describes the creation process:
MediaRecorder recorder = newMediaRecorder (); Recorder. setAudioSource (MediaRecorder. AudioSource. MIC ); Recorder. setOutputFormat (MediaRecorder. OutputFormat. THREE_GPP ); Recorder. setAudioEncoder (MediaRecorder. AudioEncoder. AMR_NB ); Recorder. setOutputFile (PATH_NAME ); Recorder. prepare (); Recorder. start (); // Recording is now started ... Recorder. stop (); Recorder. reset (); // You can reuse the object by going back to setAudioSource () step Recorder. release (); // Now the object cannot be reused |
Iv. constructor and public Method
Public Constructors |
|
MediaRecorder () Default constructor. |
Public Methods |
Final static int |
GetAudioSourceMax () Obtains the maximum value of the audio source. |
Int |
GetMaxAmplitude () Obtain the maximum amplitude of the recording after the previous call. |
Void |
Prepare () Prepare the recording. |
Void |
Release () Release resources. |
Void |
Reset () Set MediaRecorder to idle, that is, Initial. |
Void |
SetAudioChannels (int numChannels) Set the number of audio channels for recording. |
Void |
SetAudioEncoder (int audio_encoder) Sets the encoding format of the recorded sound. |
Void |
SetAudioEncodingBitRate (int bitRate) Sets the encoding bit rate of the recorded sound. |
Void |
SetAudioSamplingRate (int samplingRate) Set the sampling rate of the recorded sound. |
Void |
SetAudioSource (int audio_source) Set the sound source. Generally, the MediaRecorder. AudioSource. MIC parameter is input to specify the sound from the microphone. |
Void |
SetCamera (Camera c) Set a camera for recording. |
Void |
SetCaptureRate (double fps) Sets the video frame capture rate. |
Void |
SetLocation (float latitude, float longpolling) Set and store geographic data (longitude and latitude) in the output file ). |
Void |
SetMaxDuration (int max_duration_ms) Sets the maximum duration (in ms) of a recording session ). |
Void |
SetMaxFileSize (long max_filesize_bytes) Set the maximum file size of the recording file. |
Void |
SetOnErrorListener (MediaRecorder. OnErrorListener l) Register a listener for recording errors. |
Void |
SetOnInfoListener (MediaRecorder. OnInfoListener listener) Register an information event for recording. |
Void |
SetOrientationHint (int degrees) Sets the direction of the output video. |
Void |
SetOutputFile (FileDescriptor fd) Set the location of the recorded audio file. |
Void |
SetOutputFile (String path) Set the location of the recorded audio file. |
Void |
SetOutputFormat (int output_format) Set the format of the recorded audio and video files. |
Void |
SetPreviewDisplay (Surface sv) Set the SurfaceView used to display the video preview. |
Void |
SetProfile (CamcorderProfile profile) Specifies the CamcorderProfile object. |
Void |
SetVideoEncoder (int video_encoder) Sets the encoding format of the recorded video. |
Void |
SetVideoEncodingBitRate (int bitRate) Sets the encoding bit rate of the recorded video. |
Void |
SetVideoFrameRate (int rate) Sets the frame capture rate for the recorded video. |
Void |
SetVideoSize (int width, int height) Set the width and height of the video. |
Void |
SetVideoSource (int video_source) Sets the video source for recording. |
Void |
Start () Start recording. |
Void |
Stop () Stop recording. |
5. Use MediaRecorder to record music and use MediaRecorder to record videos.