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.

Summary

Nested Classes
public class MediaRecorder.AudioEncoder Defines the audio encoding. 
public class MediaRecorder.AudioSource Defines the audio source. 
public class MediaRecorder.OnErrorListener Interface definition for a callback to be invoked when an error occurs while recording. 
public class MediaRecorder.OnInfoListener Interface definition for a callback to be invoked when an error occurs while recording. 
public class MediaRecorder.OutputFormat Defines the output format. 
public class MediaRecorder.VideoEncoder Defines the video encoding. 
public 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
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 setAudioEncoder(int audio_encoder)
Sets the audio encoder to be used 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 setVideoEncoder(int video_encoder)
Sets the video encoder to be used 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

Unspecified media recorder error.

Constant Value: 1 (0x00000001)

public static final int MEDIA_RECORDER_INFO_MAX_DURATION_REACHED

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

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

Constant Value: 801 (0x00000321)

public static final int MEDIA_RECORDER_INFO_UNKNOWN

Unspecified media recorder error.

Constant Value: 1 (0x00000001)

Public Constructors

public MediaRecorder ()

Default constructor.

Public Methods

public int getMaxAmplitude ()

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

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

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

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 setAudioEncoder (int audio_encoder)

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 setAudioSource (int audio_source)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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 setVideoEncoder (int video_encoder)

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 setVideoFrameRate (int rate)

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)

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)

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

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

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

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.