MediaRecorder.java revision d01371bf691ff04e4957e49805503dbf20b338df
1/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.media;
18
19import android.media.CamcorderProfile;
20import android.hardware.Camera;
21import android.os.Handler;
22import android.os.Looper;
23import android.os.Message;
24import android.util.Log;
25import android.view.Surface;
26import java.io.IOException;
27import java.io.FileNotFoundException;
28import java.io.FileOutputStream;
29import java.io.FileDescriptor;
30import java.lang.ref.WeakReference;
31
32/**
33 * Used to record audio and video. The recording control is based on a
34 * simple state machine (see below).
35 *
36 * <p><img src="{@docRoot}images/mediarecorder_state_diagram.gif" border="0" />
37 * </p>
38 *
39 * <p>A common case of using MediaRecorder to record audio works as follows:
40 *
41 * <pre>MediaRecorder recorder = new MediaRecorder();
42 * recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
43 * recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
44 * recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
45 * recorder.setOutputFile(PATH_NAME);
46 * recorder.prepare();
47 * recorder.start();   // Recording is now started
48 * ...
49 * recorder.stop();
50 * recorder.reset();   // You can reuse the object by going back to setAudioSource() step
51 * recorder.release(); // Now the object cannot be reused
52 * </pre>
53 *
54 * <p>See the <a href="{@docRoot}guide/topics/media/index.html">Audio and Video</a>
55 * documentation for additional help with using MediaRecorder.
56 * <p>Note: Currently, MediaRecorder does not work on the emulator.
57 */
58public class MediaRecorder
59{
60    static {
61        System.loadLibrary("media_jni");
62        native_init();
63    }
64    private final static String TAG = "MediaRecorder";
65
66    // The two fields below are accessed by native methods
67    @SuppressWarnings("unused")
68    private int mNativeContext;
69
70    @SuppressWarnings("unused")
71    private Surface mSurface;
72
73    private String mPath;
74    private FileDescriptor mFd;
75    private EventHandler mEventHandler;
76    private OnErrorListener mOnErrorListener;
77    private OnInfoListener mOnInfoListener;
78
79    /**
80     * Default constructor.
81     */
82    public MediaRecorder() {
83
84        Looper looper;
85        if ((looper = Looper.myLooper()) != null) {
86            mEventHandler = new EventHandler(this, looper);
87        } else if ((looper = Looper.getMainLooper()) != null) {
88            mEventHandler = new EventHandler(this, looper);
89        } else {
90            mEventHandler = null;
91        }
92
93        /* Native setup requires a weak reference to our object.
94         * It's easier to create it here than in C++.
95         */
96        native_setup(new WeakReference<MediaRecorder>(this));
97    }
98
99    /**
100     * Sets a Camera to use for recording. Use this function to switch
101     * quickly between preview and capture mode without a teardown of
102     * the camera object. Must call before prepare().
103     *
104     * @param c the Camera to use for recording
105     */
106    public native void setCamera(Camera c);
107
108    /**
109     * Sets a Surface to show a preview of recorded media (video). Calls this
110     * before prepare() to make sure that the desirable preview display is
111     * set.
112     *
113     * @param sv the Surface to use for the preview
114     */
115    public void setPreviewDisplay(Surface sv) {
116        mSurface = sv;
117    }
118
119    /**
120     * Defines the audio source. These constants are used with
121     * {@link MediaRecorder#setAudioSource(int)}.
122     */
123    public final class AudioSource {
124      /* Do not change these values without updating their counterparts
125       * in include/media/mediarecorder.h!
126       */
127        private AudioSource() {}
128        public static final int DEFAULT = 0;
129        /** Microphone audio source */
130        public static final int MIC = 1;
131
132        /** Voice call uplink (Tx) audio source */
133        public static final int VOICE_UPLINK = 2;
134
135        /** Voice call downlink (Rx) audio source */
136        public static final int VOICE_DOWNLINK = 3;
137
138        /** Voice call uplink + downlink audio source */
139        public static final int VOICE_CALL = 4;
140
141        /** Microphone audio source with same orientation as camera if available, the main
142         *  device microphone otherwise */
143        public static final int CAMCORDER = 5;
144
145        /** Microphone audio source tuned for voice recognition if available, behaves like
146         *  {@link #DEFAULT} otherwise. */
147        public static final int VOICE_RECOGNITION = 6;
148    }
149
150    /**
151     * Defines the video source. These constants are used with
152     * {@link MediaRecorder#setVideoSource(int)}.
153     */
154    public final class VideoSource {
155      /* Do not change these values without updating their counterparts
156       * in include/media/mediarecorder.h!
157       */
158        private VideoSource() {}
159        public static final int DEFAULT = 0;
160        /** Camera video source */
161        public static final int CAMERA = 1;
162    }
163
164    /**
165     * Defines the output format. These constants are used with
166     * {@link MediaRecorder#setOutputFormat(int)}.
167     */
168    public final class OutputFormat {
169      /* Do not change these values without updating their counterparts
170       * in include/media/mediarecorder.h!
171       */
172        private OutputFormat() {}
173        public static final int DEFAULT = 0;
174        /** 3GPP media file format*/
175        public static final int THREE_GPP = 1;
176        /** MPEG4 media file format*/
177        public static final int MPEG_4 = 2;
178
179        /** The following formats are audio only .aac or .amr formats **/
180        /** @deprecated  Deprecated in favor of AMR_NB */
181        /** TODO: change link when AMR_NB is exposed. Deprecated in favor of MediaRecorder.OutputFormat.AMR_NB */
182        public static final int RAW_AMR = 3;
183        /** @hide AMR NB file format */
184        public static final int AMR_NB = 3;
185        /** @hide AMR WB file format */
186        public static final int AMR_WB = 4;
187        /** @hide AAC ADIF file format */
188        public static final int AAC_ADIF = 5;
189        /** @hide AAC ADTS file format */
190        public static final int AAC_ADTS = 6;
191    };
192
193    /**
194     * Defines the audio encoding. These constants are used with
195     * {@link MediaRecorder#setAudioEncoder(int)}.
196     */
197    public final class AudioEncoder {
198      /* Do not change these values without updating their counterparts
199       * in include/media/mediarecorder.h!
200       */
201        private AudioEncoder() {}
202        public static final int DEFAULT = 0;
203        /** AMR (Narrowband) audio codec */
204        public static final int AMR_NB = 1;
205        /** @hide AMR (Wideband) audio codec */
206        public static final int AMR_WB = 2;
207        /** @hide AAC audio codec */
208        public static final int AAC = 3;
209        /** @hide enhanced AAC audio codec */
210        public static final int AAC_PLUS = 4;
211        /** @hide enhanced AAC plus audio codec */
212        public static final int EAAC_PLUS = 5;
213    }
214
215    /**
216     * Defines the video encoding. These constants are used with
217     * {@link MediaRecorder#setVideoEncoder(int)}.
218     */
219    public final class VideoEncoder {
220      /* Do not change these values without updating their counterparts
221       * in include/media/mediarecorder.h!
222       */
223        private VideoEncoder() {}
224        public static final int DEFAULT = 0;
225        public static final int H263 = 1;
226        public static final int H264 = 2;
227        public static final int MPEG_4_SP = 3;
228    }
229
230    /**
231     * Sets the audio source to be used for recording. If this method is not
232     * called, the output file will not contain an audio track. The source needs
233     * to be specified before setting recording-parameters or encoders. Call
234     * this only before setOutputFormat().
235     *
236     * @param audio_source the audio source to use
237     * @throws IllegalStateException if it is called after setOutputFormat()
238     * @see android.media.MediaRecorder.AudioSource
239     */
240    public native void setAudioSource(int audio_source)
241            throws IllegalStateException;
242
243    /**
244     * Gets the maximum value for audio sources.
245     * @see android.media.MediaRecorder.AudioSource
246     */
247    public static final int getAudioSourceMax() { return AudioSource.VOICE_RECOGNITION; }
248
249    /**
250     * Sets the video source to be used for recording. If this method is not
251     * called, the output file will not contain an video track. The source needs
252     * to be specified before setting recording-parameters or encoders. Call
253     * this only before setOutputFormat().
254     *
255     * @param video_source the video source to use
256     * @throws IllegalStateException if it is called after setOutputFormat()
257     * @see android.media.MediaRecorder.VideoSource
258     */
259    public native void setVideoSource(int video_source)
260            throws IllegalStateException;
261
262    /**
263     * Uses the settings from a CamcorderProfile object for recording. This method should
264     * be called after the video AND audio sources are set, and before setOutputFile().
265     *
266     * @param profile the CamcorderProfile to use
267     * @see android.media.CamcorderProfile
268     */
269    public void setProfile(CamcorderProfile profile) {
270        setOutputFormat(profile.fileFormat);
271        setVideoFrameRate(profile.videoFrameRate);
272        setVideoSize(profile.videoFrameWidth, profile.videoFrameHeight);
273        setVideoEncodingBitRate(profile.videoBitRate);
274        setAudioEncodingBitRate(profile.audioBitRate);
275        setAudioChannels(profile.audioChannels);
276        setAudioSamplingRate(profile.audioSampleRate);
277        setVideoEncoder(profile.videoCodec);
278        setAudioEncoder(profile.audioCodec);
279    }
280
281    /**
282     * Enables/Disables time lapse capture and sets its parameters. This method should
283     * be called after setProfile().
284     *
285     * @param enableTimeLapse Pass true to enable time lapse capture, false to disable it.
286     * @param timeBetweenTimeLapseFrameCaptureMs time between two captures of time lapse frames.
287     * @param encoderLevel the video encoder level.
288     */
289    public void setTimeLapseParameters(boolean enableTimeLapse,
290            int timeBetweenTimeLapseFrameCaptureMs, int encoderLevel) {
291        setParameter(String.format("time-lapse-enable=%d",
292                    (enableTimeLapse) ? 1 : 0));
293        setParameter(String.format("time-between-time-lapse-frame-capture=%d",
294                    timeBetweenTimeLapseFrameCaptureMs));
295        setVideoEncoderLevel(encoderLevel);
296    }
297
298    /**
299     * Sets the format of the output file produced during recording. Call this
300     * after setAudioSource()/setVideoSource() but before prepare().
301     *
302     * <p>It is recommended to always use 3GP format when using the H.263
303     * video encoder and AMR audio encoder. Using an MPEG-4 container format
304     * may confuse some desktop players.</p>
305     *
306     * @param output_format the output format to use. The output format
307     * needs to be specified before setting recording-parameters or encoders.
308     * @throws IllegalStateException if it is called after prepare() or before
309     * setAudioSource()/setVideoSource().
310     * @see android.media.MediaRecorder.OutputFormat
311     */
312    public native void setOutputFormat(int output_format)
313            throws IllegalStateException;
314
315    /**
316     * Sets the width and height of the video to be captured.  Must be called
317     * after setVideoSource(). Call this after setOutFormat() but before
318     * prepare().
319     *
320     * @param width the width of the video to be captured
321     * @param height the height of the video to be captured
322     * @throws IllegalStateException if it is called after
323     * prepare() or before setOutputFormat()
324     */
325    public native void setVideoSize(int width, int height)
326            throws IllegalStateException;
327
328    /**
329     * Sets the frame rate of the video to be captured.  Must be called
330     * after setVideoSource(). Call this after setOutFormat() but before
331     * prepare().
332     *
333     * @param rate the number of frames per second of video to capture
334     * @throws IllegalStateException if it is called after
335     * prepare() or before setOutputFormat().
336     *
337     * NOTE: On some devices that have auto-frame rate, this sets the
338     * maximum frame rate, not a constant frame rate. Actual frame rate
339     * will vary according to lighting conditions.
340     */
341    public native void setVideoFrameRate(int rate) throws IllegalStateException;
342
343    /**
344     * Sets the maximum duration (in ms) of the recording session.
345     * Call this after setOutFormat() but before prepare().
346     * After recording reaches the specified duration, a notification
347     * will be sent to the {@link android.media.MediaRecorder.OnInfoListener}
348     * with a "what" code of {@link #MEDIA_RECORDER_INFO_MAX_DURATION_REACHED}
349     * and recording will be stopped. Stopping happens asynchronously, there
350     * is no guarantee that the recorder will have stopped by the time the
351     * listener is notified.
352     *
353     * @param max_duration_ms the maximum duration in ms (if zero or negative, disables the duration limit)
354     *
355     */
356    public native void setMaxDuration(int max_duration_ms) throws IllegalArgumentException;
357
358    /**
359     * Sets the maximum filesize (in bytes) of the recording session.
360     * Call this after setOutFormat() but before prepare().
361     * After recording reaches the specified filesize, a notification
362     * will be sent to the {@link android.media.MediaRecorder.OnInfoListener}
363     * with a "what" code of {@link #MEDIA_RECORDER_INFO_MAX_FILESIZE_REACHED}
364     * and recording will be stopped. Stopping happens asynchronously, there
365     * is no guarantee that the recorder will have stopped by the time the
366     * listener is notified.
367     *
368     * @param max_filesize_bytes the maximum filesize in bytes (if zero or negative, disables the limit)
369     *
370     */
371    public native void setMaxFileSize(long max_filesize_bytes) throws IllegalArgumentException;
372
373    /**
374     * Sets the audio encoder to be used for recording. If this method is not
375     * called, the output file will not contain an audio track. Call this after
376     * setOutputFormat() but before prepare().
377     *
378     * @param audio_encoder the audio encoder to use.
379     * @throws IllegalStateException if it is called before
380     * setOutputFormat() or after prepare().
381     * @see android.media.MediaRecorder.AudioEncoder
382     */
383    public native void setAudioEncoder(int audio_encoder)
384            throws IllegalStateException;
385
386    /**
387     * Sets the video encoder to be used for recording. If this method is not
388     * called, the output file will not contain an video track. Call this after
389     * setOutputFormat() and before prepare().
390     *
391     * @param video_encoder the video encoder to use.
392     * @throws IllegalStateException if it is called before
393     * setOutputFormat() or after prepare()
394     * @see android.media.MediaRecorder.VideoEncoder
395     */
396    public native void setVideoEncoder(int video_encoder)
397            throws IllegalStateException;
398
399    /**
400     * Sets the audio sampling rate for recording. Call this method before prepare().
401     * Prepare() may perform additional checks on the parameter to make sure whether
402     * the specified audio sampling rate is applicable. The sampling rate really depends
403     * on the format for the audio recording, as well as the capabilities of the platform.
404     * For instance, the sampling rate supported by AAC audio coding standard ranges
405     * from 8 to 96 kHz. Please consult with the related audio coding standard for the
406     * supported audio sampling rate.
407     *
408     * @param samplingRate the sampling rate for audio in samples per second.
409     */
410    public void setAudioSamplingRate(int samplingRate) {
411        if (samplingRate <= 0) {
412            throw new IllegalArgumentException("Audio sampling rate is not positive");
413        }
414        setParameter(String.format("audio-param-sampling-rate=%d", samplingRate));
415    }
416
417    /**
418     * Sets the number of audio channels for recording. Call this method before prepare().
419     * Prepare() may perform additional checks on the parameter to make sure whether the
420     * specified number of audio channels are applicable.
421     *
422     * @param numChannels the number of audio channels. Usually it is either 1 (mono) or 2
423     * (stereo).
424     */
425    public void setAudioChannels(int numChannels) {
426        if (numChannels <= 0) {
427            throw new IllegalArgumentException("Number of channels is not positive");
428        }
429        setParameter(String.format("audio-param-number-of-channels=%d", numChannels));
430    }
431
432    /**
433     * Sets the audio encoding bit rate for recording. Call this method before prepare().
434     * Prepare() may perform additional checks on the parameter to make sure whether the
435     * specified bit rate is applicable, and sometimes the passed bitRate will be clipped
436     * internally to ensure the audio recording can proceed smoothly based on the
437     * capabilities of the platform.
438     *
439     * @param bitRate the audio encoding bit rate in bits per second.
440     */
441    public void setAudioEncodingBitRate(int bitRate) {
442        if (bitRate <= 0) {
443            throw new IllegalArgumentException("Audio encoding bit rate is not positive");
444        }
445        setParameter(String.format("audio-param-encoding-bitrate=%d", bitRate));
446    }
447
448    /**
449     * Sets the video encoding bit rate for recording. Call this method before prepare().
450     * Prepare() may perform additional checks on the parameter to make sure whether the
451     * specified bit rate is applicable, and sometimes the passed bitRate will be
452     * clipped internally to ensure the video recording can proceed smoothly based on
453     * the capabilities of the platform.
454     *
455     * @param bitRate the video encoding bit rate in bits per second.
456     */
457    public void setVideoEncodingBitRate(int bitRate) {
458        if (bitRate <= 0) {
459            throw new IllegalArgumentException("Video encoding bit rate is not positive");
460        }
461        setParameter(String.format("video-param-encoding-bitrate=%d", bitRate));
462    }
463
464    /**
465     * Sets the level of the encoder. Call this before prepare().
466     *
467     * @param encoderLevel the video encoder level.
468     */
469    public void setVideoEncoderLevel(int encoderLevel) {
470        setParameter(String.format("video-param-encoder-level=%d", encoderLevel));
471    }
472
473    /**
474     * Pass in the file descriptor of the file to be written. Call this after
475     * setOutputFormat() but before prepare().
476     *
477     * @param fd an open file descriptor to be written into.
478     * @throws IllegalStateException if it is called before
479     * setOutputFormat() or after prepare()
480     */
481    public void setOutputFile(FileDescriptor fd) throws IllegalStateException
482    {
483        mPath = null;
484        mFd = fd;
485    }
486
487    /**
488     * Sets the path of the output file to be produced. Call this after
489     * setOutputFormat() but before prepare().
490     *
491     * @param path The pathname to use.
492     * @throws IllegalStateException if it is called before
493     * setOutputFormat() or after prepare()
494     */
495    public void setOutputFile(String path) throws IllegalStateException
496    {
497        mFd = null;
498        mPath = path;
499    }
500
501    // native implementation
502    private native void _setOutputFile(FileDescriptor fd, long offset, long length)
503        throws IllegalStateException, IOException;
504    private native void _prepare() throws IllegalStateException, IOException;
505
506    /**
507     * Prepares the recorder to begin capturing and encoding data. This method
508     * must be called after setting up the desired audio and video sources,
509     * encoders, file format, etc., but before start().
510     *
511     * @throws IllegalStateException if it is called after
512     * start() or before setOutputFormat().
513     * @throws IOException if prepare fails otherwise.
514     */
515    public void prepare() throws IllegalStateException, IOException
516    {
517        if (mPath != null) {
518            FileOutputStream fos = new FileOutputStream(mPath);
519            try {
520                _setOutputFile(fos.getFD(), 0, 0);
521            } finally {
522                fos.close();
523            }
524        } else if (mFd != null) {
525            _setOutputFile(mFd, 0, 0);
526        } else {
527            throw new IOException("No valid output file");
528        }
529        _prepare();
530    }
531
532    /**
533     * Begins capturing and encoding data to the file specified with
534     * setOutputFile(). Call this after prepare().
535     *
536     * @throws IllegalStateException if it is called before
537     * prepare().
538     */
539    public native void start() throws IllegalStateException;
540
541    /**
542     * Stops recording. Call this after start(). Once recording is stopped,
543     * you will have to configure it again as if it has just been constructed.
544     *
545     * @throws IllegalStateException if it is called before start()
546     */
547    public native void stop() throws IllegalStateException;
548
549    /**
550     * Restarts the MediaRecorder to its idle state. After calling
551     * this method, you will have to configure it again as if it had just been
552     * constructed.
553     */
554    public void reset() {
555        native_reset();
556
557        // make sure none of the listeners get called anymore
558        mEventHandler.removeCallbacksAndMessages(null);
559    }
560
561    private native void native_reset();
562
563    /**
564     * Returns the maximum absolute amplitude that was sampled since the last
565     * call to this method. Call this only after the setAudioSource().
566     *
567     * @return the maximum absolute amplitude measured since the last call, or
568     * 0 when called for the first time
569     * @throws IllegalStateException if it is called before
570     * the audio source has been set.
571     */
572    public native int getMaxAmplitude() throws IllegalStateException;
573
574    /* Do not change this value without updating its counterpart
575     * in include/media/mediarecorder.h!
576     */
577    /** Unspecified media recorder error.
578     * @see android.media.MediaRecorder.OnErrorListener
579     */
580    public static final int MEDIA_RECORDER_ERROR_UNKNOWN = 1;
581
582    /**
583     * Interface definition for a callback to be invoked when an error
584     * occurs while recording.
585     */
586    public interface OnErrorListener
587    {
588        /**
589         * Called when an error occurs while recording.
590         *
591         * @param mr the MediaRecorder that encountered the error
592         * @param what    the type of error that has occurred:
593         * <ul>
594         * <li>{@link #MEDIA_RECORDER_ERROR_UNKNOWN}
595         * </ul>
596         * @param extra   an extra code, specific to the error type
597         */
598        void onError(MediaRecorder mr, int what, int extra);
599    }
600
601    /**
602     * Register a callback to be invoked when an error occurs while
603     * recording.
604     *
605     * @param l the callback that will be run
606     */
607    public void setOnErrorListener(OnErrorListener l)
608    {
609        mOnErrorListener = l;
610    }
611
612    /* Do not change these values without updating their counterparts
613     * in include/media/mediarecorder.h!
614     */
615    /** Unspecified media recorder error.
616     * @see android.media.MediaRecorder.OnInfoListener
617     */
618    public static final int MEDIA_RECORDER_INFO_UNKNOWN              = 1;
619    /** A maximum duration had been setup and has now been reached.
620     * @see android.media.MediaRecorder.OnInfoListener
621     */
622    public static final int MEDIA_RECORDER_INFO_MAX_DURATION_REACHED = 800;
623    /** A maximum filesize had been setup and has now been reached.
624     * @see android.media.MediaRecorder.OnInfoListener
625     */
626    public static final int MEDIA_RECORDER_INFO_MAX_FILESIZE_REACHED = 801;
627
628    /**
629     * Interface definition for a callback to be invoked when an error
630     * occurs while recording.
631     */
632    public interface OnInfoListener
633    {
634        /**
635         * Called when an error occurs while recording.
636         *
637         * @param mr the MediaRecorder that encountered the error
638         * @param what    the type of error that has occurred:
639         * <ul>
640         * <li>{@link #MEDIA_RECORDER_INFO_UNKNOWN}
641         * <li>{@link #MEDIA_RECORDER_INFO_MAX_DURATION_REACHED}
642         * <li>{@link #MEDIA_RECORDER_INFO_MAX_FILESIZE_REACHED}
643         * </ul>
644         * @param extra   an extra code, specific to the error type
645         */
646        void onInfo(MediaRecorder mr, int what, int extra);
647    }
648
649    /**
650     * Register a callback to be invoked when an informational event occurs while
651     * recording.
652     *
653     * @param listener the callback that will be run
654     */
655    public void setOnInfoListener(OnInfoListener listener)
656    {
657        mOnInfoListener = listener;
658    }
659
660    private class EventHandler extends Handler
661    {
662        private MediaRecorder mMediaRecorder;
663
664        public EventHandler(MediaRecorder mr, Looper looper) {
665            super(looper);
666            mMediaRecorder = mr;
667        }
668
669        /* Do not change these values without updating their counterparts
670         * in include/media/mediarecorder.h!
671         */
672        private static final int MEDIA_RECORDER_EVENT_ERROR = 1;
673        private static final int MEDIA_RECORDER_EVENT_INFO  = 2;
674
675        @Override
676        public void handleMessage(Message msg) {
677            if (mMediaRecorder.mNativeContext == 0) {
678                Log.w(TAG, "mediarecorder went away with unhandled events");
679                return;
680            }
681            switch(msg.what) {
682            case MEDIA_RECORDER_EVENT_ERROR:
683                if (mOnErrorListener != null)
684                    mOnErrorListener.onError(mMediaRecorder, msg.arg1, msg.arg2);
685
686                return;
687
688            case MEDIA_RECORDER_EVENT_INFO:
689                if (mOnInfoListener != null)
690                    mOnInfoListener.onInfo(mMediaRecorder, msg.arg1, msg.arg2);
691
692                return;
693
694            default:
695                Log.e(TAG, "Unknown message type " + msg.what);
696                return;
697            }
698        }
699    }
700
701    /**
702     * Called from native code when an interesting event happens.  This method
703     * just uses the EventHandler system to post the event back to the main app thread.
704     * We use a weak reference to the original MediaRecorder object so that the native
705     * code is safe from the object disappearing from underneath it.  (This is
706     * the cookie passed to native_setup().)
707     */
708    private static void postEventFromNative(Object mediarecorder_ref,
709                                            int what, int arg1, int arg2, Object obj)
710    {
711        MediaRecorder mr = (MediaRecorder)((WeakReference)mediarecorder_ref).get();
712        if (mr == null) {
713            return;
714        }
715
716        if (mr.mEventHandler != null) {
717            Message m = mr.mEventHandler.obtainMessage(what, arg1, arg2, obj);
718            mr.mEventHandler.sendMessage(m);
719        }
720    }
721
722    /**
723     * Releases resources associated with this MediaRecorder object.
724     * It is good practice to call this method when you're done
725     * using the MediaRecorder.
726     */
727    public native void release();
728
729    private static native final void native_init();
730
731    private native final void native_setup(Object mediarecorder_this) throws IllegalStateException;
732
733    private native final void native_finalize();
734
735    private native void setParameter(String nameValuePair);
736
737    @Override
738    protected void finalize() { native_finalize(); }
739}
740