Phones generally have microphones and cameras, and the Android system can use the hardware to record audio and video.
To increase support for recorded audio and video, the Android system provides a Mediarecorder class. The use of this class is also very simple, let's look at this class:
First, class structure:
|
Java.lang.Object |
|
? |
Android.media.MediaRecorder |
Ii. Overview of the class:
A class for recording audio and video.
Three, the state diagram:
VCMRLCL9ZDGF0ZV9KAWFNCMFT ">
Description
Very similar to the MediaPlayer class Mediarecorder has its own state diagram. Here is an introduction to the various states of Mediarecorder:
Initial: The 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 source or audio source is set, it is converted to the initialized state. In addition, any State other than the released state can get Mediarecorder into that state by calling the Reset () method.
Initialized: The state is initialized and can be entered by calling the Setaudiosource () or Setvideosource () method in the initial state. In this state, the output format can be set by the Setoutputformat () method, at which point the Mediarecorder is converted to the datasourceconfigured state. In addition, the reset () method is used to enter the initial state.
Datasourceconfigured: Data source configuration status, during which you can set encoding, output file, screen rotation, preview display, and so on. This state can be entered in the initialized state through the Setoutputformat () method. In addition, the reset () method can be used to return to the initial state or to the prepared state via the Prepare () method.
Prepared: Ready state, in datasourceconfigured state through the prepare () method into the state. In this state you can enter the recording state via Start (). In addition, the reset () method can be used to return to the initialized state.
Recording: Recording status can be entered in the prepared state by calling the start () method. In addition, it can be returned to the initial state via the Stop () method or the Reset () method.
Released: Release state (the official document gives the word "idle state"), you can enter this state by calling the release () method in the initial state, which frees all resources that are bound to the Mediarecorder object.
Error: In the wrong state, when the error occurs, it can enter the initial state via the Reset () method.
Tip: Similar to MediaPlayer using Mediarecorder recording, you need to strictly follow the sequence of function calls in the state diagram description, call different functions in different states, or else an exception will occur.
The following example 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 is reused |
Iv. structural methods and public methods
|
Public constructors |
|
Mediarecorder ()
Default constructor. |
|
Public Methods |
|
Final static int |
Getaudiosourcemax ()
Gets the maximum value of the audio source. |
|
Int |
Getmaxamplitude ()
Gets the maximum amplitude that appears in the recording after the previous call to this method. |
|
void |
Prepare ()
Prepare the recording. |
|
void |
Release ()
Frees resources. |
|
void |
Reset ()
Mediarecorder is set to idle, which is the initial state. |
|
void |
Setaudiochannels (int numchannels)
Sets the number of audio channels recorded. |
|
void |
Setaudioencoder (int audio_encoder)
Sets the encoding format for the recorded sound. |
|
void |
setaudioencodingbitrate (int bitrate)
Sets the encoded bit rate for the recorded sound. |
|
void |
setaudiosamplingrate (int samplingrate)
Sets the sample rate for the recorded sound. |
|
void |
Setaudiosource (int audio_source)
Set the sound source, typically incoming mediarecorder. The audiosource.mic parameter specifies that the sound from the microphone is recorded. |
|
void |
Setcamera (Camera c)
Set up a camera for recording. |
|
void |
Setcapturerate (double fps)
Sets the video frame capture rate. |
|
void |
setlocation (float latitude, float longitude)
Sets and stores the geographic data (longitude and latitude) in the output file. |
|
void |
setmaxduration (int Max_duration_ms)
Sets the maximum duration, in MS, for a recording session. |
|
void |
Setmaxfilesize (Long Max_filesize_bytes)
Sets the maximum file size for the recording file. |
|
void |
Setonerrorlistener (Mediarecorder.onerrorlistener L)
Registers a listener that is used to record errors that occur when recording. |
|
void |
Setoninfolistener (Mediarecorder.oninfolistener Listener)
Registers a message event that occurs when a recording is recorded. |
|
void |
Setorientationhint (int degrees)
Sets the direction prompt for the output video playback. |
|
void |
Setoutputfile (FileDescriptor FD)
Sets the location where the recorded audio files are saved. |
|
void |
Setoutputfile (String Path)
Sets the location where the recorded audio files are saved. |
|
void |
Setoutputformat (int output_format)
Sets the format of the recorded audio and video file. |
|
void |
Setpreviewdisplay (Surface SV)
Sets which surfaceview to use to display the video preview. |
|
void |
Setprofile (Camcorderprofile profile)
Specifies the Camcorderprofile object. |
|
void |
Setvideoencoder (int video_encoder)
Sets the encoding format for the recorded video. |
|
void |
setvideoencodingbitrate (int bitrate)
Sets the encoding bitrate for the recorded video. |
|
void |
setvideoframerate (int rate)
Sets the capture frame rate for recorded video. |
|
void |
setvideosize (int width, int height)
Sets the width to shoot and the height of the video. |
|
void |
Setvideosource (int video_source)
Sets the source of the video used for recording. |
|
void |
Start ()
Start recording. |
|
void |
Stop ()
Stop recording. |
V. Examples of applications Use Mediarecorder to record music using Mediarecorder to record video
Android Development Mediarecorder Class