FileDocCategorySizeDatePackage
MediaRecorder.javaAPI DocAndroid 1.5 API20929Wed May 06 22:42:00 BST 2009android.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 Audio and Video documentation for additional help with using MediaRecorder.

Fields Summary
private static final String
TAG
private int
mNativeContext
private android.view.Surface
mSurface
private String
mPath
private FileDescriptor
mFd
private EventHandler
mEventHandler
private OnErrorListener
mOnErrorListener
private OnInfoListener
mOnInfoListener
public static final int
MEDIA_RECORDER_ERROR_UNKNOWN
Unspecified media recorder error.
public static final int
MEDIA_RECORDER_INFO_UNKNOWN
Unspecified media recorder error.
public static final int
MEDIA_RECORDER_INFO_MAX_DURATION_REACHED
A maximum duration had been setup and has now been reached.
public static final int
MEDIA_RECORDER_INFO_MAX_FILESIZE_REACHED
A maximum filesize had been setup and has now been reached.
Constructors Summary
public MediaRecorder()
Default constructor.


        Looper looper;
        if ((looper = Looper.myLooper()) != null) {
            mEventHandler = new EventHandler(this, looper);
        } else if ((looper = Looper.getMainLooper()) != null) {
            mEventHandler = new EventHandler(this, looper);
        } else {
            mEventHandler = null;
        }

        /* Native setup requires a weak reference to our object.
         * It's easier to create it here than in C++.
         */
        native_setup(new WeakReference<MediaRecorder>(this));
    
Methods Summary
private native void_prepare()

private native void_setOutputFile(java.io.FileDescriptor fd, long offset, long length)

protected voidfinalize()

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

return
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.

private final native voidnative_finalize()

private native voidnative_reset()

private final native voidnative_setup(java.lang.Object mediarecorder_this)

private static voidpostEventFromNative(java.lang.Object mediarecorder_ref, int what, int arg1, int arg2, java.lang.Object obj)
Called from native code when an interesting event happens. This method just uses the EventHandler system to post the event back to the main app thread. We use a weak reference to the original MediaRecorder object so that the native code is safe from the object disappearing from underneath it. (This is the cookie passed to native_setup().)

        MediaRecorder mr = (MediaRecorder)((WeakReference)mediarecorder_ref).get();
        if (mr == null) {
            return;
        }

        if (mr.mEventHandler != null) {
            Message m = mr.mEventHandler.obtainMessage(what, arg1, arg2, obj);
            mr.mEventHandler.sendMessage(m);
        }
    
public voidprepare()
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().
throws
IOException if prepare fails otherwise.

        if (mPath != null) {
            FileOutputStream fos = new FileOutputStream(mPath);
            try {
                _setOutputFile(fos.getFD(), 0, 0);
            } finally {
                fos.close();
            }
        } else if (mFd != null) {
            _setOutputFile(mFd, 0, 0);
        } else {
            throw new IOException("No valid output file");
        }
        _prepare();
    
public native voidrelease()
Releases resources associated with this MediaRecorder object. It is good practice to call this method when you're done using the MediaRecorder.

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

        native_reset();

        // make sure none of the listeners get called anymore
        mEventHandler.removeCallbacksAndMessages(null);
    
public native voidsetAudioEncoder(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().

param
audio_encoder the audio encoder to use.
throws
IllegalStateException if it is called before setOutputFormat() or after prepare().
see
android.media.MediaRecorder.AudioEncoder

public native voidsetAudioSource(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().

param
audio_source the audio source to use
throws
IllegalStateException if it is called after setOutputFormat()
see
android.media.MediaRecorder.AudioSource

public native voidsetCamera(android.hardware.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().

param
c the Camera to use for recording

public native voidsetMaxDuration(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 {@link android.media.MediaRecorder.OnInfoListener} with a "what" code of {@link #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.

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

public native voidsetMaxFileSize(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 {@link android.media.MediaRecorder.OnInfoListener} with a "what" code of {@link #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.

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

public voidsetOnErrorListener(android.media.MediaRecorder$OnErrorListener l)
Register a callback to be invoked when an error occurs while recording.

param
l the callback that will be run


                       
      
    
                                                             
              
    

                             
       
    
        mOnErrorListener = l;
    
public voidsetOnInfoListener(android.media.MediaRecorder$OnInfoListener listener)
Register a callback to be invoked when an informational event occurs while recording.

param
listener the callback that will be run


                       
      
    
                                                                 
              
    

                              
       
    
        mOnInfoListener = listener;
    
public voidsetOutputFile(java.io.FileDescriptor fd)
Pass in the file descriptor of the file to be written. Call this after setOutputFormat() but before prepare().

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

    

                                                                  
        
             

                                                                  
        
             

                                                                                      
        
             

                                                                
          
             

                                                                                   
          

                                                                                        
          

                                                                                       
          

                                                            
        
             

                                                            
        
             

                                                
         
    
        mPath = null;
        mFd = fd;
    
public voidsetOutputFile(java.lang.String path)
Sets the path of the output file to be produced. Call this after setOutputFormat() but before prepare().

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

        mFd = null;
        mPath = path;
    
public native voidsetOutputFormat(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.

param
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
android.media.MediaRecorder.OutputFormat

public voidsetPreviewDisplay(android.view.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.

param
sv the Surface to use for the preview

        mSurface = sv;
    
public native voidsetVideoEncoder(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().

param
video_encoder the video encoder to use.
throws
IllegalStateException if it is called before setOutputFormat() or after prepare()
see
android.media.MediaRecorder.VideoEncoder

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

param
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 native voidsetVideoSize(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().

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

public native voidsetVideoSource(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().

param
video_source the video source to use
throws
IllegalStateException if it is called after setOutputFormat()
see
android.media.MediaRecorder.VideoSource

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

throws
IllegalStateException if it is called before prepare().

public native voidstop()
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()