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.
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 | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
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 audio encoder to be used 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 path of the output file to be produced.
| |||||||||||
Pass in the file descriptor of the file to be written.
| |||||||||||
Sets the format of the output file produced during recording.
| |||||||||||
Sets a Surface to show a preview of recorded media (video).
| |||||||||||
Sets the video encoder to be used 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 | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Is called before the object's memory is being reclaimed by the VM.
|
[Expand]
Inherited Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
From class java.lang.Object
|
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.
Default constructor.
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 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 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 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() |
---|
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 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 |
---|
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 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() |
---|
Is called before the object's memory is being reclaimed by the VM. This can only happen once the VM has detected, during a run of the garbage collector, 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.