Build 1.0_r1(from source)

android.media
Class MediaRecorder

java.lang.Object
  extended by android.media.MediaRecorder

public class MediaRecorder
extends Object

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 Android Media APIs page for additional help with using MediaRecorder.


Nested Class Summary
 class MediaRecorder.AudioEncoder
          Defines the audio encoding.
 class MediaRecorder.AudioSource
          Defines the audio source.
 class MediaRecorder.OutputFormat
          Defines the output format.
 class MediaRecorder.VideoEncoder
          Defines the video encoding.
 class MediaRecorder.VideoSource
          Defines the video source.
 
Constructor Summary
MediaRecorder()
          Default constructor.
 
Method Summary
protected  void finalize()
          Called by the virtual machine when there are no longer any (non-weak) references to the receiver.
 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 setOutputFile(String path)
          Sets the path of the output file to be produced.
 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.
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

MediaRecorder

public MediaRecorder()
Default constructor.

Method Detail

setPreviewDisplay

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

setAudioSource

public void setAudioSource(int audio_source)
                    throws IllegalStateException
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()
See Also:
MediaRecorder.AudioSource

setVideoSource

public void setVideoSource(int video_source)
                    throws IllegalStateException
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()
See Also:
MediaRecorder.VideoSource

setOutputFormat

public void setOutputFormat(int output_format)
                     throws IllegalStateException
Sets the format of the output file produced during recording. Call this after setAudioSource()/setVideoSource() but before prepare().

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().
See Also:
MediaRecorder.OutputFormat

setVideoSize

public void setVideoSize(int width,
                         int height)
                  throws IllegalStateException
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()

setVideoFrameRate

public void setVideoFrameRate(int rate)
                       throws IllegalStateException
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().

setAudioEncoder

public void setAudioEncoder(int audio_encoder)
                     throws IllegalStateException
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().
See Also:
MediaRecorder.AudioEncoder

setVideoEncoder

public void setVideoEncoder(int video_encoder)
                     throws IllegalStateException
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()
See Also:
MediaRecorder.VideoEncoder

setOutputFile

public void setOutputFile(String path)
                   throws IllegalStateException
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()

prepare

public void prepare()
             throws IllegalStateException
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().

start

public void start()
           throws IllegalStateException
Begins capturing and encoding data to the file specified with setOutputFile(). Call this after prepare().

Throws:
IllegalStateException - if it is called before prepare().

stop

public void stop()
          throws IllegalStateException
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()

reset

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.


getMaxAmplitude

public int getMaxAmplitude()
                    throws IllegalStateException
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.

release

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.


finalize

protected void finalize()
Description copied from class: Object
Called by the virtual machine when there are no longer any (non-weak) references to the receiver. Subclasses can use this facility to guarantee that any associated resources are cleaned up before the receiver is garbage collected. Uncaught exceptions which are thrown during the running of the method cause it to terminate immediately, but are otherwise ignored.

Note: The virtual machine assumes that the implementation in class Object is empty.

Overrides:
finalize in class Object

Build 1.0_r1(from source)

Please submit a feedback, bug or feature