public class

MediaRecorder

extends Object
java.lang.Object
   ↳ android.media.MediaRecorder

Class Overview

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.

Summary

Nested Classes
class MediaRecorder.AudioEncoder Defines the audio encoding. 
class MediaRecorder.AudioSource Defines the audio source. 
interface MediaRecorder.OnErrorListener Interface definition for a callback to be invoked when an error occurs while recording. 
interface MediaRecorder.OnInfoListener Interface definition for a callback to be invoked when an error occurs while recording. 
class MediaRecorder.OutputFormat Defines the output format. 
class MediaRecorder.VideoEncoder Defines the video encoding. 
class 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
MediaRecorder()
Default constructor.
Public Methods
final static int getAudioSourceMax()
Gets the maximum value for audio sources.
int getMaxAmplitude()
Returns the maximum absolute amplitude that was sampled since the last call to this method.
void prepare()
Prepares the recorder to begin capturing and encoding data.
void release()
Releases resources associated with this MediaRecorder object.
void reset()
Restarts the MediaRecorder to its idle state.
void setAudioChannels(int numChannels)
Sets the number of audio channels for recording.
void setAudioEncoder(int audio_encoder)
Sets the audio encoder to be used for recording.
void setAudioEncodingBitRate(int bitRate)
Sets the audio encoding bit rate for recording.
void setAudioSamplingRate(int samplingRate)
Sets the audio sampling rate for recording.
void setAudioSource(int audio_source)
Sets the audio source to be used for recording.
void setCamera(Camera c)
Sets a Camera to use for recording.
void setMaxDuration(int max_duration_ms)
Sets the maximum duration (in ms) of the recording session.
void setMaxFileSize(long max_filesize_bytes)
Sets the maximum filesize (in bytes) of the recording session.
void setOnErrorListener(MediaRecorder.OnErrorListener l)
Register a callback to be invoked when an error occurs while recording.
void setOnInfoListener(MediaRecorder.OnInfoListener listener)
Register a callback to be invoked when an informational event occurs while recording.
void setOutputFile(String path)
Sets the path of the output file to be produced.
void setOutputFile(FileDescriptor fd)
Pass in the file descriptor of the file to be written.
void setOutputFormat(int output_format)
Sets the format of the output file produced during recording.
void setPreviewDisplay(Surface sv)
Sets a Surface to show a preview of recorded media (video).
void setProfile(CamcorderProfile profile)
Uses the settings from a CamcorderProfile object for recording.
void setVideoEncoder(int video_encoder)
Sets the video encoder to be used for recording.
void setVideoEncodingBitRate(int bitRate)
Sets the video encoding bit rate for recording.
void setVideoFrameRate(int rate)
Sets the frame rate of the video to be captured.
void setVideoSize(int width, int height)
Sets the width and height of the video to be captured.
void setVideoSource(int video_source)
Sets the video source to be used for recording.
void start()
Begins capturing and encoding data to the file specified with setOutputFile().
void stop()
Stops recording.
Protected Methods
void finalize()
Is called before the object's memory is being reclaimed by the VM.
[Expand]
Inherited Methods
From class java.lang.Object

Constants

public static final int MEDIA_RECORDER_ERROR_UNKNOWN

Since: API Level 3

Unspecified media recorder error.

Constant Value: 1 (0x00000001)

public static final int MEDIA_RECORDER_INFO_MAX_DURATION_REACHED

Since: API Level 3

A maximum duration had been setup and has now been reached.

Constant Value: 800 (0x00000320)

public static final int MEDIA_RECORDER_INFO_MAX_FILESIZE_REACHED

Since: API Level 3

A maximum filesize had been setup and has now been reached.

Constant Value: 801 (0x00000321)

public static final int MEDIA_RECORDER_INFO_UNKNOWN

Since: API Level 3

Unspecified media recorder error.

Constant Value: 1 (0x00000001)

Public Constructors

public MediaRecorder ()

Since: API Level 1

Default constructor.

Public Methods

public static final int getAudioSourceMax ()

Since: API Level 4

Gets the maximum value for audio sources.

public int getMaxAmplitude ()

Since: API Level 1

Returns the maximum absolute amplitude that was sampled since the last call to this method. Call this only after the setAudioSource().

Returns
  • the maximum absolute amplitude measured since the last call, or 0 when called for the first time
Throws
IllegalStateException if it is called before the audio source has been set.

public void prepare ()

Since: API Level 1

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().

Throws
IllegalStateException if it is called after start() or before setOutputFormat().
IOException if prepare fails otherwise.

