java.lang.Object | |
↳ | android.media.MediaRecorder |
Used to record audio and video. The recording control is based on a simple state machine (see below).
A common case of using MediaRecorder to record audio works as follows:
MediaRecorder recorder = new MediaRecorder(); 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
See the Audio and Video documentation for additional help with using MediaRecorder.
Note: Currently, MediaRecorder does not work on the emulator.
Nested Classes | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
MediaRecorder.AudioEncoder | Defines the audio encoding. | ||||||||||
MediaRecorder.AudioSource | Defines the audio source. | ||||||||||
MediaRecorder.OnErrorListener | Interface definition for a callback to be invoked when an error occurs while recording. | ||||||||||
MediaRecorder.OnInfoListener | Interface definition for a callback to be invoked when an error occurs while recording. | ||||||||||
MediaRecorder.OutputFormat | Defines the output format. | ||||||||||
MediaRecorder.VideoEncoder | Defines the video encoding. | ||||||||||
MediaRecorder.VideoSource | Defines the video source. |
Constants | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
int | MEDIA_RECORDER_ERROR_UNKNOWN | Unspecified media recorder error. | |||||||||
int | MEDIA_RECORDER_INFO_MAX_DURATION_REACHED | A maximum duration had been setup and has now been reached. | |||||||||
int | MEDIA_RECORDER_INFO_MAX_FILESIZE_REACHED | A maximum filesize had been setup and has now been reached. | |||||||||
int | MEDIA_RECORDER_INFO_UNKNOWN | Unspecified media recorder error. |
Public Constructors | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Default constructor.
|
Public Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Gets the maximum value for audio sources.
| |||||||||||
Returns the maximum absolute amplitude that was sampled since the last
call to this method.
| |||||||||||
Prepares the recorder to begin capturing and encoding data.
| |||||||||||
Releases resources associated with this MediaRecorder object.
| |||||||||||
Restarts the MediaRecorder to its idle state.
| |||||||||||
Sets the number of audio channels for recording.
| |||||||||||
Sets the audio encoder to be used for recording.
| |||||||||||
Sets the audio encoding bit rate for recording.
| |||||||||||
Sets the audio sampling rate for recording.
| |||||||||||
Sets the audio source to be used for recording.
| |||||||||||
Sets a Camera to use for recording.
| |||||||||||
Sets the maximum duration (in ms) of the recording session.
| |||||||||||
Sets the maximum filesize (in bytes) of the recording session.
| |||||||||||
Register a callback to be invoked when an error occurs while
recording.
| |||||||||||
Register a callback to be invoked when an informational event occurs while
recording.
| |||||||||||
Sets the orientation hint for output video playback.
| |||||||||||
Pass in the file descriptor of the file to be written.
| |||||||||||
Sets the path of the output file to be produced.
| |||||||||||
Sets the format of the output file produced during recording.
| |||||||||||
Sets a Surface to show a preview of recorded media (video).
| |||||||||||
Uses the settings from a CamcorderProfile object for recording.
| |||||||||||
Sets the video encoder to be used for recording.
| |||||||||||
Sets the video encoding bit rate for recording.
| |||||||||||
Sets the frame rate of the video to be captured.
| |||||||||||
Sets the width and height of the video to be captured.
| |||||||||||
Sets the video source to be used for recording.
| |||||||||||
Begins capturing and encoding data to the file specified with
setOutputFile().
| |||||||||||
Stops recording.
|
Protected Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Called before the object's memory is reclaimed by the VM.
|
[Expand]
Inherited Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
![]() |
Unspecified media recorder error.
A maximum duration had been setup and has now been reached.
A maximum filesize had been setup and has now been reached.
Unspecified media recorder error.
Gets the maximum value for audio sources.
Returns the maximum absolute amplitude that was sampled since the last call to this method. Call this only after the setAudioSource().
IllegalStateException | if it is called before the audio source has been set. |
---|
Prepares the recorder to begin capturing and encoding data. This method must be called after setting up the desired audio and video sources, encoders, file format, etc., but before start().
IllegalStateException | if it is called after start() or before setOutputFormat(). |
---|---|
IOException | if prepare fails otherwise. |
Releases resources associated with this MediaRecorder object. It is good practice to call this method when you're done using the MediaRecorder.
Restarts the MediaRecorder to its idle state. After calling this method, you will have to configure it again as if it had just been constructed.
Sets the number of audio channels for recording. Call this method before prepare(). Prepare() may perform additional checks on the parameter to make sure whether the specified number of audio channels are applicable.
numChannels | the number of audio channels. Usually it is either 1 (mono) or 2 (stereo). |
---|
Sets the audio encoder to be used for recording. If this method is not called, the output file will not contain an audio track. Call this after setOutputFormat() but before prepare().
audio_encoder | the audio encoder to use. |
---|
IllegalStateException | if it is called before setOutputFormat() or after prepare(). |
---|
Sets the audio encoding bit rate for recording. Call this method before prepare(). Prepare() may perform additional checks on the parameter to make sure whether the specified bit rate is applicable, and sometimes the passed bitRate will be clipped internally to ensure the audio recording can proceed smoothly based on the capabilities of the platform.
bitRate | the audio encoding bit rate in bits per second. |
---|
Sets the audio sampling rate for recording. Call this method before prepare(). Prepare() may perform additional checks on the parameter to make sure whether the specified audio sampling rate is applicable. The sampling rate really depends on the format for the audio recording, as well as the capabilities of the platform. For instance, the sampling rate supported by AAC audio coding standard ranges from 8 to 96 kHz. Please consult with the related audio coding standard for the supported audio sampling rate.
samplingRate | the sampling rate for audio in samples per second. |
---|
Sets the audio source to be used for recording. If this method is not called, the output file will not contain an audio track. The source needs to be specified before setting recording-parameters or encoders. Call this only before setOutputFormat().
audio_source | the audio source to use |
---|
IllegalStateException | if it is called after setOutputFormat() |
---|
Sets a Camera to use for recording. Use this function to switch quickly between preview and capture mode without a teardown of the camera object. Must call before prepare().
c | the Camera to use for recording |
---|
Sets the maximum duration (in ms) of the recording session.
Call this after setOutFormat() but before prepare().
After recording reaches the specified duration, a notification
will be sent to the MediaRecorder.OnInfoListener
with a "what" code of MEDIA_RECORDER_INFO_MAX_DURATION_REACHED
and recording will be stopped. Stopping happens asynchronously, there
is no guarantee that the recorder will have stopped by the time the
listener is notified.
max_duration_ms | the maximum duration in ms (if zero or negative, disables the duration limit) |
---|
IllegalArgumentException |
---|
Sets the maximum filesize (in bytes) of the recording session.
Call this after setOutFormat() but before prepare().
After recording reaches the specified filesize, a notification
will be sent to the MediaRecorder.OnInfoListener
with a "what" code of MEDIA_RECORDER_INFO_MAX_FILESIZE_REACHED
and recording will be stopped. Stopping happens asynchronously, there
is no guarantee that the recorder will have stopped by the time the
listener is notified.
max_filesize_bytes | the maximum filesize in bytes (if zero or negative, disables the limit) |
---|
IllegalArgumentException |
---|
Register a callback to be invoked when an error occurs while recording.
l | the callback that will be run |
---|
Register a callback to be invoked when an informational event occurs while recording.
listener | the callback that will be run |
---|
Sets the orientation hint for output video playback. This method should be called before prepare(). This method will not trigger the source video frame to rotate during video recording, but to add a composition matrix containing the rotation angle in the output video if the output format is OutputFormat.THREE_GPP or OutputFormat.MPEG_4 so that a video player can choose the proper orientation for playback. Note that some video players may choose to ignore the compostion matrix in a video during playback.
degrees | the angle to be rotated clockwise in degrees. The supported angles are 0, 90, 180, and 270 degrees. |
---|
IllegalArgumentException | if the angle is not supported. |
---|
Pass in the file descriptor of the file to be written. Call this after setOutputFormat() but before prepare().
fd | an open file descriptor to be written into. |
---|
IllegalStateException | if it is called before setOutputFormat() or after prepare() |
---|
Sets the path of the output file to be produced. Call this after setOutputFormat() but before prepare().
path | The pathname to use. |
---|
IllegalStateException | if it is called before setOutputFormat() or after prepare() |
---|
Sets the format of the output file produced during recording. Call this after setAudioSource()/setVideoSource() but before prepare().
It is recommended to always use 3GP format when using the H.263 video encoder and AMR audio encoder. Using an MPEG-4 container format may confuse some desktop players.
output_format | the output format to use. The output format needs to be specified before setting recording-parameters or encoders. |
---|
IllegalStateException | if it is called after prepare() or before setAudioSource()/setVideoSource(). |
---|
Sets a Surface to show a preview of recorded media (video). Calls this before prepare() to make sure that the desirable preview display is set.
sv | the Surface to use for the preview |
---|
Uses the settings from a CamcorderProfile object for recording. This method should be called after the video AND audio sources are set, and before setOutputFile().
profile | the CamcorderProfile to use |
---|
Sets the video encoder to be used for recording. If this method is not called, the output file will not contain an video track. Call this after setOutputFormat() and before prepare().
video_encoder | the video encoder to use. |
---|
IllegalStateException | if it is called before setOutputFormat() or after prepare() |
---|
Sets the video encoding bit rate for recording. Call this method before prepare(). Prepare() may perform additional checks on the parameter to make sure whether the specified bit rate is applicable, and sometimes the passed bitRate will be clipped internally to ensure the video recording can proceed smoothly based on the capabilities of the platform.
bitRate | the video encoding bit rate in bits per second. |
---|
Sets the frame rate of the video to be captured. Must be called after setVideoSource(). Call this after setOutFormat() but before prepare().
rate | the number of frames per second of video to capture |
---|
IllegalStateException | if it is called after prepare() or before setOutputFormat(). NOTE: On some devices that have auto-frame rate, this sets the maximum frame rate, not a constant frame rate. Actual frame rate will vary according to lighting conditions. |
---|
Sets the width and height of the video to be captured. Must be called after setVideoSource(). Call this after setOutFormat() but before prepare().
width | the width of the video to be captured |
---|---|
height | the height of the video to be captured |
IllegalStateException | if it is called after prepare() or before setOutputFormat() |
---|
Sets the video source to be used for recording. If this method is not called, the output file will not contain an video track. The source needs to be specified before setting recording-parameters or encoders. Call this only before setOutputFormat().
video_source | the video source to use |
---|
IllegalStateException | if it is called after setOutputFormat() |
---|
Begins capturing and encoding data to the file specified with setOutputFile(). Call this after prepare().
IllegalStateException | if it is called before prepare(). |
---|
Stops recording. Call this after start(). Once recording is stopped, you will have to configure it again as if it has just been constructed.
IllegalStateException | if it is called before start() |
---|
Called before the object's memory is reclaimed by the VM. This can only happen once the garbage collector has detected that the object is no longer reachable by any thread of the running application.
The method can be used to free system resources or perform other cleanup
before the object is garbage collected. The default implementation of the
method is empty, which is also expected by the VM, but subclasses can
override finalize()
as required. Uncaught exceptions which are
thrown during the execution of this method cause it to terminate
immediately but are otherwise ignored.
Note that the VM does guarantee that finalize()
is called at most
once for any object, but it doesn't guarantee when (if at all) finalize()
will be called. For example, object B's finalize()
can delay the execution of object A's finalize()
method and
therefore it can delay the reclamation of A's memory. To be safe, use a
ReferenceQueue
, because it provides more control
over the way the VM deals with references during garbage collection.