MediaRecorder.java revision ad8f19c6b3167cadc90a35f4d795b07aa2f04ffa
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 /** @hide Stream over a socket, limited to a single stream */ 193 public static final int OUTPUT_FORMAT_RTP_AVP = 7; 194 195 /** @hide H.264/AAC data encapsulated in MPEG2/TS */ 196 public static final int OUTPUT_FORMAT_MPEG2TS = 8; 197 }; 198 199 /** 200 * Defines the audio encoding. These constants are used with 201 * {@link MediaRecorder#setAudioEncoder(int)}. 202 */ 203 public final class AudioEncoder { 204 /* Do not change these values without updating their counterparts 205 * in include/media/mediarecorder.h! 206 */ 207 private AudioEncoder() {} 208 public static final int DEFAULT = 0; 209 /** AMR (Narrowband) audio codec */ 210 public static final int AMR_NB = 1; 211 /** @hide AMR (Wideband) audio codec */ 212 public static final int AMR_WB = 2; 213 /** @hide AAC audio codec */ 214 public static final int AAC = 3; 215 /** @hide enhanced AAC audio codec */ 216 public static final int AAC_PLUS = 4; 217 /** @hide enhanced AAC plus audio codec */ 218 public static final int EAAC_PLUS = 5; 219 } 220 221 /** 222 * Defines the video encoding. These constants are used with 223 * {@link MediaRecorder#setVideoEncoder(int)}. 224 */ 225 public final class VideoEncoder { 226 /* Do not change these values without updating their counterparts 227 * in include/media/mediarecorder.h! 228 */ 229 private VideoEncoder() {} 230 public static final int DEFAULT = 0; 231 public static final int H263 = 1; 232 public static final int H264 = 2; 233 public static final int MPEG_4_SP = 3; 234 } 235 236 /** 237 * Sets the audio source to be used for recording. If this method is not 238 * called, the output file will not contain an audio track. The source needs 239 * to be specified before setting recording-parameters or encoders. Call 240 * this only before setOutputFormat(). 241 * 242 * @param audio_source the audio source to use 243 * @throws IllegalStateException if it is called after setOutputFormat() 244 * @see android.media.MediaRecorder.AudioSource 245 */ 246 public native void setAudioSource(int audio_source) 247 throws IllegalStateException; 248 249 /** 250 * Gets the maximum value for audio sources. 251 * @see android.media.MediaRecorder.AudioSource 252 */ 253 public static final int getAudioSourceMax() { return AudioSource.VOICE_RECOGNITION; } 254 255 /** 256 * Sets the video source to be used for recording. If this method is not 257 * called, the output file will not contain an video track. The source needs 258 * to be specified before setting recording-parameters or encoders. Call 259 * this only before setOutputFormat(). 260 * 261 * @param video_source the video source to use 262 * @throws IllegalStateException if it is called after setOutputFormat() 263 * @see android.media.MediaRecorder.VideoSource 264 */ 265 public native void setVideoSource(int video_source) 266 throws IllegalStateException; 267 268 /** 269 * Uses the settings from a CamcorderProfile object for recording. This method should 270 * be called after the video AND audio sources are set, and before setOutputFile(). 271 * 272 * @param profile the CamcorderProfile to use 273 * @see android.media.CamcorderProfile 274 */ 275 public void setProfile(CamcorderProfile profile) { 276 setOutputFormat(profile.fileFormat); 277 setVideoFrameRate(profile.videoFrameRate); 278 setVideoSize(profile.videoFrameWidth, profile.videoFrameHeight); 279 setVideoEncodingBitRate(profile.videoBitRate); 280 setAudioEncodingBitRate(profile.audioBitRate); 281 setAudioChannels(profile.audioChannels); 282 setAudioSamplingRate(profile.audioSampleRate); 283 setVideoEncoder(profile.videoCodec); 284 setAudioEncoder(profile.audioCodec); 285 } 286 287 /** 288 * Sets the orientation hint for output video playback. 289 * This method should be called before start(). This method will not 290 * trigger the source video frame to rotate during video recording, but to 291 * add a composition matrix containing the rotation angle in the output 292 * video if the output format is OutputFormat.THREE_GPP or 293 * OutputFormat.MPEG_4 so that a video player can choose the proper 294 * orientation for playback. Note that some video players may choose 295 * to ignore the compostion matrix in a video during playback. 296 * 297 * @param degrees the angle to be rotated clockwise in degrees. 298 * The supported angles are 0, 90, 180, and 270 degrees. 299 * @throws IllegalArgumentException if the angle is not supported. 300 * 301 */ 302 public void setOrientationHint(int degrees) { 303 if (degrees != 0 && 304 degrees != 90 && 305 degrees != 180 && 306 degrees != 270) { 307 throw new IllegalArgumentException("Unsupported angle: " + degrees); 308 } 309 setParameter(String.format("video-param-rotation-angle-degrees=%d", degrees)); 310 } 311 312 /** 313 * Sets the format of the output file produced during recording. Call this 314 * after setAudioSource()/setVideoSource() but before prepare(). 315 * 316 * <p>It is recommended to always use 3GP format when using the H.263 317 * video encoder and AMR audio encoder. Using an MPEG-4 container format 318 * may confuse some desktop players.</p> 319 * 320 * @param output_format the output format to use. The output format 321 * needs to be specified before setting recording-parameters or encoders. 322 * @throws IllegalStateException if it is called after prepare() or before 323 * setAudioSource()/setVideoSource(). 324 * @see android.media.MediaRecorder.OutputFormat 325 */ 326 public native void setOutputFormat(int output_format) 327 throws IllegalStateException; 328 329 /** 330 * Sets the width and height of the video to be captured. Must be called 331 * after setVideoSource(). Call this after setOutFormat() but before 332 * prepare(). 333 * 334 * @param width the width of the video to be captured 335 * @param height the height of the video to be captured 336 * @throws IllegalStateException if it is called after 337 * prepare() or before setOutputFormat() 338 */ 339 public native void setVideoSize(int width, int height) 340 throws IllegalStateException; 341 342 /** 343 * Sets the frame rate of the video to be captured. Must be called 344 * after setVideoSource(). Call this after setOutFormat() but before 345 * prepare(). 346 * 347 * @param rate the number of frames per second of video to capture 348 * @throws IllegalStateException if it is called after 349 * prepare() or before setOutputFormat(). 350 * 351 * NOTE: On some devices that have auto-frame rate, this sets the 352 * maximum frame rate, not a constant frame rate. Actual frame rate 353 * will vary according to lighting conditions. 354 */ 355 public native void setVideoFrameRate(int rate) throws IllegalStateException; 356 357 /** 358 * Sets the maximum duration (in ms) of the recording session. 359 * Call this after setOutFormat() but before prepare(). 360 * After recording reaches the specified duration, a notification 361 * will be sent to the {@link android.media.MediaRecorder.OnInfoListener} 362 * with a "what" code of {@link #MEDIA_RECORDER_INFO_MAX_DURATION_REACHED} 363 * and recording will be stopped. Stopping happens asynchronously, there 364 * is no guarantee that the recorder will have stopped by the time the 365 * listener is notified. 366 * 367 * @param max_duration_ms the maximum duration in ms (if zero or negative, disables the duration limit) 368 * 369 */ 370 public native void setMaxDuration(int max_duration_ms) throws IllegalArgumentException; 371 372 /** 373 * Sets the maximum filesize (in bytes) of the recording session. 374 * Call this after setOutFormat() but before prepare(). 375 * After recording reaches the specified filesize, a notification 376 * will be sent to the {@link android.media.MediaRecorder.OnInfoListener} 377 * with a "what" code of {@link #MEDIA_RECORDER_INFO_MAX_FILESIZE_REACHED} 378 * and recording will be stopped. Stopping happens asynchronously, there 379 * is no guarantee that the recorder will have stopped by the time the 380 * listener is notified. 381 * 382 * @param max_filesize_bytes the maximum filesize in bytes (if zero or negative, disables the limit) 383 * 384 */ 385 public native void setMaxFileSize(long max_filesize_bytes) throws IllegalArgumentException; 386 387 /** 388 * Sets the audio encoder to be used for recording. If this method is not 389 * called, the output file will not contain an audio track. Call this after 390 * setOutputFormat() but before prepare(). 391 * 392 * @param audio_encoder the audio encoder to use. 393 * @throws IllegalStateException if it is called before 394 * setOutputFormat() or after prepare(). 395 * @see android.media.MediaRecorder.AudioEncoder 396 */ 397 public native void setAudioEncoder(int audio_encoder) 398 throws IllegalStateException; 399 400 /** 401 * Sets the video encoder to be used for recording. If this method is not 402 * called, the output file will not contain an video track. Call this after 403 * setOutputFormat() and before prepare(). 404 * 405 * @param video_encoder the video encoder to use. 406 * @throws IllegalStateException if it is called before 407 * setOutputFormat() or after prepare() 408 * @see android.media.MediaRecorder.VideoEncoder 409 */ 410 public native void setVideoEncoder(int video_encoder) 411 throws IllegalStateException; 412 413 /** 414 * Sets the audio sampling rate for recording. Call this method before prepare(). 415 * Prepare() may perform additional checks on the parameter to make sure whether 416 * the specified audio sampling rate is applicable. The sampling rate really depends 417 * on the format for the audio recording, as well as the capabilities of the platform. 418 * For instance, the sampling rate supported by AAC audio coding standard ranges 419 * from 8 to 96 kHz. Please consult with the related audio coding standard for the 420 * supported audio sampling rate. 421 * 422 * @param samplingRate the sampling rate for audio in samples per second. 423 */ 424 public void setAudioSamplingRate(int samplingRate) { 425 if (samplingRate <= 0) { 426 throw new IllegalArgumentException("Audio sampling rate is not positive"); 427 } 428 setParameter(String.format("audio-param-sampling-rate=%d", samplingRate)); 429 } 430 431 /** 432 * Sets the number of audio channels for recording. Call this method before prepare(). 433 * Prepare() may perform additional checks on the parameter to make sure whether the 434 * specified number of audio channels are applicable. 435 * 436 * @param numChannels the number of audio channels. Usually it is either 1 (mono) or 2 437 * (stereo). 438 */ 439 public void setAudioChannels(int numChannels) { 440 if (numChannels <= 0) { 441 throw new IllegalArgumentException("Number of channels is not positive"); 442 } 443 setParameter(String.format("audio-param-number-of-channels=%d", numChannels)); 444 } 445 446 /** 447 * Sets the audio encoding bit rate for recording. Call this method before prepare(). 448 * Prepare() may perform additional checks on the parameter to make sure whether the 449 * specified bit rate is applicable, and sometimes the passed bitRate will be clipped 450 * internally to ensure the audio recording can proceed smoothly based on the 451 * capabilities of the platform. 452 * 453 * @param bitRate the audio encoding bit rate in bits per second. 454 */ 455 public void setAudioEncodingBitRate(int bitRate) { 456 if (bitRate <= 0) { 457 throw new IllegalArgumentException("Audio encoding bit rate is not positive"); 458 } 459 setParameter(String.format("audio-param-encoding-bitrate=%d", bitRate)); 460 } 461 462 /** 463 * Sets the video encoding bit rate for recording. Call this method before prepare(). 464 * Prepare() may perform additional checks on the parameter to make sure whether the 465 * specified bit rate is applicable, and sometimes the passed bitRate will be 466 * clipped internally to ensure the video recording can proceed smoothly based on 467 * the capabilities of the platform. 468 * 469 * @param bitRate the video encoding bit rate in bits per second. 470 */ 471 public void setVideoEncodingBitRate(int bitRate) { 472 if (bitRate <= 0) { 473 throw new IllegalArgumentException("Video encoding bit rate is not positive"); 474 } 475 setParameter(String.format("video-param-encoding-bitrate=%d", bitRate)); 476 } 477 478 /** 479 * Pass in the file descriptor of the file to be written. Call this after 480 * setOutputFormat() but before prepare(). 481 * 482 * @param fd an open file descriptor to be written into. 483 * @throws IllegalStateException if it is called before 484 * setOutputFormat() or after prepare() 485 */ 486 public void setOutputFile(FileDescriptor fd) throws IllegalStateException 487 { 488 mPath = null; 489 mFd = fd; 490 } 491 492 /** 493 * Sets the path of the output file to be produced. Call this after 494 * setOutputFormat() but before prepare(). 495 * 496 * @param path The pathname to use. 497 * @throws IllegalStateException if it is called before 498 * setOutputFormat() or after prepare() 499 */ 500 public void setOutputFile(String path) throws IllegalStateException 501 { 502 mFd = null; 503 mPath = path; 504 } 505 506 // native implementation 507 private native void _setOutputFile(FileDescriptor fd, long offset, long length) 508 throws IllegalStateException, IOException; 509 private native void _prepare() throws IllegalStateException, IOException; 510 511 /** 512 * Prepares the recorder to begin capturing and encoding data. This method 513 * must be called after setting up the desired audio and video sources, 514 * encoders, file format, etc., but before start(). 515 * 516 * @throws IllegalStateException if it is called after 517 * start() or before setOutputFormat(). 518 * @throws IOException if prepare fails otherwise. 519 */ 520 public void prepare() throws IllegalStateException, IOException 521 { 522 if (mPath != null) { 523 FileOutputStream fos = new FileOutputStream(mPath); 524 try { 525 _setOutputFile(fos.getFD(), 0, 0); 526 } finally { 527 fos.close(); 528 } 529 } else if (mFd != null) { 530 _setOutputFile(mFd, 0, 0); 531 } else { 532 throw new IOException("No valid output file"); 533 } 534 _prepare(); 535 } 536 537 /** 538 * Begins capturing and encoding data to the file specified with 539 * setOutputFile(). Call this after prepare(). 540 * 541 * @throws IllegalStateException if it is called before 542 * prepare(). 543 */ 544 public native void start() throws IllegalStateException; 545 546 /** 547 * Stops recording. Call this after start(). Once recording is stopped, 548 * you will have to configure it again as if it has just been constructed. 549 * 550 * @throws IllegalStateException if it is called before start() 551 */ 552 public native void stop() throws IllegalStateException; 553 554 /** 555 * Restarts the MediaRecorder to its idle state. After calling 556 * this method, you will have to configure it again as if it had just been 557 * constructed. 558 */ 559 public void reset() { 560 native_reset(); 561 562 // make sure none of the listeners get called anymore 563 mEventHandler.removeCallbacksAndMessages(null); 564 } 565 566 private native void native_reset(); 567 568 /** 569 * Returns the maximum absolute amplitude that was sampled since the last 570 * call to this method. Call this only after the setAudioSource(). 571 * 572 * @return the maximum absolute amplitude measured since the last call, or 573 * 0 when called for the first time 574 * @throws IllegalStateException if it is called before 575 * the audio source has been set. 576 */ 577 public native int getMaxAmplitude() throws IllegalStateException; 578 579 /* Do not change this value without updating its counterpart 580 * in include/media/mediarecorder.h! 581 */ 582 /** Unspecified media recorder error. 583 * @see android.media.MediaRecorder.OnErrorListener 584 */ 585 public static final int MEDIA_RECORDER_ERROR_UNKNOWN = 1; 586 587 /** 588 * Interface definition for a callback to be invoked when an error 589 * occurs while recording. 590 */ 591 public interface OnErrorListener 592 { 593 /** 594 * Called when an error occurs while recording. 595 * 596 * @param mr the MediaRecorder that encountered the error 597 * @param what the type of error that has occurred: 598 * <ul> 599 * <li>{@link #MEDIA_RECORDER_ERROR_UNKNOWN} 600 * </ul> 601 * @param extra an extra code, specific to the error type 602 */ 603 void onError(MediaRecorder mr, int what, int extra); 604 } 605 606 /** 607 * Register a callback to be invoked when an error occurs while 608 * recording. 609 * 610 * @param l the callback that will be run 611 */ 612 public void setOnErrorListener(OnErrorListener l) 613 { 614 mOnErrorListener = l; 615 } 616 617 /* Do not change these values without updating their counterparts 618 * in include/media/mediarecorder.h! 619 */ 620 /** Unspecified media recorder error. 621 * @see android.media.MediaRecorder.OnInfoListener 622 */ 623 public static final int MEDIA_RECORDER_INFO_UNKNOWN = 1; 624 /** A maximum duration had been setup and has now been reached. 625 * @see android.media.MediaRecorder.OnInfoListener 626 */ 627 public static final int MEDIA_RECORDER_INFO_MAX_DURATION_REACHED = 800; 628 /** A maximum filesize had been setup and has now been reached. 629 * @see android.media.MediaRecorder.OnInfoListener 630 */ 631 public static final int MEDIA_RECORDER_INFO_MAX_FILESIZE_REACHED = 801; 632 633 /** 634 * Interface definition for a callback to be invoked when an error 635 * occurs while recording. 636 */ 637 public interface OnInfoListener 638 { 639 /** 640 * Called when an error occurs while recording. 641 * 642 * @param mr the MediaRecorder that encountered the error 643 * @param what the type of error that has occurred: 644 * <ul> 645 * <li>{@link #MEDIA_RECORDER_INFO_UNKNOWN} 646 * <li>{@link #MEDIA_RECORDER_INFO_MAX_DURATION_REACHED} 647 * <li>{@link #MEDIA_RECORDER_INFO_MAX_FILESIZE_REACHED} 648 * </ul> 649 * @param extra an extra code, specific to the error type 650 */ 651 void onInfo(MediaRecorder mr, int what, int extra); 652 } 653 654 /** 655 * Register a callback to be invoked when an informational event occurs while 656 * recording. 657 * 658 * @param listener the callback that will be run 659 */ 660 public void setOnInfoListener(OnInfoListener listener) 661 { 662 mOnInfoListener = listener; 663 } 664 665 private class EventHandler extends Handler 666 { 667 private MediaRecorder mMediaRecorder; 668 669 public EventHandler(MediaRecorder mr, Looper looper) { 670 super(looper); 671 mMediaRecorder = mr; 672 } 673 674 /* Do not change these values without updating their counterparts 675 * in include/media/mediarecorder.h! 676 */ 677 private static final int MEDIA_RECORDER_EVENT_ERROR = 1; 678 private static final int MEDIA_RECORDER_EVENT_INFO = 2; 679 680 @Override 681 public void handleMessage(Message msg) { 682 if (mMediaRecorder.mNativeContext == 0) { 683 Log.w(TAG, "mediarecorder went away with unhandled events"); 684 return; 685 } 686 switch(msg.what) { 687 case MEDIA_RECORDER_EVENT_ERROR: 688 if (mOnErrorListener != null) 689 mOnErrorListener.onError(mMediaRecorder, msg.arg1, msg.arg2); 690 691 return; 692 693 case MEDIA_RECORDER_EVENT_INFO: 694 if (mOnInfoListener != null) 695 mOnInfoListener.onInfo(mMediaRecorder, msg.arg1, msg.arg2); 696 697 return; 698 699 default: 700 Log.e(TAG, "Unknown message type " + msg.what); 701 return; 702 } 703 } 704 } 705 706 /** 707 * Called from native code when an interesting event happens. This method 708 * just uses the EventHandler system to post the event back to the main app thread. 709 * We use a weak reference to the original MediaRecorder object so that the native 710 * code is safe from the object disappearing from underneath it. (This is 711 * the cookie passed to native_setup().) 712 */ 713 private static void postEventFromNative(Object mediarecorder_ref, 714 int what, int arg1, int arg2, Object obj) 715 { 716 MediaRecorder mr = (MediaRecorder)((WeakReference)mediarecorder_ref).get(); 717 if (mr == null) { 718 return; 719 } 720 721 if (mr.mEventHandler != null) { 722 Message m = mr.mEventHandler.obtainMessage(what, arg1, arg2, obj); 723 mr.mEventHandler.sendMessage(m); 724 } 725 } 726 727 /** 728 * Releases resources associated with this MediaRecorder object. 729 * It is good practice to call this method when you're done 730 * using the MediaRecorder. 731 */ 732 public native void release(); 733 734 private static native final void native_init(); 735 736 private native final void native_setup(Object mediarecorder_this) throws IllegalStateException; 737 738 private native final void native_finalize(); 739 740 private native void setParameter(String nameValuePair); 741 742 @Override 743 protected void finalize() { native_finalize(); } 744} 745