public void release ()

Since: API Level 1

Releases resources associated with this MediaRecorder object. It is good practice to call this method when you're done using the MediaRecorder.

public void reset ()

Since: API Level 1

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.

public void setAudioChannels (int numChannels)

Since: API Level 8

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.

Parameters
numChannels the number of audio channels. Usually it is either 1 (mono) or 2 (stereo).

public void setAudioEncoder (int audio_encoder)

Since: API Level 1

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().

Parameters
audio_encoder the audio encoder to use.
Throws
IllegalStateException if it is called before setOutputFormat() or after prepare().

public void setAudioEncodingBitRate (int bitRate)

Since: API Level 8

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.

Parameters
bitRate the audio encoding bit rate in bits per second.

public void setAudioSamplingRate (int samplingRate)

Since: API Level 8

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.

Parameters
samplingRate the sampling rate for audio in samples per second.

public void setAudioSource (int audio_source)

Since: API Level 1

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().

Parameters
audio_source the audio source to use
Throws
IllegalStateException if it is called after setOutputFormat()

public void setCamera (Camera c)

Since: API Level 3

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().

Parameters
c the Camera to use for recording

public void setMaxDuration (int max_duration_ms)

Since: API Level 3

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.

Parameters
max_duration_ms the maximum duration in ms (if zero or negative, disables the duration limit)

public void setMaxFileSize (long max_filesize_bytes)

Since: API Level 3

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.

Parameters
max_filesize_bytes the maximum filesize in bytes (if zero or negative, disables the limit)

public void setOnErrorListener (MediaRecorder.OnErrorListener l)

Since: API Level 3

Register a callback to be invoked when an error occurs while recording.

Parameters
l the callback that will be run

public void setOnInfoListener (MediaRecorder.OnInfoListener listener)

Since: API Level 3

Register a callback to be invoked when an informational event occurs while recording.

Parameters
listener the callback that will be run

public void setOutputFile (String path)

Since: API Level 1

Sets the path of the output file to be produced. Call this after setOutputFormat() but before prepare().

Parameters
path The pathname to use.
Throws
IllegalStateException if it is called before setOutputFormat() or after prepare()

public void setOutputFile (FileDescriptor fd)

Since: API Level 3

Pass in the file descriptor of the file to be written. Call this after setOutputFormat() but before prepare().

Parameters
fd an open file descriptor to be written into.
Throws
IllegalStateException if it is called before setOutputFormat() or after prepare()

public void setOutputFormat (int output_format)

Since: API Level 1

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.

Parameters
output_format the output format to use. The output format needs to be specified before setting recording-parameters or encoders.
Throws
IllegalStateException if it is called after prepare() or before setAudioSource()/setVideoSource().

public void setPreviewDisplay (Surface sv)

Since: API Level 1

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.

Parameters
sv the Surface to use for the preview

public void setProfile (CamcorderProfile profile)

Since: API Level 8

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().

Parameters
profile the CamcorderProfile to use
See Also

public void setVideoEncoder (int video_encoder)

Since: API Level 3

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().

Parameters
video_encoder the video encoder to use.
Throws
IllegalStateException if it is called before setOutputFormat() or after prepare()

public void setVideoEncodingBitRate (int bitRate)

Since: API Level 8

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.

Parameters
bitRate the video encoding bit rate in bits per second.

public void setVideoFrameRate (int rate)

Since: API Level 3

Sets the frame rate of the video to be captured. Must be called after setVideoSource(). Call this after setOutFormat() but before prepare().

Parameters
rate the number of frames per second of video to capture
Throws
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.

public void setVideoSize (int width, int height)

Since: API Level 3

Sets the width and height of the video to be captured. Must be called after setVideoSource(). Call this after setOutFormat() but before prepare().

Parameters
width the width of the video to be captured
height the height of the video to be captured
Throws
IllegalStateException if it is called after prepare() or before setOutputFormat()

public void setVideoSource (int video_source)

Since: API Level 3

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().

Parameters
video_source the video source to use
Throws
IllegalStateException if it is called after setOutputFormat()

public void start ()

Since: API Level 1

Begins capturing and encoding data to the file specified with setOutputFile(). Call this after prepare().

Throws
IllegalStateException if it is called before prepare().

public void stop ()

Since: API Level 1

Stops recording. Call this after start(). Once recording is stopped, you will have to configure it again as if it has just been constructed.

Throws
IllegalStateException if it is called before start()

Protected Methods

protected void finalize ()

Since: API Level 1

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.