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