MediaRecorder.java revision 10353e36018224b86ef2918d1d68107459d445cf
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.annotation.NonNull; 20import android.annotation.SystemApi; 21import android.app.ActivityThread; 22import android.hardware.Camera; 23import android.os.Bundle; 24import android.os.Handler; 25import android.os.Looper; 26import android.os.Message; 27import android.os.PersistableBundle; 28import android.util.Log; 29import android.view.Surface; 30 31import java.io.FileDescriptor; 32import java.io.IOException; 33import java.io.RandomAccessFile; 34import java.lang.ref.WeakReference; 35 36/** 37 * Used to record audio and video. The recording control is based on a 38 * simple state machine (see below). 39 * 40 * <p><img src="{@docRoot}images/mediarecorder_state_diagram.gif" border="0" /> 41 * </p> 42 * 43 * <p>A common case of using MediaRecorder to record audio works as follows: 44 * 45 * <pre>MediaRecorder recorder = new MediaRecorder(); 46 * recorder.setAudioSource(MediaRecorder.AudioSource.MIC); 47 * recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); 48 * recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); 49 * recorder.setOutputFile(PATH_NAME); 50 * recorder.prepare(); 51 * recorder.start(); // Recording is now started 52 * ... 53 * recorder.stop(); 54 * recorder.reset(); // You can reuse the object by going back to setAudioSource() step 55 * recorder.release(); // Now the object cannot be reused 56 * </pre> 57 * 58 * <p>Applications may want to register for informational and error 59 * events in order to be informed of some internal update and possible 60 * runtime errors during recording. Registration for such events is 61 * done by setting the appropriate listeners (via calls 62 * (to {@link #setOnInfoListener(OnInfoListener)}setOnInfoListener and/or 63 * {@link #setOnErrorListener(OnErrorListener)}setOnErrorListener). 64 * In order to receive the respective callback associated with these listeners, 65 * applications are required to create MediaRecorder objects on threads with a 66 * Looper running (the main UI thread by default already has a Looper running). 67 * 68 * <p><strong>Note:</strong> Currently, MediaRecorder does not work on the emulator. 69 * 70 * <div class="special reference"> 71 * <h3>Developer Guides</h3> 72 * <p>For more information about how to use MediaRecorder for recording video, read the 73 * <a href="{@docRoot}guide/topics/media/camera.html#capture-video">Camera</a> developer guide. 74 * For more information about how to use MediaRecorder for recording sound, read the 75 * <a href="{@docRoot}guide/topics/media/audio-capture.html">Audio Capture</a> developer guide.</p> 76 * </div> 77 */ 78public class MediaRecorder 79{ 80 static { 81 System.loadLibrary("media_jni"); 82 native_init(); 83 } 84 private final static String TAG = "MediaRecorder"; 85 86 // The two fields below are accessed by native methods 87 @SuppressWarnings("unused") 88 private long mNativeContext; 89 90 @SuppressWarnings("unused") 91 private Surface mSurface; 92 93 private String mPath; 94 private FileDescriptor mFd; 95 private EventHandler mEventHandler; 96 private OnErrorListener mOnErrorListener; 97 private OnInfoListener mOnInfoListener; 98 99 /** 100 * Default constructor. 101 */ 102 public MediaRecorder() { 103 104 Looper looper; 105 if ((looper = Looper.myLooper()) != null) { 106 mEventHandler = new EventHandler(this, looper); 107 } else if ((looper = Looper.getMainLooper()) != null) { 108 mEventHandler = new EventHandler(this, looper); 109 } else { 110 mEventHandler = null; 111 } 112 113 String packageName = ActivityThread.currentPackageName(); 114 /* Native setup requires a weak reference to our object. 115 * It's easier to create it here than in C++. 116 */ 117 native_setup(new WeakReference<MediaRecorder>(this), packageName, 118 ActivityThread.currentOpPackageName()); 119 } 120 121 /** 122 * Sets a {@link android.hardware.Camera} to use for recording. 123 * 124 * <p>Use this function to switch quickly between preview and capture mode without a teardown of 125 * the camera object. {@link android.hardware.Camera#unlock()} should be called before 126 * this. Must call before {@link #prepare}.</p> 127 * 128 * @param c the Camera to use for recording 129 * @deprecated Use {@link #getSurface} and the {@link android.hardware.camera2} API instead. 130 */ 131 @Deprecated 132 public native void setCamera(Camera c); 133 134 /** 135 * Gets the surface to record from when using SURFACE video source. 136 * 137 * <p> May only be called after {@link #prepare}. Frames rendered to the Surface before 138 * {@link #start} will be discarded.</p> 139 * 140 * @throws IllegalStateException if it is called before {@link #prepare}, after 141 * {@link #stop}, or is called when VideoSource is not set to SURFACE. 142 * @see android.media.MediaRecorder.VideoSource 143 */ 144 public native Surface getSurface(); 145 146 /** 147 * Configures the recorder to use a persistent surface when using SURFACE video source. 148 * <p> May only be called before {@link #prepare}. If called, {@link #getSurface} should 149 * not be used and will throw IllegalStateException. Frames rendered to the Surface 150 * before {@link #start} will be discarded.</p> 151 152 * @param surface a persistent input surface created by 153 * {@link MediaCodec#createPersistentInputSurface} 154 * @throws IllegalStateException if it is called after {@link #prepare} and before 155 * {@link #stop}. 156 * @throws IllegalArgumentException if the surface was not created by 157 * {@link MediaCodec#createPersistentInputSurface}. 158 * @see MediaCodec#createPersistentInputSurface 159 * @see MediaRecorder.VideoSource 160 */ 161 public void setInputSurface(@NonNull Surface surface) { 162 if (!(surface instanceof MediaCodec.PersistentSurface)) { 163 throw new IllegalArgumentException("not a PersistentSurface"); 164 } 165 native_setInputSurface(surface); 166 } 167 168 private native final void native_setInputSurface(@NonNull Surface surface); 169 170 /** 171 * Sets a Surface to show a preview of recorded media (video). Calls this 172 * before prepare() to make sure that the desirable preview display is 173 * set. If {@link #setCamera(Camera)} is used and the surface has been 174 * already set to the camera, application do not need to call this. If 175 * this is called with non-null surface, the preview surface of the camera 176 * will be replaced by the new surface. If this method is called with null 177 * surface or not called at all, media recorder will not change the preview 178 * surface of the camera. 179 * 180 * @param sv the Surface to use for the preview 181 * @see android.hardware.Camera#setPreviewDisplay(android.view.SurfaceHolder) 182 */ 183 public void setPreviewDisplay(Surface sv) { 184 mSurface = sv; 185 } 186 187 /** 188 * Defines the audio source. 189 * An audio source defines both a default physical source of audio signal, and a recording 190 * configuration. These constants are for instance used 191 * in {@link MediaRecorder#setAudioSource(int)} or 192 * {@link AudioRecord.Builder#setAudioSource(int)}. 193 */ 194 public final class AudioSource { 195 196 private AudioSource() {} 197 198 /** @hide */ 199 public final static int AUDIO_SOURCE_INVALID = -1; 200 201 /* Do not change these values without updating their counterparts 202 * in system/media/audio/include/system/audio.h! 203 */ 204 205 /** Default audio source **/ 206 public static final int DEFAULT = 0; 207 208 /** Microphone audio source */ 209 public static final int MIC = 1; 210 211 /** Voice call uplink (Tx) audio source. 212 * <p> 213 * Capturing from <code>VOICE_UPLINK</code> source requires the 214 * {@link android.Manifest.permission#CAPTURE_AUDIO_OUTPUT} permission. 215 * This permission is reserved for use by system components and is not available to 216 * third-party applications. 217 * </p> 218 */ 219 public static final int VOICE_UPLINK = 2; 220 221 /** Voice call downlink (Rx) audio source. 222 * <p> 223 * Capturing from <code>VOICE_DOWNLINK</code> source requires the 224 * {@link android.Manifest.permission#CAPTURE_AUDIO_OUTPUT} permission. 225 * This permission is reserved for use by system components and is not available to 226 * third-party applications. 227 * </p> 228 */ 229 public static final int VOICE_DOWNLINK = 3; 230 231 /** Voice call uplink + downlink audio source 232 * <p> 233 * Capturing from <code>VOICE_CALL</code> source requires the 234 * {@link android.Manifest.permission#CAPTURE_AUDIO_OUTPUT} permission. 235 * This permission is reserved for use by system components and is not available to 236 * third-party applications. 237 * </p> 238 */ 239 public static final int VOICE_CALL = 4; 240 241 /** Microphone audio source tuned for video recording, with the same orientation 242 * as the camera if available. */ 243 public static final int CAMCORDER = 5; 244 245 /** Microphone audio source tuned for voice recognition. */ 246 public static final int VOICE_RECOGNITION = 6; 247 248 /** Microphone audio source tuned for voice communications such as VoIP. It 249 * will for instance take advantage of echo cancellation or automatic gain control 250 * if available. 251 */ 252 public static final int VOICE_COMMUNICATION = 7; 253 254 /** 255 * Audio source for a submix of audio streams to be presented remotely. 256 * <p> 257 * An application can use this audio source to capture a mix of audio streams 258 * that should be transmitted to a remote receiver such as a Wifi display. 259 * While recording is active, these audio streams are redirected to the remote 260 * submix instead of being played on the device speaker or headset. 261 * </p><p> 262 * Certain streams are excluded from the remote submix, including 263 * {@link AudioManager#STREAM_RING}, {@link AudioManager#STREAM_ALARM}, 264 * and {@link AudioManager#STREAM_NOTIFICATION}. These streams will continue 265 * to be presented locally as usual. 266 * </p><p> 267 * Capturing the remote submix audio requires the 268 * {@link android.Manifest.permission#CAPTURE_AUDIO_OUTPUT} permission. 269 * This permission is reserved for use by system components and is not available to 270 * third-party applications. 271 * </p> 272 */ 273 public static final int REMOTE_SUBMIX = 8; 274 275 /** Microphone audio source tuned for unprocessed (raw) sound if available, behaves like 276 * {@link #DEFAULT} otherwise. */ 277 public static final int UNPROCESSED = 9; 278 279 /** 280 * Audio source for capturing broadcast radio tuner output. 281 * @hide 282 */ 283 @SystemApi 284 public static final int RADIO_TUNER = 1998; 285 286 /** 287 * Audio source for preemptible, low-priority software hotword detection 288 * It presents the same gain and pre processing tuning as {@link #VOICE_RECOGNITION}. 289 * <p> 290 * An application should use this audio source when it wishes to do 291 * always-on software hotword detection, while gracefully giving in to any other application 292 * that might want to read from the microphone. 293 * </p> 294 * This is a hidden audio source. 295 * @hide 296 */ 297 @SystemApi 298 public static final int HOTWORD = 1999; 299 } 300 301 // TODO make AudioSource static (API change) and move this method inside the AudioSource class 302 /** 303 * @hide 304 * @param source An audio source to test 305 * @return true if the source is only visible to system components 306 */ 307 public static boolean isSystemOnlyAudioSource(int source) { 308 switch(source) { 309 case AudioSource.DEFAULT: 310 case AudioSource.MIC: 311 case AudioSource.VOICE_UPLINK: 312 case AudioSource.VOICE_DOWNLINK: 313 case AudioSource.VOICE_CALL: 314 case AudioSource.CAMCORDER: 315 case AudioSource.VOICE_RECOGNITION: 316 case AudioSource.VOICE_COMMUNICATION: 317 //case REMOTE_SUBMIX: considered "system" as it requires system permissions 318 case AudioSource.UNPROCESSED: 319 return false; 320 default: 321 return true; 322 } 323 } 324 325 /** 326 * Defines the video source. These constants are used with 327 * {@link MediaRecorder#setVideoSource(int)}. 328 */ 329 public final class VideoSource { 330 /* Do not change these values without updating their counterparts 331 * in include/media/mediarecorder.h! 332 */ 333 private VideoSource() {} 334 public static final int DEFAULT = 0; 335 /** Camera video source 336 * <p> 337 * Using the {@link android.hardware.Camera} API as video source. 338 * </p> 339 */ 340 public static final int CAMERA = 1; 341 /** Surface video source 342 * <p> 343 * Using a Surface as video source. 344 * </p><p> 345 * This flag must be used when recording from an 346 * {@link android.hardware.camera2} API source. 347 * </p><p> 348 * When using this video source type, use {@link MediaRecorder#getSurface()} 349 * to retrieve the surface created by MediaRecorder. 350 */ 351 public static final int SURFACE = 2; 352 } 353 354 /** 355 * Defines the output format. These constants are used with 356 * {@link MediaRecorder#setOutputFormat(int)}. 357 */ 358 public final class OutputFormat { 359 /* Do not change these values without updating their counterparts 360 * in include/media/mediarecorder.h! 361 */ 362 private OutputFormat() {} 363 public static final int DEFAULT = 0; 364 /** 3GPP media file format*/ 365 public static final int THREE_GPP = 1; 366 /** MPEG4 media file format*/ 367 public static final int MPEG_4 = 2; 368 369 /** The following formats are audio only .aac or .amr formats */ 370 371 /** 372 * AMR NB file format 373 * @deprecated Deprecated in favor of MediaRecorder.OutputFormat.AMR_NB 374 */ 375 public static final int RAW_AMR = 3; 376 377 /** AMR NB file format */ 378 public static final int AMR_NB = 3; 379 380 /** AMR WB file format */ 381 public static final int AMR_WB = 4; 382 383 /** @hide AAC ADIF file format */ 384 public static final int AAC_ADIF = 5; 385 386 /** AAC ADTS file format */ 387 public static final int AAC_ADTS = 6; 388 389 /** @hide Stream over a socket, limited to a single stream */ 390 public static final int OUTPUT_FORMAT_RTP_AVP = 7; 391 392 /** H.264/AAC data encapsulated in MPEG2/TS */ 393 public static final int MPEG_2_TS = 8; 394 395 /** VP8/VORBIS data in a WEBM container */ 396 public static final int WEBM = 9; 397 }; 398 399 /** 400 * Defines the audio encoding. These constants are used with 401 * {@link MediaRecorder#setAudioEncoder(int)}. 402 */ 403 public final class AudioEncoder { 404 /* Do not change these values without updating their counterparts 405 * in include/media/mediarecorder.h! 406 */ 407 private AudioEncoder() {} 408 public static final int DEFAULT = 0; 409 /** AMR (Narrowband) audio codec */ 410 public static final int AMR_NB = 1; 411 /** AMR (Wideband) audio codec */ 412 public static final int AMR_WB = 2; 413 /** AAC Low Complexity (AAC-LC) audio codec */ 414 public static final int AAC = 3; 415 /** High Efficiency AAC (HE-AAC) audio codec */ 416 public static final int HE_AAC = 4; 417 /** Enhanced Low Delay AAC (AAC-ELD) audio codec */ 418 public static final int AAC_ELD = 5; 419 /** Ogg Vorbis audio codec */ 420 public static final int VORBIS = 6; 421 } 422 423 /** 424 * Defines the video encoding. These constants are used with 425 * {@link MediaRecorder#setVideoEncoder(int)}. 426 */ 427 public final class VideoEncoder { 428 /* Do not change these values without updating their counterparts 429 * in include/media/mediarecorder.h! 430 */ 431 private VideoEncoder() {} 432 public static final int DEFAULT = 0; 433 public static final int H263 = 1; 434 public static final int H264 = 2; 435 public static final int MPEG_4_SP = 3; 436 public static final int VP8 = 4; 437 public static final int HEVC = 5; 438 } 439 440 /** 441 * Sets the audio source to be used for recording. If this method is not 442 * called, the output file will not contain an audio track. The source needs 443 * to be specified before setting recording-parameters or encoders. Call 444 * this only before setOutputFormat(). 445 * 446 * @param audio_source the audio source to use 447 * @throws IllegalStateException if it is called after setOutputFormat() 448 * @see android.media.MediaRecorder.AudioSource 449 */ 450 public native void setAudioSource(int audio_source) 451 throws IllegalStateException; 452 453 /** 454 * Gets the maximum value for audio sources. 455 * @see android.media.MediaRecorder.AudioSource 456 */ 457 public static final int getAudioSourceMax() { 458 return AudioSource.UNPROCESSED; 459 } 460 461 /** 462 * Sets the video source to be used for recording. If this method is not 463 * called, the output file will not contain an video track. The source needs 464 * to be specified before setting recording-parameters or encoders. Call 465 * this only before setOutputFormat(). 466 * 467 * @param video_source the video source to use 468 * @throws IllegalStateException if it is called after setOutputFormat() 469 * @see android.media.MediaRecorder.VideoSource 470 */ 471 public native void setVideoSource(int video_source) 472 throws IllegalStateException; 473 474 /** 475 * Uses the settings from a CamcorderProfile object for recording. This method should 476 * be called after the video AND audio sources are set, and before setOutputFile(). 477 * If a time lapse CamcorderProfile is used, audio related source or recording 478 * parameters are ignored. 479 * 480 * @param profile the CamcorderProfile to use 481 * @see android.media.CamcorderProfile 482 */ 483 public void setProfile(CamcorderProfile profile) { 484 setOutputFormat(profile.fileFormat); 485 setVideoFrameRate(profile.videoFrameRate); 486 setVideoSize(profile.videoFrameWidth, profile.videoFrameHeight); 487 setVideoEncodingBitRate(profile.videoBitRate); 488 setVideoEncoder(profile.videoCodec); 489 if (profile.quality >= CamcorderProfile.QUALITY_TIME_LAPSE_LOW && 490 profile.quality <= CamcorderProfile.QUALITY_TIME_LAPSE_QVGA) { 491 // Nothing needs to be done. Call to setCaptureRate() enables 492 // time lapse video recording. 493 } else { 494 setAudioEncodingBitRate(profile.audioBitRate); 495 setAudioChannels(profile.audioChannels); 496 setAudioSamplingRate(profile.audioSampleRate); 497 setAudioEncoder(profile.audioCodec); 498 } 499 } 500 501 /** 502 * Set video frame capture rate. This can be used to set a different video frame capture 503 * rate than the recorded video's playback rate. This method also sets the recording mode 504 * to time lapse. In time lapse video recording, only video is recorded. Audio related 505 * parameters are ignored when a time lapse recording session starts, if an application 506 * sets them. 507 * 508 * @param fps Rate at which frames should be captured in frames per second. 509 * The fps can go as low as desired. However the fastest fps will be limited by the hardware. 510 * For resolutions that can be captured by the video camera, the fastest fps can be computed using 511 * {@link android.hardware.Camera.Parameters#getPreviewFpsRange(int[])}. For higher 512 * resolutions the fastest fps may be more restrictive. 513 * Note that the recorder cannot guarantee that frames will be captured at the 514 * given rate due to camera/encoder limitations. However it tries to be as close as 515 * possible. 516 */ 517 public void setCaptureRate(double fps) { 518 // Make sure that time lapse is enabled when this method is called. 519 setParameter("time-lapse-enable=1"); 520 setParameter("time-lapse-fps=" + fps); 521 } 522 523 /** 524 * Sets the orientation hint for output video playback. 525 * This method should be called before prepare(). This method will not 526 * trigger the source video frame to rotate during video recording, but to 527 * add a composition matrix containing the rotation angle in the output 528 * video if the output format is OutputFormat.THREE_GPP or 529 * OutputFormat.MPEG_4 so that a video player can choose the proper 530 * orientation for playback. Note that some video players may choose 531 * to ignore the compostion matrix in a video during playback. 532 * 533 * @param degrees the angle to be rotated clockwise in degrees. 534 * The supported angles are 0, 90, 180, and 270 degrees. 535 * @throws IllegalArgumentException if the angle is not supported. 536 * 537 */ 538 public void setOrientationHint(int degrees) { 539 if (degrees != 0 && 540 degrees != 90 && 541 degrees != 180 && 542 degrees != 270) { 543 throw new IllegalArgumentException("Unsupported angle: " + degrees); 544 } 545 setParameter("video-param-rotation-angle-degrees=" + degrees); 546 } 547 548 /** 549 * Set and store the geodata (latitude and longitude) in the output file. 550 * This method should be called before prepare(). The geodata is 551 * stored in udta box if the output format is OutputFormat.THREE_GPP 552 * or OutputFormat.MPEG_4, and is ignored for other output formats. 553 * The geodata is stored according to ISO-6709 standard. 554 * 555 * @param latitude latitude in degrees. Its value must be in the 556 * range [-90, 90]. 557 * @param longitude longitude in degrees. Its value must be in the 558 * range [-180, 180]. 559 * 560 * @throws IllegalArgumentException if the given latitude or 561 * longitude is out of range. 562 * 563 */ 564 public void setLocation(float latitude, float longitude) { 565 int latitudex10000 = (int) (latitude * 10000 + 0.5); 566 int longitudex10000 = (int) (longitude * 10000 + 0.5); 567 568 if (latitudex10000 > 900000 || latitudex10000 < -900000) { 569 String msg = "Latitude: " + latitude + " out of range."; 570 throw new IllegalArgumentException(msg); 571 } 572 if (longitudex10000 > 1800000 || longitudex10000 < -1800000) { 573 String msg = "Longitude: " + longitude + " out of range"; 574 throw new IllegalArgumentException(msg); 575 } 576 577 setParameter("param-geotag-latitude=" + latitudex10000); 578 setParameter("param-geotag-longitude=" + longitudex10000); 579 } 580 581 /** 582 * Sets the format of the output file produced during recording. Call this 583 * after setAudioSource()/setVideoSource() but before prepare(). 584 * 585 * <p>It is recommended to always use 3GP format when using the H.263 586 * video encoder and AMR audio encoder. Using an MPEG-4 container format 587 * may confuse some desktop players.</p> 588 * 589 * @param output_format the output format to use. The output format 590 * needs to be specified before setting recording-parameters or encoders. 591 * @throws IllegalStateException if it is called after prepare() or before 592 * setAudioSource()/setVideoSource(). 593 * @see android.media.MediaRecorder.OutputFormat 594 */ 595 public native void setOutputFormat(int output_format) 596 throws IllegalStateException; 597 598 /** 599 * Sets the width and height of the video to be captured. Must be called 600 * after setVideoSource(). Call this after setOutFormat() but before 601 * prepare(). 602 * 603 * @param width the width of the video to be captured 604 * @param height the height of the video to be captured 605 * @throws IllegalStateException if it is called after 606 * prepare() or before setOutputFormat() 607 */ 608 public native void setVideoSize(int width, int height) 609 throws IllegalStateException; 610 611 /** 612 * Sets the frame rate of the video to be captured. Must be called 613 * after setVideoSource(). Call this after setOutFormat() but before 614 * prepare(). 615 * 616 * @param rate the number of frames per second of video to capture 617 * @throws IllegalStateException if it is called after 618 * prepare() or before setOutputFormat(). 619 * 620 * NOTE: On some devices that have auto-frame rate, this sets the 621 * maximum frame rate, not a constant frame rate. Actual frame rate 622 * will vary according to lighting conditions. 623 */ 624 public native void setVideoFrameRate(int rate) throws IllegalStateException; 625 626 /** 627 * Sets the maximum duration (in ms) of the recording session. 628 * Call this after setOutFormat() but before prepare(). 629 * After recording reaches the specified duration, a notification 630 * will be sent to the {@link android.media.MediaRecorder.OnInfoListener} 631 * with a "what" code of {@link #MEDIA_RECORDER_INFO_MAX_DURATION_REACHED} 632 * and recording will be stopped. Stopping happens asynchronously, there 633 * is no guarantee that the recorder will have stopped by the time the 634 * listener is notified. 635 * 636 * @param max_duration_ms the maximum duration in ms (if zero or negative, disables the duration limit) 637 * 638 */ 639 public native void setMaxDuration(int max_duration_ms) throws IllegalArgumentException; 640 641 /** 642 * Sets the maximum filesize (in bytes) of the recording session. 643 * Call this after setOutFormat() but before prepare(). 644 * After recording reaches the specified filesize, a notification 645 * will be sent to the {@link android.media.MediaRecorder.OnInfoListener} 646 * with a "what" code of {@link #MEDIA_RECORDER_INFO_MAX_FILESIZE_REACHED} 647 * and recording will be stopped. Stopping happens asynchronously, there 648 * is no guarantee that the recorder will have stopped by the time the 649 * listener is notified. 650 * 651 * @param max_filesize_bytes the maximum filesize in bytes (if zero or negative, disables the limit) 652 * 653 */ 654 public native void setMaxFileSize(long max_filesize_bytes) throws IllegalArgumentException; 655 656 /** 657 * Sets the audio encoder to be used for recording. If this method is not 658 * called, the output file will not contain an audio track. Call this after 659 * setOutputFormat() but before prepare(). 660 * 661 * @param audio_encoder the audio encoder to use. 662 * @throws IllegalStateException if it is called before 663 * setOutputFormat() or after prepare(). 664 * @see android.media.MediaRecorder.AudioEncoder 665 */ 666 public native void setAudioEncoder(int audio_encoder) 667 throws IllegalStateException; 668 669 /** 670 * Sets the video encoder to be used for recording. If this method is not 671 * called, the output file will not contain an video track. Call this after 672 * setOutputFormat() and before prepare(). 673 * 674 * @param video_encoder the video encoder to use. 675 * @throws IllegalStateException if it is called before 676 * setOutputFormat() or after prepare() 677 * @see android.media.MediaRecorder.VideoEncoder 678 */ 679 public native void setVideoEncoder(int video_encoder) 680 throws IllegalStateException; 681 682 /** 683 * Sets the audio sampling rate for recording. Call this method before prepare(). 684 * Prepare() may perform additional checks on the parameter to make sure whether 685 * the specified audio sampling rate is applicable. The sampling rate really depends 686 * on the format for the audio recording, as well as the capabilities of the platform. 687 * For instance, the sampling rate supported by AAC audio coding standard ranges 688 * from 8 to 96 kHz, the sampling rate supported by AMRNB is 8kHz, and the sampling 689 * rate supported by AMRWB is 16kHz. Please consult with the related audio coding 690 * standard for the supported audio sampling rate. 691 * 692 * @param samplingRate the sampling rate for audio in samples per second. 693 */ 694 public void setAudioSamplingRate(int samplingRate) { 695 if (samplingRate <= 0) { 696 throw new IllegalArgumentException("Audio sampling rate is not positive"); 697 } 698 setParameter("audio-param-sampling-rate=" + samplingRate); 699 } 700 701 /** 702 * Sets the number of audio channels for recording. Call this method before prepare(). 703 * Prepare() may perform additional checks on the parameter to make sure whether the 704 * specified number of audio channels are applicable. 705 * 706 * @param numChannels the number of audio channels. Usually it is either 1 (mono) or 2 707 * (stereo). 708 */ 709 public void setAudioChannels(int numChannels) { 710 if (numChannels <= 0) { 711 throw new IllegalArgumentException("Number of channels is not positive"); 712 } 713 setParameter("audio-param-number-of-channels=" + numChannels); 714 } 715 716 /** 717 * Sets the audio encoding bit rate for recording. Call this method before prepare(). 718 * Prepare() may perform additional checks on the parameter to make sure whether the 719 * specified bit rate is applicable, and sometimes the passed bitRate will be clipped 720 * internally to ensure the audio recording can proceed smoothly based on the 721 * capabilities of the platform. 722 * 723 * @param bitRate the audio encoding bit rate in bits per second. 724 */ 725 public void setAudioEncodingBitRate(int bitRate) { 726 if (bitRate <= 0) { 727 throw new IllegalArgumentException("Audio encoding bit rate is not positive"); 728 } 729 setParameter("audio-param-encoding-bitrate=" + bitRate); 730 } 731 732 /** 733 * Sets the video encoding bit rate for recording. Call this method before prepare(). 734 * Prepare() may perform additional checks on the parameter to make sure whether the 735 * specified bit rate is applicable, and sometimes the passed bitRate will be 736 * clipped internally to ensure the video recording can proceed smoothly based on 737 * the capabilities of the platform. 738 * 739 * @param bitRate the video encoding bit rate in bits per second. 740 */ 741 public void setVideoEncodingBitRate(int bitRate) { 742 if (bitRate <= 0) { 743 throw new IllegalArgumentException("Video encoding bit rate is not positive"); 744 } 745 setParameter("video-param-encoding-bitrate=" + bitRate); 746 } 747 748 /** 749 * Sets the desired video encoding profile and level for recording. The profile and level 750 * must be valid for the video encoder set by {@link #setVideoEncoder}. This method can 751 * called before or after {@link #setVideoEncoder} but it must be called before {@link #prepare}. 752 * {@code prepare()} may perform additional checks on the parameter to make sure that the specified 753 * profile and level are applicable, and sometimes the passed profile or level will be 754 * discarded due to codec capablity or to ensure the video recording can proceed smoothly 755 * based on the capabilities of the platform. <br>Application can also use the 756 * {@link MediaCodecInfo.CodecCapabilities#profileLevels} to query applicable combination of profile 757 * and level for the corresponding format. Note that the requested profile/level may not be supported by 758 * the codec that is actually being used by this MediaRecorder instance. 759 * @param profile declared in {@link MediaCodecInfo.CodecProfileLevel}. 760 * @param level declared in {@link MediaCodecInfo.CodecProfileLevel}. 761 * @throws IllegalArgumentException when an invalid profile or level value is used. 762 */ 763 public void setVideoEncodingProfileLevel(int profile, int level) { 764 if (profile <= 0) { 765 throw new IllegalArgumentException("Video encoding profile is not positive"); 766 } 767 if (level <= 0) { 768 throw new IllegalArgumentException("Video encoding level is not positive"); 769 } 770 setParameter("video-param-encoder-profile=" + profile); 771 setParameter("video-param-encoder-level=" + level); 772 } 773 774 /** 775 * Currently not implemented. It does nothing. 776 * @deprecated Time lapse mode video recording using camera still image capture 777 * is not desirable, and will not be supported. 778 * @hide 779 */ 780 public void setAuxiliaryOutputFile(FileDescriptor fd) 781 { 782 Log.w(TAG, "setAuxiliaryOutputFile(FileDescriptor) is no longer supported."); 783 } 784 785 /** 786 * Currently not implemented. It does nothing. 787 * @deprecated Time lapse mode video recording using camera still image capture 788 * is not desirable, and will not be supported. 789 * @hide 790 */ 791 public void setAuxiliaryOutputFile(String path) 792 { 793 Log.w(TAG, "setAuxiliaryOutputFile(String) is no longer supported."); 794 } 795 796 /** 797 * Pass in the file descriptor of the file to be written. Call this after 798 * setOutputFormat() but before prepare(). 799 * 800 * @param fd an open file descriptor to be written into. 801 * @throws IllegalStateException if it is called before 802 * setOutputFormat() or after prepare() 803 */ 804 public void setOutputFile(FileDescriptor fd) throws IllegalStateException 805 { 806 mPath = null; 807 mFd = fd; 808 } 809 810 /** 811 * Sets the next output file descriptor to be used when the maximum filesize is reached 812 * on the prior output {@link #setOutputFile} or {@link #setNextOutputFile}). File descriptor 813 * must be seekable and writable. After setting the next output file, application should not 814 * use the file referenced by this file descriptor until {@link #stop}. It is the application's 815 * responsibility to close the file descriptor. It is safe to do so as soon as this call returns. 816 * Application must call this after receiving on the 817 * {@link android.media.MediaRecorder.OnInfoListener} a "what" code of 818 * {@link #MEDIA_RECORDER_INFO_MAX_FILESIZE_APPROACHING} and before receiving a "what" code of 819 * {@link #MEDIA_RECORDER_INFO_MAX_FILESIZE_REACHED}. The file is not used until switching to 820 * that output. Application will receive{@link #MEDIA_RECORDER_INFO_NEXT_OUTPUT_FILE_STARTED} 821 * when the next output file is used. Application will not be able to set a new output file if 822 * the previous one has not been used. Application is responsible for cleaning up unused files 823 * after {@link #stop} is called. 824 * 825 * @param fd an open file descriptor to be written into. 826 * @throws IllegalStateException if it is called before prepare(). 827 * @throws IOException if setNextOutputFile fails otherwise. 828 */ 829 public void setNextOutputFile(FileDescriptor fd) throws IllegalStateException, IOException 830 { 831 _setNextOutputFile(fd); 832 } 833 834 /** 835 * Sets the path of the output file to be produced. Call this after 836 * setOutputFormat() but before prepare(). 837 * 838 * @param path The pathname to use. 839 * @throws IllegalStateException if it is called before 840 * setOutputFormat() or after prepare() 841 */ 842 public void setOutputFile(String path) throws IllegalStateException 843 { 844 mFd = null; 845 mPath = path; 846 } 847 848 /** 849 * Sets the next output file path to be used when the maximum filesize is reached 850 * on the prior output {@link #setOutputFile} or {@link #setNextOutputFile}). File should 851 * be seekable. After setting the next output file, application should not use the file 852 * referenced by this file descriptor until {@link #stop}. Application must call this 853 * after receiving on the {@link android.media.MediaRecorder.OnInfoListener} a "what" code of 854 * {@link #MEDIA_RECORDER_INFO_MAX_FILESIZE_APPROACHING} and before receiving a "what" code of 855 * {@link #MEDIA_RECORDER_INFO_MAX_FILESIZE_REACHED}. The file is not used until switching to 856 * that output. Application will receive {@link #MEDIA_RECORDER_INFO_NEXT_OUTPUT_FILE_STARTED} 857 * when the next output file is used. Application will not be able to set a new output file if 858 * the previous one has not been used. Application is responsible for cleaning up unused files 859 * after {@link #stop} is called. 860 * 861 * @param path The pathname to use. 862 * @throws IllegalStateException if it is called before prepare(). 863 * @throws IOException if setNextOutputFile fails otherwise. 864 */ 865 public void setNextOutputFile(String path) throws IllegalStateException, IOException 866 { 867 if (path != null) { 868 RandomAccessFile file = new RandomAccessFile(path, "rws"); 869 try { 870 _setNextOutputFile(file.getFD()); 871 } finally { 872 file.close(); 873 } 874 } 875 } 876 877 // native implementation 878 private native void _setOutputFile(FileDescriptor fd) throws IllegalStateException, IOException; 879 private native void _setNextOutputFile(FileDescriptor fd) throws IllegalStateException, IOException; 880 private native void _prepare() throws IllegalStateException, IOException; 881 882 /** 883 * Prepares the recorder to begin capturing and encoding data. This method 884 * must be called after setting up the desired audio and video sources, 885 * encoders, file format, etc., but before start(). 886 * 887 * @throws IllegalStateException if it is called after 888 * start() or before setOutputFormat(). 889 * @throws IOException if prepare fails otherwise. 890 */ 891 public void prepare() throws IllegalStateException, IOException 892 { 893 if (mPath != null) { 894 RandomAccessFile file = new RandomAccessFile(mPath, "rws"); 895 try { 896 _setOutputFile(file.getFD()); 897 } finally { 898 file.close(); 899 } 900 } else if (mFd != null) { 901 _setOutputFile(mFd); 902 } else { 903 throw new IOException("No valid output file"); 904 } 905 906 _prepare(); 907 } 908 909 /** 910 * Begins capturing and encoding data to the file specified with 911 * setOutputFile(). Call this after prepare(). 912 * 913 * <p>Since API level 13, if applications set a camera via 914 * {@link #setCamera(Camera)}, the apps can use the camera after this method 915 * call. The apps do not need to lock the camera again. However, if this 916 * method fails, the apps should still lock the camera back. The apps should 917 * not start another recording session during recording. 918 * 919 * @throws IllegalStateException if it is called before 920 * prepare() or when the camera is already in use by another app. 921 */ 922 public native void start() throws IllegalStateException; 923 924 /** 925 * Stops recording. Call this after start(). Once recording is stopped, 926 * you will have to configure it again as if it has just been constructed. 927 * Note that a RuntimeException is intentionally thrown to the 928 * application, if no valid audio/video data has been received when stop() 929 * is called. This happens if stop() is called immediately after 930 * start(). The failure lets the application take action accordingly to 931 * clean up the output file (delete the output file, for instance), since 932 * the output file is not properly constructed when this happens. 933 * 934 * @throws IllegalStateException if it is called before start() 935 */ 936 public native void stop() throws IllegalStateException; 937 938 /** 939 * Pauses recording. Call this after start(). You may resume recording 940 * with resume() without reconfiguration, as opposed to stop(). It does 941 * nothing if the recording is already paused. 942 * 943 * When the recording is paused and resumed, the resulting output would 944 * be as if nothing happend during paused period, immediately switching 945 * to the resumed scene. 946 * 947 * @throws IllegalStateException if it is called before start() or after 948 * stop() 949 */ 950 public native void pause() throws IllegalStateException; 951 952 /** 953 * Resumes recording. Call this after start(). It does nothing if the 954 * recording is not paused. 955 * 956 * @throws IllegalStateException if it is called before start() or after 957 * stop() 958 * @see android.media.MediaRecorder#pause 959 */ 960 public native void resume() throws IllegalStateException; 961 962 /** 963 * Restarts the MediaRecorder to its idle state. After calling 964 * this method, you will have to configure it again as if it had just been 965 * constructed. 966 */ 967 public void reset() { 968 native_reset(); 969 970 // make sure none of the listeners get called anymore 971 mEventHandler.removeCallbacksAndMessages(null); 972 } 973 974 private native void native_reset(); 975 976 /** 977 * Returns the maximum absolute amplitude that was sampled since the last 978 * call to this method. Call this only after the setAudioSource(). 979 * 980 * @return the maximum absolute amplitude measured since the last call, or 981 * 0 when called for the first time 982 * @throws IllegalStateException if it is called before 983 * the audio source has been set. 984 */ 985 public native int getMaxAmplitude() throws IllegalStateException; 986 987 /* Do not change this value without updating its counterpart 988 * in include/media/mediarecorder.h or mediaplayer.h! 989 */ 990 /** Unspecified media recorder error. 991 * @see android.media.MediaRecorder.OnErrorListener 992 */ 993 public static final int MEDIA_RECORDER_ERROR_UNKNOWN = 1; 994 /** Media server died. In this case, the application must release the 995 * MediaRecorder object and instantiate a new one. 996 * @see android.media.MediaRecorder.OnErrorListener 997 */ 998 public static final int MEDIA_ERROR_SERVER_DIED = 100; 999 1000 /** 1001 * Interface definition for a callback to be invoked when an error 1002 * occurs while recording. 1003 */ 1004 public interface OnErrorListener 1005 { 1006 /** 1007 * Called when an error occurs while recording. 1008 * 1009 * @param mr the MediaRecorder that encountered the error 1010 * @param what the type of error that has occurred: 1011 * <ul> 1012 * <li>{@link #MEDIA_RECORDER_ERROR_UNKNOWN} 1013 * <li>{@link #MEDIA_ERROR_SERVER_DIED} 1014 * </ul> 1015 * @param extra an extra code, specific to the error type 1016 */ 1017 void onError(MediaRecorder mr, int what, int extra); 1018 } 1019 1020 /** 1021 * Register a callback to be invoked when an error occurs while 1022 * recording. 1023 * 1024 * @param l the callback that will be run 1025 */ 1026 public void setOnErrorListener(OnErrorListener l) 1027 { 1028 mOnErrorListener = l; 1029 } 1030 1031 /* Do not change these values without updating their counterparts 1032 * in include/media/mediarecorder.h! 1033 */ 1034 /** Unspecified media recorder info. 1035 * @see android.media.MediaRecorder.OnInfoListener 1036 */ 1037 public static final int MEDIA_RECORDER_INFO_UNKNOWN = 1; 1038 /** A maximum duration had been setup and has now been reached. 1039 * @see android.media.MediaRecorder.OnInfoListener 1040 */ 1041 public static final int MEDIA_RECORDER_INFO_MAX_DURATION_REACHED = 800; 1042 /** A maximum filesize had been setup and has now been reached. 1043 * Note: This event will not be sent if application already set 1044 * next output file through {@link #setNextOutputFile}. 1045 * @see android.media.MediaRecorder.OnInfoListener 1046 */ 1047 public static final int MEDIA_RECORDER_INFO_MAX_FILESIZE_REACHED = 801; 1048 /** A maximum filesize had been setup and current recorded file size 1049 * has reached 90% of the limit. This is sent once per file upon 1050 * reaching/passing the 90% limit. To continue the recording, applicaiton 1051 * should use {@link #setNextOutputFile} to set the next output file. 1052 * Otherwise, recording will stop when reaching maximum file size. 1053 * @see android.media.MediaRecorder.OnInfoListener 1054 */ 1055 public static final int MEDIA_RECORDER_INFO_MAX_FILESIZE_APPROACHING = 802; 1056 /** A maximum filesize had been reached and MediaRecorder has switched 1057 * output to a new file set by application {@link #setNextOutputFile}. 1058 * For best practice, application should use this event to keep track 1059 * of whether the file previously set has been used or not. 1060 * @see android.media.MediaRecorder.OnInfoListener 1061 */ 1062 public static final int MEDIA_RECORDER_INFO_NEXT_OUTPUT_FILE_STARTED = 803; 1063 1064 /** informational events for individual tracks, for testing purpose. 1065 * The track informational event usually contains two parts in the ext1 1066 * arg of the onInfo() callback: bit 31-28 contains the track id; and 1067 * the rest of the 28 bits contains the informational event defined here. 1068 * For example, ext1 = (1 << 28 | MEDIA_RECORDER_TRACK_INFO_TYPE) if the 1069 * track id is 1 for informational event MEDIA_RECORDER_TRACK_INFO_TYPE; 1070 * while ext1 = (0 << 28 | MEDIA_RECORDER_TRACK_INFO_TYPE) if the track 1071 * id is 0 for informational event MEDIA_RECORDER_TRACK_INFO_TYPE. The 1072 * application should extract the track id and the type of informational 1073 * event from ext1, accordingly. 1074 * 1075 * FIXME: 1076 * Please update the comment for onInfo also when these 1077 * events are unhidden so that application knows how to extract the track 1078 * id and the informational event type from onInfo callback. 1079 * 1080 * {@hide} 1081 */ 1082 public static final int MEDIA_RECORDER_TRACK_INFO_LIST_START = 1000; 1083 /** Signal the completion of the track for the recording session. 1084 * {@hide} 1085 */ 1086 public static final int MEDIA_RECORDER_TRACK_INFO_COMPLETION_STATUS = 1000; 1087 /** Indicate the recording progress in time (ms) during recording. 1088 * {@hide} 1089 */ 1090 public static final int MEDIA_RECORDER_TRACK_INFO_PROGRESS_IN_TIME = 1001; 1091 /** Indicate the track type: 0 for Audio and 1 for Video. 1092 * {@hide} 1093 */ 1094 public static final int MEDIA_RECORDER_TRACK_INFO_TYPE = 1002; 1095 /** Provide the track duration information. 1096 * {@hide} 1097 */ 1098 public static final int MEDIA_RECORDER_TRACK_INFO_DURATION_MS = 1003; 1099 /** Provide the max chunk duration in time (ms) for the given track. 1100 * {@hide} 1101 */ 1102 public static final int MEDIA_RECORDER_TRACK_INFO_MAX_CHUNK_DUR_MS = 1004; 1103 /** Provide the total number of recordd frames. 1104 * {@hide} 1105 */ 1106 public static final int MEDIA_RECORDER_TRACK_INFO_ENCODED_FRAMES = 1005; 1107 /** Provide the max spacing between neighboring chunks for the given track. 1108 * {@hide} 1109 */ 1110 public static final int MEDIA_RECORDER_TRACK_INTER_CHUNK_TIME_MS = 1006; 1111 /** Provide the elapsed time measuring from the start of the recording 1112 * till the first output frame of the given track is received, excluding 1113 * any intentional start time offset of a recording session for the 1114 * purpose of eliminating the recording sound in the recorded file. 1115 * {@hide} 1116 */ 1117 public static final int MEDIA_RECORDER_TRACK_INFO_INITIAL_DELAY_MS = 1007; 1118 /** Provide the start time difference (delay) betweeen this track and 1119 * the start of the movie. 1120 * {@hide} 1121 */ 1122 public static final int MEDIA_RECORDER_TRACK_INFO_START_OFFSET_MS = 1008; 1123 /** Provide the total number of data (in kilo-bytes) encoded. 1124 * {@hide} 1125 */ 1126 public static final int MEDIA_RECORDER_TRACK_INFO_DATA_KBYTES = 1009; 1127 /** 1128 * {@hide} 1129 */ 1130 public static final int MEDIA_RECORDER_TRACK_INFO_LIST_END = 2000; 1131 1132 1133 /** 1134 * Interface definition of a callback to be invoked to communicate some 1135 * info and/or warning about the recording. 1136 */ 1137 public interface OnInfoListener 1138 { 1139 /** 1140 * Called to indicate an info or a warning during recording. 1141 * 1142 * @param mr the MediaRecorder the info pertains to 1143 * @param what the type of info or warning that has occurred 1144 * <ul> 1145 * <li>{@link #MEDIA_RECORDER_INFO_UNKNOWN} 1146 * <li>{@link #MEDIA_RECORDER_INFO_MAX_DURATION_REACHED} 1147 * <li>{@link #MEDIA_RECORDER_INFO_MAX_FILESIZE_REACHED} 1148 * </ul> 1149 * @param extra an extra code, specific to the info type 1150 */ 1151 void onInfo(MediaRecorder mr, int what, int extra); 1152 } 1153 1154 /** 1155 * Register a callback to be invoked when an informational event occurs while 1156 * recording. 1157 * 1158 * @param listener the callback that will be run 1159 */ 1160 public void setOnInfoListener(OnInfoListener listener) 1161 { 1162 mOnInfoListener = listener; 1163 } 1164 1165 private class EventHandler extends Handler 1166 { 1167 private MediaRecorder mMediaRecorder; 1168 1169 public EventHandler(MediaRecorder mr, Looper looper) { 1170 super(looper); 1171 mMediaRecorder = mr; 1172 } 1173 1174 /* Do not change these values without updating their counterparts 1175 * in include/media/mediarecorder.h! 1176 */ 1177 private static final int MEDIA_RECORDER_EVENT_LIST_START = 1; 1178 private static final int MEDIA_RECORDER_EVENT_ERROR = 1; 1179 private static final int MEDIA_RECORDER_EVENT_INFO = 2; 1180 private static final int MEDIA_RECORDER_EVENT_LIST_END = 99; 1181 1182 /* Events related to individual tracks */ 1183 private static final int MEDIA_RECORDER_TRACK_EVENT_LIST_START = 100; 1184 private static final int MEDIA_RECORDER_TRACK_EVENT_ERROR = 100; 1185 private static final int MEDIA_RECORDER_TRACK_EVENT_INFO = 101; 1186 private static final int MEDIA_RECORDER_TRACK_EVENT_LIST_END = 1000; 1187 1188 1189 @Override 1190 public void handleMessage(Message msg) { 1191 if (mMediaRecorder.mNativeContext == 0) { 1192 Log.w(TAG, "mediarecorder went away with unhandled events"); 1193 return; 1194 } 1195 switch(msg.what) { 1196 case MEDIA_RECORDER_EVENT_ERROR: 1197 case MEDIA_RECORDER_TRACK_EVENT_ERROR: 1198 if (mOnErrorListener != null) 1199 mOnErrorListener.onError(mMediaRecorder, msg.arg1, msg.arg2); 1200 1201 return; 1202 1203 case MEDIA_RECORDER_EVENT_INFO: 1204 case MEDIA_RECORDER_TRACK_EVENT_INFO: 1205 if (mOnInfoListener != null) 1206 mOnInfoListener.onInfo(mMediaRecorder, msg.arg1, msg.arg2); 1207 1208 return; 1209 1210 default: 1211 Log.e(TAG, "Unknown message type " + msg.what); 1212 return; 1213 } 1214 } 1215 } 1216 1217 /** 1218 * Called from native code when an interesting event happens. This method 1219 * just uses the EventHandler system to post the event back to the main app thread. 1220 * We use a weak reference to the original MediaRecorder object so that the native 1221 * code is safe from the object disappearing from underneath it. (This is 1222 * the cookie passed to native_setup().) 1223 */ 1224 private static void postEventFromNative(Object mediarecorder_ref, 1225 int what, int arg1, int arg2, Object obj) 1226 { 1227 MediaRecorder mr = (MediaRecorder)((WeakReference)mediarecorder_ref).get(); 1228 if (mr == null) { 1229 return; 1230 } 1231 1232 if (mr.mEventHandler != null) { 1233 Message m = mr.mEventHandler.obtainMessage(what, arg1, arg2, obj); 1234 mr.mEventHandler.sendMessage(m); 1235 } 1236 } 1237 1238 /** 1239 * Releases resources associated with this MediaRecorder object. 1240 * It is good practice to call this method when you're done 1241 * using the MediaRecorder. In particular, whenever an Activity 1242 * of an application is paused (its onPause() method is called), 1243 * or stopped (its onStop() method is called), this method should be 1244 * invoked to release the MediaRecorder object, unless the application 1245 * has a special need to keep the object around. In addition to 1246 * unnecessary resources (such as memory and instances of codecs) 1247 * being held, failure to call this method immediately if a 1248 * MediaRecorder object is no longer needed may also lead to 1249 * continuous battery consumption for mobile devices, and recording 1250 * failure for other applications if no multiple instances of the 1251 * same codec are supported on a device. Even if multiple instances 1252 * of the same codec are supported, some performance degradation 1253 * may be expected when unnecessary multiple instances are used 1254 * at the same time. 1255 */ 1256 public native void release(); 1257 1258 private static native final void native_init(); 1259 1260 private native final void native_setup(Object mediarecorder_this, 1261 String clientName, String opPackageName) throws IllegalStateException; 1262 1263 private native final void native_finalize(); 1264 1265 private native void setParameter(String nameValuePair); 1266 1267 /** 1268 * Return Metrics data about the current Mediarecorder instance. 1269 * 1270 * @return a {@link PersistableBundle} containing the set of attributes and values 1271 * available for the media being generated by this instance of 1272 * MediaRecorder. 1273 * The attributes are descibed in {@link MetricsConstants}. 1274 * 1275 * Additional vendor-specific fields may also be present in 1276 * the return value. 1277 */ 1278 public PersistableBundle getMetrics() { 1279 PersistableBundle bundle = native_getMetrics(); 1280 return bundle; 1281 } 1282 1283 private native PersistableBundle native_getMetrics(); 1284 1285 @Override 1286 protected void finalize() { native_finalize(); } 1287 1288 public final static class MetricsConstants 1289 { 1290 private MetricsConstants() {} 1291 1292 /** 1293 * Key to extract the audio bitrate 1294 * from the {@link MediaRecorder#getMetrics} return. 1295 * The value is an integer. 1296 */ 1297 public static final String AUDIO_BITRATE = "android.media.mediarecorder.audio-bitrate"; 1298 1299 /** 1300 * Key to extract the number of audio channels 1301 * from the {@link MediaRecorder#getMetrics} return. 1302 * The value is an integer. 1303 */ 1304 public static final String AUDIO_CHANNELS = "android.media.mediarecorder.audio-channels"; 1305 1306 /** 1307 * Key to extract the audio samplerate 1308 * from the {@link MediaRecorder#getMetrics} return. 1309 * The value is an integer. 1310 */ 1311 public static final String AUDIO_SAMPLERATE = "android.media.mediarecorder.audio-samplerate"; 1312 1313 /** 1314 * Key to extract the audio timescale 1315 * from the {@link MediaRecorder#getMetrics} return. 1316 * The value is an integer. 1317 */ 1318 public static final String AUDIO_TIMESCALE = "android.media.mediarecorder.audio-timescale"; 1319 1320 /** 1321 * Key to extract the video capture frame rate 1322 * from the {@link MediaRecorder#getMetrics} return. 1323 * The value is a double. 1324 */ 1325 public static final String CAPTURE_FPS = "android.media.mediarecorder.capture-fps"; 1326 1327 /** 1328 * Key to extract the video capture framerate enable value 1329 * from the {@link MediaRecorder#getMetrics} return. 1330 * The value is an integer. 1331 */ 1332 public static final String CAPTURE_FPS_ENABLE = "android.media.mediarecorder.capture-fpsenable"; 1333 1334 /** 1335 * Key to extract the intended playback frame rate 1336 * from the {@link MediaRecorder#getMetrics} return. 1337 * The value is an integer. 1338 */ 1339 public static final String FRAMERATE = "android.media.mediarecorder.frame-rate"; 1340 1341 /** 1342 * Key to extract the height (in pixels) of the captured video 1343 * from the {@link MediaRecorder#getMetrics} return. 1344 * The value is an integer. 1345 */ 1346 public static final String HEIGHT = "android.media.mediarecorder.height"; 1347 1348 /** 1349 * Key to extract the recorded movies time units 1350 * from the {@link MediaRecorder#getMetrics} return. 1351 * The value is an integer. 1352 * A value of 1000 indicates that the movie's timing is in milliseconds. 1353 */ 1354 public static final String MOVIE_TIMESCALE = "android.media.mediarecorder.movie-timescale"; 1355 1356 /** 1357 * Key to extract the rotation (in degrees) to properly orient the video 1358 * from the {@link MediaRecorder#getMetrics} return. 1359 * The value is an integer. 1360 */ 1361 public static final String ROTATION = "android.media.mediarecorder.rotation"; 1362 1363 /** 1364 * Key to extract the video bitrate from being used 1365 * from the {@link MediaRecorder#getMetrics} return. 1366 * The value is an integer. 1367 */ 1368 public static final String VIDEO_BITRATE = "android.media.mediarecorder.video-bitrate"; 1369 1370 /** 1371 * Key to extract the value for how often video iframes are generated 1372 * from the {@link MediaRecorder#getMetrics} return. 1373 * The value is an integer. 1374 */ 1375 public static final String VIDEO_IFRAME_INTERVAL = "android.media.mediarecorder.video-iframe-interval"; 1376 1377 /** 1378 * Key to extract the video encoding level 1379 * from the {@link MediaRecorder#getMetrics} return. 1380 * The value is an integer. 1381 */ 1382 public static final String VIDEO_LEVEL = "android.media.mediarecorder.video-encoder-level"; 1383 1384 /** 1385 * Key to extract the video encoding profile 1386 * from the {@link MediaRecorder#getMetrics} return. 1387 * The value is an integer. 1388 */ 1389 public static final String VIDEO_PROFILE = "android.media.mediarecorder.video-encoder-profile"; 1390 1391 /** 1392 * Key to extract the recorded video time units 1393 * from the {@link MediaRecorder#getMetrics} return. 1394 * The value is an integer. 1395 * A value of 1000 indicates that the video's timing is in milliseconds. 1396 */ 1397 public static final String VIDEO_TIMESCALE = "android.media.mediarecorder.video-timescale"; 1398 1399 /** 1400 * Key to extract the width (in pixels) of the captured video 1401 * from the {@link MediaRecorder#getMetrics} return. 1402 * The value is an integer. 1403 */ 1404 public static final String WIDTH = "android.media.mediarecorder.width"; 1405 1406 } 1407} 1408 1409