AudioRecord.java revision 9cc489a2196e449dbe090ad9bdbc5cedd0b8334f
1/* 2 * Copyright (C) 2008 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 java.lang.ref.WeakReference; 20import java.io.OutputStream; 21import java.io.IOException; 22import java.lang.IllegalArgumentException; 23import java.lang.IllegalStateException; 24import java.lang.Thread; 25import java.nio.ByteBuffer; 26 27import android.os.Handler; 28import android.os.Looper; 29import android.os.Message; 30import android.util.Log; 31 32/** 33 * The AudioRecord class manages the audio resources for Java applications 34 * to record audio from the audio input hardware of the platform. This is 35 * achieved by "pulling" (reading) the data from the AudioRecord object. The 36 * application is responsible for polling the AudioRecord object in time using one of 37 * the following three methods: {@link #read(byte[],int, int)}, {@link #read(short[], int, int)} 38 * or {@link #read(ByteBuffer, int)}. The choice of which method to use will be based 39 * on the audio data storage format that is the most convenient for the user of AudioRecord. 40 * <p>Upon creation, an AudioRecord object initializes its associated audio buffer that it will 41 * fill with the new audio data. The size of this buffer, specified during the construction, 42 * determines how long an AudioRecord can record before "over-running" data that has not 43 * been read yet. Data should be read from the audio hardware in chunks of sizes inferior to 44 * the total recording buffer size. 45 */ 46public class AudioRecord 47{ 48 //--------------------------------------------------------- 49 // Constants 50 //-------------------- 51 /** 52 * indicates AudioRecord state is not successfully initialized. 53 */ 54 public static final int STATE_UNINITIALIZED = 0; 55 /** 56 * indicates AudioRecord state is ready to be used 57 */ 58 public static final int STATE_INITIALIZED = 1; 59 60 /** 61 * indicates AudioRecord recording state is not recording 62 */ 63 public static final int RECORDSTATE_STOPPED = 1; // matches SL_RECORDSTATE_STOPPED 64 /** 65 * indicates AudioRecord recording state is recording 66 */ 67 public static final int RECORDSTATE_RECORDING = 3;// matches SL_RECORDSTATE_RECORDING 68 69 // Error codes: 70 // to keep in sync with frameworks/base/core/jni/android_media_AudioRecord.cpp 71 /** 72 * Denotes a successful operation. 73 */ 74 public static final int SUCCESS = 0; 75 /** 76 * Denotes a generic operation failure. 77 */ 78 public static final int ERROR = -1; 79 /** 80 * Denotes a failure due to the use of an invalid value. 81 */ 82 public static final int ERROR_BAD_VALUE = -2; 83 /** 84 * Denotes a failure due to the improper use of a method. 85 */ 86 public static final int ERROR_INVALID_OPERATION = -3; 87 88 private static final int AUDIORECORD_ERROR_SETUP_ZEROFRAMECOUNT = -16; 89 private static final int AUDIORECORD_ERROR_SETUP_INVALIDCHANNELMASK = -17; 90 private static final int AUDIORECORD_ERROR_SETUP_INVALIDFORMAT = -18; 91 private static final int AUDIORECORD_ERROR_SETUP_INVALIDSOURCE = -19; 92 private static final int AUDIORECORD_ERROR_SETUP_NATIVEINITFAILED = -20; 93 94 // Events: 95 // to keep in sync with frameworks/base/include/media/AudioRecord.h 96 /** 97 * Event id denotes when record head has reached a previously set marker. 98 */ 99 private static final int NATIVE_EVENT_MARKER = 2; 100 /** 101 * Event id denotes when previously set update period has elapsed during recording. 102 */ 103 private static final int NATIVE_EVENT_NEW_POS = 3; 104 105 private final static String TAG = "AudioRecord-Java"; 106 107 108 //--------------------------------------------------------- 109 // Used exclusively by native code 110 //-------------------- 111 /** 112 * Accessed by native methods: provides access to C++ AudioRecord object 113 */ 114 @SuppressWarnings("unused") 115 private int mNativeRecorderInJavaObj; 116 117 /** 118 * Accessed by native methods: provides access to the callback data. 119 */ 120 @SuppressWarnings("unused") 121 private int mNativeCallbackCookie; 122 123 124 //--------------------------------------------------------- 125 // Member variables 126 //-------------------- 127 /** 128 * The audio data sampling rate in Hz. 129 */ 130 private int mSampleRate = 22050; 131 /** 132 * The number of input audio channels (1 is mono, 2 is stereo) 133 */ 134 private int mChannelCount = 1; 135 /** 136 * The audio channel mask 137 */ 138 private int mChannels = AudioFormat.CHANNEL_IN_MONO; 139 /** 140 * The current audio channel configuration 141 */ 142 private int mChannelConfiguration = AudioFormat.CHANNEL_IN_MONO; 143 /** 144 * The encoding of the audio samples. 145 * @see AudioFormat#ENCODING_PCM_8BIT 146 * @see AudioFormat#ENCODING_PCM_16BIT 147 */ 148 private int mAudioFormat = AudioFormat.ENCODING_PCM_16BIT; 149 /** 150 * Where the audio data is recorded from. 151 */ 152 private int mRecordSource = MediaRecorder.AudioSource.DEFAULT; 153 /** 154 * Indicates the state of the AudioRecord instance. 155 */ 156 private int mState = STATE_UNINITIALIZED; 157 /** 158 * Indicates the recording state of the AudioRecord instance. 159 */ 160 private int mRecordingState = RECORDSTATE_STOPPED; 161 /** 162 * Lock to make sure mRecordingState updates are reflecting the actual state of the object. 163 */ 164 private Object mRecordingStateLock = new Object(); 165 /** 166 * The listener the AudioRecord notifies when the record position reaches a marker 167 * or for periodic updates during the progression of the record head. 168 * @see #setRecordPositionUpdateListener(OnRecordPositionUpdateListener) 169 * @see #setRecordPositionUpdateListener(OnRecordPositionUpdateListener, Handler) 170 */ 171 private OnRecordPositionUpdateListener mPositionListener = null; 172 /** 173 * Lock to protect position listener updates against event notifications 174 */ 175 private final Object mPositionListenerLock = new Object(); 176 /** 177 * Handler for marker events coming from the native code 178 */ 179 private NativeEventHandler mEventHandler = null; 180 /** 181 * Looper associated with the thread that creates the AudioRecord instance 182 */ 183 private Looper mInitializationLooper = null; 184 /** 185 * Size of the native audio buffer. 186 */ 187 private int mNativeBufferSizeInBytes = 0; 188 189 190 //--------------------------------------------------------- 191 // Constructor, Finalize 192 //-------------------- 193 /** 194 * Class constructor. 195 * @param audioSource the recording source. See {@link MediaRecorder.AudioSource} for 196 * recording source definitions. 197 * @param sampleRateInHz the sample rate expressed in Hertz. Examples of rates are (but 198 * not limited to) 44100, 22050 and 11025. 199 * @param channelConfig describes the configuration of the audio channels. 200 * See {@link AudioFormat#CHANNEL_IN_MONO} and 201 * {@link AudioFormat#CHANNEL_IN_STEREO} 202 * @param audioFormat the format in which the audio data is represented. 203 * See {@link AudioFormat#ENCODING_PCM_16BIT} and 204 * {@link AudioFormat#ENCODING_PCM_8BIT} 205 * @param bufferSizeInBytes the total size (in bytes) of the buffer where audio data is written 206 * to during the recording. New audio data can be read from this buffer in smaller chunks 207 * than this size. See {@link #getMinBufferSize(int, int, int)} to determine the minimum 208 * required buffer size for the successful creation of an AudioRecord instance. Using values 209 * smaller than getMinBufferSize() will result in an initialization failure. 210 * @throws java.lang.IllegalArgumentException 211 */ 212 public AudioRecord(int audioSource, int sampleRateInHz, int channelConfig, int audioFormat, 213 int bufferSizeInBytes) 214 throws IllegalArgumentException { 215 mState = STATE_UNINITIALIZED; 216 mRecordingState = RECORDSTATE_STOPPED; 217 218 // remember which looper is associated with the AudioRecord instanciation 219 if ((mInitializationLooper = Looper.myLooper()) == null) { 220 mInitializationLooper = Looper.getMainLooper(); 221 } 222 223 audioParamCheck(audioSource, sampleRateInHz, channelConfig, audioFormat); 224 225 audioBuffSizeCheck(bufferSizeInBytes); 226 227 // native initialization 228 //TODO: update native initialization when information about hardware init failure 229 // due to capture device already open is available. 230 int initResult = native_setup( new WeakReference<AudioRecord>(this), 231 mRecordSource, mSampleRate, mChannels, mAudioFormat, mNativeBufferSizeInBytes); 232 if (initResult != SUCCESS) { 233 loge("Error code "+initResult+" when initializing native AudioRecord object."); 234 return; // with mState == STATE_UNINITIALIZED 235 } 236 237 mState = STATE_INITIALIZED; 238 } 239 240 241 // Convenience method for the constructor's parameter checks. 242 // This is where constructor IllegalArgumentException-s are thrown 243 // postconditions: 244 // mRecordSource is valid 245 // mChannelCount is valid 246 // mChannels is valid 247 // mAudioFormat is valid 248 // mSampleRate is valid 249 private void audioParamCheck(int audioSource, int sampleRateInHz, 250 int channelConfig, int audioFormat) { 251 252 //-------------- 253 // audio source 254 if ( (audioSource < MediaRecorder.AudioSource.DEFAULT) || 255 (audioSource > MediaRecorder.getAudioSourceMax()) ) { 256 throw (new IllegalArgumentException("Invalid audio source.")); 257 } else { 258 mRecordSource = audioSource; 259 } 260 261 //-------------- 262 // sample rate 263 if ( (sampleRateInHz < 4000) || (sampleRateInHz > 48000) ) { 264 throw (new IllegalArgumentException(sampleRateInHz 265 + "Hz is not a supported sample rate.")); 266 } else { 267 mSampleRate = sampleRateInHz; 268 } 269 270 //-------------- 271 // channel config 272 mChannelConfiguration = channelConfig; 273 274 switch (channelConfig) { 275 case AudioFormat.CHANNEL_IN_DEFAULT: // AudioFormat.CHANNEL_CONFIGURATION_DEFAULT 276 case AudioFormat.CHANNEL_IN_MONO: 277 case AudioFormat.CHANNEL_CONFIGURATION_MONO: 278 mChannelCount = 1; 279 mChannels = AudioFormat.CHANNEL_IN_MONO; 280 break; 281 case AudioFormat.CHANNEL_IN_STEREO: 282 case AudioFormat.CHANNEL_CONFIGURATION_STEREO: 283 mChannelCount = 2; 284 mChannels = AudioFormat.CHANNEL_IN_STEREO; 285 break; 286 default: 287 mChannelCount = 0; 288 mChannels = AudioFormat.CHANNEL_INVALID; 289 mChannelConfiguration = AudioFormat.CHANNEL_INVALID; 290 throw (new IllegalArgumentException("Unsupported channel configuration.")); 291 } 292 293 //-------------- 294 // audio format 295 switch (audioFormat) { 296 case AudioFormat.ENCODING_DEFAULT: 297 mAudioFormat = AudioFormat.ENCODING_PCM_16BIT; 298 break; 299 case AudioFormat.ENCODING_PCM_16BIT: 300 case AudioFormat.ENCODING_PCM_8BIT: 301 mAudioFormat = audioFormat; 302 break; 303 default: 304 mAudioFormat = AudioFormat.ENCODING_INVALID; 305 throw (new IllegalArgumentException("Unsupported sample encoding." 306 + " Should be ENCODING_PCM_8BIT or ENCODING_PCM_16BIT.")); 307 } 308 } 309 310 311 // Convenience method for the contructor's audio buffer size check. 312 // preconditions: 313 // mChannelCount is valid 314 // mAudioFormat is AudioFormat.ENCODING_PCM_8BIT OR AudioFormat.ENCODING_PCM_16BIT 315 // postcondition: 316 // mNativeBufferSizeInBytes is valid (multiple of frame size, positive) 317 private void audioBuffSizeCheck(int audioBufferSize) { 318 // NB: this section is only valid with PCM data. 319 // To update when supporting compressed formats 320 int frameSizeInBytes = mChannelCount 321 * (mAudioFormat == AudioFormat.ENCODING_PCM_8BIT ? 1 : 2); 322 if ((audioBufferSize % frameSizeInBytes != 0) || (audioBufferSize < 1)) { 323 throw (new IllegalArgumentException("Invalid audio buffer size.")); 324 } 325 326 mNativeBufferSizeInBytes = audioBufferSize; 327 } 328 329 330 331 /** 332 * Releases the native AudioRecord resources. 333 * The object can no longer be used and the reference should be set to null 334 * after a call to release() 335 */ 336 public void release() { 337 try { 338 stop(); 339 } catch(IllegalStateException ise) { 340 // don't raise an exception, we're releasing the resources. 341 } 342 native_release(); 343 mState = STATE_UNINITIALIZED; 344 } 345 346 347 @Override 348 protected void finalize() { 349 native_finalize(); 350 } 351 352 353 //-------------------------------------------------------------------------- 354 // Getters 355 //-------------------- 356 /** 357 * Returns the configured audio data sample rate in Hz 358 */ 359 public int getSampleRate() { 360 return mSampleRate; 361 } 362 363 /** 364 * Returns the audio recording source. 365 * @see MediaRecorder.AudioSource 366 */ 367 public int getAudioSource() { 368 return mRecordSource; 369 } 370 371 /** 372 * Returns the configured audio data format. See {@link AudioFormat#ENCODING_PCM_16BIT} 373 * and {@link AudioFormat#ENCODING_PCM_8BIT}. 374 */ 375 public int getAudioFormat() { 376 return mAudioFormat; 377 } 378 379 /** 380 * Returns the configured channel configuration. 381 * See {@link AudioFormat#CHANNEL_IN_MONO} 382 * and {@link AudioFormat#CHANNEL_IN_STEREO}. 383 */ 384 public int getChannelConfiguration() { 385 return mChannelConfiguration; 386 } 387 388 /** 389 * Returns the configured number of channels. 390 */ 391 public int getChannelCount() { 392 return mChannelCount; 393 } 394 395 /** 396 * Returns the state of the AudioRecord instance. This is useful after the 397 * AudioRecord instance has been created to check if it was initialized 398 * properly. This ensures that the appropriate hardware resources have been 399 * acquired. 400 * @see AudioRecord#STATE_INITIALIZED 401 * @see AudioRecord#STATE_UNINITIALIZED 402 */ 403 public int getState() { 404 return mState; 405 } 406 407 /** 408 * Returns the recording state of the AudioRecord instance. 409 * @see AudioRecord#RECORDSTATE_STOPPED 410 * @see AudioRecord#RECORDSTATE_RECORDING 411 */ 412 public int getRecordingState() { 413 return mRecordingState; 414 } 415 416 /** 417 * Returns the notification marker position expressed in frames. 418 */ 419 public int getNotificationMarkerPosition() { 420 return native_get_marker_pos(); 421 } 422 423 /** 424 * Returns the notification update period expressed in frames. 425 */ 426 public int getPositionNotificationPeriod() { 427 return native_get_pos_update_period(); 428 } 429 430 /** 431 * Returns the minimum buffer size required for the successful creation of an AudioRecord 432 * object. 433 * Note that this size doesn't guarantee a smooth recording under load, and higher values 434 * should be chosen according to the expected frequency at which the AudioRecord instance 435 * will be polled for new data. 436 * @param sampleRateInHz the sample rate expressed in Hertz. 437 * @param channelConfig describes the configuration of the audio channels. 438 * See {@link AudioFormat#CHANNEL_IN_MONO} and 439 * {@link AudioFormat#CHANNEL_IN_STEREO} 440 * @param audioFormat the format in which the audio data is represented. 441 * See {@link AudioFormat#ENCODING_PCM_16BIT}. 442 * @return {@link #ERROR_BAD_VALUE} if the recording parameters are not supported by the 443 * hardware, or an invalid parameter was passed, 444 * or {@link #ERROR} if the implementation was unable to query the hardware for its 445 * output properties, 446 * or the minimum buffer size expressed in bytes. 447 */ 448 static public int getMinBufferSize(int sampleRateInHz, int channelConfig, int audioFormat) { 449 int channelCount = 0; 450 switch(channelConfig) { 451 case AudioFormat.CHANNEL_IN_DEFAULT: // AudioFormat.CHANNEL_CONFIGURATION_DEFAULT 452 case AudioFormat.CHANNEL_IN_MONO: 453 case AudioFormat.CHANNEL_CONFIGURATION_MONO: 454 channelCount = 1; 455 break; 456 case AudioFormat.CHANNEL_IN_STEREO: 457 case AudioFormat.CHANNEL_CONFIGURATION_STEREO: 458 channelCount = 2; 459 break; 460 case AudioFormat.CHANNEL_INVALID: 461 default: 462 loge("getMinBufferSize(): Invalid channel configuration."); 463 return AudioRecord.ERROR_BAD_VALUE; 464 } 465 466 // PCM_8BIT is not supported at the moment 467 if (audioFormat != AudioFormat.ENCODING_PCM_16BIT) { 468 loge("getMinBufferSize(): Invalid audio format."); 469 return AudioRecord.ERROR_BAD_VALUE; 470 } 471 472 int size = native_get_min_buff_size(sampleRateInHz, channelCount, audioFormat); 473 if (size == 0) { 474 return AudioRecord.ERROR_BAD_VALUE; 475 } 476 else if (size == -1) { 477 return AudioRecord.ERROR; 478 } 479 else { 480 return size; 481 } 482 } 483 484 485 //--------------------------------------------------------- 486 // Transport control methods 487 //-------------------- 488 /** 489 * Starts recording from the AudioRecord instance. 490 * @throws IllegalStateException 491 */ 492 public void startRecording() 493 throws IllegalStateException { 494 if (mState != STATE_INITIALIZED) { 495 throw(new IllegalStateException("startRecording() called on an " 496 +"uninitialized AudioRecord.")); 497 } 498 499 // start recording 500 synchronized(mRecordingStateLock) { 501 if (native_start() == SUCCESS) { 502 mRecordingState = RECORDSTATE_RECORDING; 503 } 504 } 505 } 506 507 508 509 /** 510 * Stops recording. 511 * @throws IllegalStateException 512 */ 513 public void stop() 514 throws IllegalStateException { 515 if (mState != STATE_INITIALIZED) { 516 throw(new IllegalStateException("stop() called on an uninitialized AudioRecord.")); 517 } 518 519 // stop recording 520 synchronized(mRecordingStateLock) { 521 native_stop(); 522 mRecordingState = RECORDSTATE_STOPPED; 523 } 524 } 525 526 527 //--------------------------------------------------------- 528 // Audio data supply 529 //-------------------- 530 /** 531 * Reads audio data from the audio hardware for recording into a buffer. 532 * @param audioData the array to which the recorded audio data is written. 533 * @param offsetInBytes index in audioData from which the data is written expressed in bytes. 534 * @param sizeInBytes the number of requested bytes. 535 * @return the number of bytes that were read or or {@link #ERROR_INVALID_OPERATION} 536 * if the object wasn't properly initialized, or {@link #ERROR_BAD_VALUE} if 537 * the parameters don't resolve to valid data and indexes. 538 * The number of bytes will not exceed sizeInBytes. 539 */ 540 public int read(byte[] audioData, int offsetInBytes, int sizeInBytes) { 541 if (mState != STATE_INITIALIZED) { 542 return ERROR_INVALID_OPERATION; 543 } 544 545 if ( (audioData == null) || (offsetInBytes < 0 ) || (sizeInBytes < 0) 546 || (offsetInBytes + sizeInBytes > audioData.length)) { 547 return ERROR_BAD_VALUE; 548 } 549 550 return native_read_in_byte_array(audioData, offsetInBytes, sizeInBytes); 551 } 552 553 554 /** 555 * Reads audio data from the audio hardware for recording into a buffer. 556 * @param audioData the array to which the recorded audio data is written. 557 * @param offsetInShorts index in audioData from which the data is written expressed in shorts. 558 * @param sizeInShorts the number of requested shorts. 559 * @return the number of shorts that were read or or {@link #ERROR_INVALID_OPERATION} 560 * if the object wasn't properly initialized, or {@link #ERROR_BAD_VALUE} if 561 * the parameters don't resolve to valid data and indexes. 562 * The number of shorts will not exceed sizeInShorts. 563 */ 564 public int read(short[] audioData, int offsetInShorts, int sizeInShorts) { 565 if (mState != STATE_INITIALIZED) { 566 return ERROR_INVALID_OPERATION; 567 } 568 569 if ( (audioData == null) || (offsetInShorts < 0 ) || (sizeInShorts < 0) 570 || (offsetInShorts + sizeInShorts > audioData.length)) { 571 return ERROR_BAD_VALUE; 572 } 573 574 return native_read_in_short_array(audioData, offsetInShorts, sizeInShorts); 575 } 576 577 578 /** 579 * Reads audio data from the audio hardware for recording into a direct buffer. If this buffer 580 * is not a direct buffer, this method will always return 0. 581 * @param audioBuffer the direct buffer to which the recorded audio data is written. 582 * @param sizeInBytes the number of requested bytes. 583 * @return the number of bytes that were read or or {@link #ERROR_INVALID_OPERATION} 584 * if the object wasn't properly initialized, or {@link #ERROR_BAD_VALUE} if 585 * the parameters don't resolve to valid data and indexes. 586 * The number of bytes will not exceed sizeInBytes. 587 */ 588 public int read(ByteBuffer audioBuffer, int sizeInBytes) { 589 if (mState != STATE_INITIALIZED) { 590 return ERROR_INVALID_OPERATION; 591 } 592 593 if ( (audioBuffer == null) || (sizeInBytes < 0) ) { 594 return ERROR_BAD_VALUE; 595 } 596 597 return native_read_in_direct_buffer(audioBuffer, sizeInBytes); 598 } 599 600 601 //-------------------------------------------------------------------------- 602 // Initialization / configuration 603 //-------------------- 604 /** 605 * Sets the listener the AudioRecord notifies when a previously set marker is reached or 606 * for each periodic record head position update. 607 * @param listener 608 */ 609 public void setRecordPositionUpdateListener(OnRecordPositionUpdateListener listener) { 610 setRecordPositionUpdateListener(listener, null); 611 } 612 613 /** 614 * Sets the listener the AudioRecord notifies when a previously set marker is reached or 615 * for each periodic record head position update. 616 * Use this method to receive AudioRecord events in the Handler associated with another 617 * thread than the one in which you created the AudioTrack instance. 618 * @param listener 619 * @param handler the Handler that will receive the event notification messages. 620 */ 621 public void setRecordPositionUpdateListener(OnRecordPositionUpdateListener listener, 622 Handler handler) { 623 synchronized (mPositionListenerLock) { 624 625 mPositionListener = listener; 626 627 if (listener != null) { 628 if (handler != null) { 629 mEventHandler = new NativeEventHandler(this, handler.getLooper()); 630 } else { 631 // no given handler, use the looper the AudioRecord was created in 632 mEventHandler = new NativeEventHandler(this, mInitializationLooper); 633 } 634 } else { 635 mEventHandler = null; 636 } 637 } 638 639 } 640 641 642 /** 643 * Sets the marker position at which the listener is called, if set with 644 * {@link #setRecordPositionUpdateListener(OnRecordPositionUpdateListener)} or 645 * {@link #setRecordPositionUpdateListener(OnRecordPositionUpdateListener, Handler)}. 646 * @param markerInFrames marker position expressed in frames 647 * @return error code or success, see {@link #SUCCESS}, {@link #ERROR_BAD_VALUE}, 648 * {@link #ERROR_INVALID_OPERATION} 649 */ 650 public int setNotificationMarkerPosition(int markerInFrames) { 651 return native_set_marker_pos(markerInFrames); 652 } 653 654 655 /** 656 * Sets the period at which the listener is called, if set with 657 * {@link #setRecordPositionUpdateListener(OnRecordPositionUpdateListener)} or 658 * {@link #setRecordPositionUpdateListener(OnRecordPositionUpdateListener, Handler)}. 659 * @param periodInFrames update period expressed in frames 660 * @return error code or success, see {@link #SUCCESS}, {@link #ERROR_INVALID_OPERATION} 661 */ 662 public int setPositionNotificationPeriod(int periodInFrames) { 663 return native_set_pos_update_period(periodInFrames); 664 } 665 666 667 //--------------------------------------------------------- 668 // Interface definitions 669 //-------------------- 670 /** 671 * Interface definition for a callback to be invoked when an AudioRecord has 672 * reached a notification marker set by {@link AudioRecord#setNotificationMarkerPosition(int)} 673 * or for periodic updates on the progress of the record head, as set by 674 * {@link AudioRecord#setPositionNotificationPeriod(int)}. 675 */ 676 public interface OnRecordPositionUpdateListener { 677 /** 678 * Called on the listener to notify it that the previously set marker has been reached 679 * by the recording head. 680 */ 681 void onMarkerReached(AudioRecord recorder); 682 683 /** 684 * Called on the listener to periodically notify it that the record head has reached 685 * a multiple of the notification period. 686 */ 687 void onPeriodicNotification(AudioRecord recorder); 688 } 689 690 691 692 //--------------------------------------------------------- 693 // Inner classes 694 //-------------------- 695 696 /** 697 * Helper class to handle the forwarding of native events to the appropriate listener 698 * (potentially) handled in a different thread 699 */ 700 private class NativeEventHandler extends Handler { 701 702 private final AudioRecord mAudioRecord; 703 704 NativeEventHandler(AudioRecord recorder, Looper looper) { 705 super(looper); 706 mAudioRecord = recorder; 707 } 708 709 @Override 710 public void handleMessage(Message msg) { 711 OnRecordPositionUpdateListener listener = null; 712 synchronized (mPositionListenerLock) { 713 listener = mAudioRecord.mPositionListener; 714 } 715 716 switch(msg.what) { 717 case NATIVE_EVENT_MARKER: 718 if (listener != null) { 719 listener.onMarkerReached(mAudioRecord); 720 } 721 break; 722 case NATIVE_EVENT_NEW_POS: 723 if (listener != null) { 724 listener.onPeriodicNotification(mAudioRecord); 725 } 726 break; 727 default: 728 Log.e(TAG, "[ android.media.AudioRecord.NativeEventHandler ] " + 729 "Unknown event type: " + msg.what); 730 break; 731 } 732 } 733 }; 734 735 736 //--------------------------------------------------------- 737 // Java methods called from the native side 738 //-------------------- 739 @SuppressWarnings("unused") 740 private static void postEventFromNative(Object audiorecord_ref, 741 int what, int arg1, int arg2, Object obj) { 742 //logd("Event posted from the native side: event="+ what + " args="+ arg1+" "+arg2); 743 AudioRecord recorder = (AudioRecord)((WeakReference)audiorecord_ref).get(); 744 if (recorder == null) { 745 return; 746 } 747 748 if (recorder.mEventHandler != null) { 749 Message m = 750 recorder.mEventHandler.obtainMessage(what, arg1, arg2, obj); 751 recorder.mEventHandler.sendMessage(m); 752 } 753 754 } 755 756 757 //--------------------------------------------------------- 758 // Native methods called from the Java side 759 //-------------------- 760 761 private native final int native_setup(Object audiorecord_this, 762 int recordSource, int sampleRate, int nbChannels, int audioFormat, int buffSizeInBytes); 763 764 private native final void native_finalize(); 765 766 private native final void native_release(); 767 768 private native final int native_start(); 769 770 private native final void native_stop(); 771 772 private native final int native_read_in_byte_array(byte[] audioData, 773 int offsetInBytes, int sizeInBytes); 774 775 private native final int native_read_in_short_array(short[] audioData, 776 int offsetInShorts, int sizeInShorts); 777 778 private native final int native_read_in_direct_buffer(Object jBuffer, int sizeInBytes); 779 780 private native final int native_set_marker_pos(int marker); 781 private native final int native_get_marker_pos(); 782 783 private native final int native_set_pos_update_period(int updatePeriod); 784 private native final int native_get_pos_update_period(); 785 786 static private native final int native_get_min_buff_size( 787 int sampleRateInHz, int channelCount, int audioFormat); 788 789 790 //--------------------------------------------------------- 791 // Utility methods 792 //------------------ 793 794 private static void logd(String msg) { 795 Log.d(TAG, "[ android.media.AudioRecord ] " + msg); 796 } 797 798 private static void loge(String msg) { 799 Log.e(TAG, "[ android.media.AudioRecord ] " + msg); 800 } 801 802} 803 804