Esempio n. 1
0
  public void testAudioRecordOP() throws Exception {
    if (!hasMicrophone()) {
      return;
    }
    final int SLEEP_TIME = 10;
    final int RECORD_TIME = 10000;
    assertEquals(AudioRecord.STATE_INITIALIZED, mAudioRecord.getState());

    int markerInFrames = mAudioRecord.getSampleRate() / 2;
    assertEquals(AudioRecord.SUCCESS, mAudioRecord.setNotificationMarkerPosition(markerInFrames));
    assertEquals(markerInFrames, mAudioRecord.getNotificationMarkerPosition());
    int periodInFrames = mAudioRecord.getSampleRate();
    assertEquals(AudioRecord.SUCCESS, mAudioRecord.setPositionNotificationPeriod(periodInFrames));
    assertEquals(periodInFrames, mAudioRecord.getPositionNotificationPeriod());
    OnRecordPositionUpdateListener listener =
        new OnRecordPositionUpdateListener() {

          public void onMarkerReached(AudioRecord recorder) {
            mIsOnMarkerReachedCalled = true;
          }

          public void onPeriodicNotification(AudioRecord recorder) {
            mIsOnPeriodicNotificationCalled = true;
          }
        };
    mAudioRecord.setRecordPositionUpdateListener(listener);

    // use byte array as buffer
    final int BUFFER_SIZE = 102400;
    byte[] byteData = new byte[BUFFER_SIZE];
    long time = System.currentTimeMillis();
    mAudioRecord.startRecording();
    assertEquals(AudioRecord.RECORDSTATE_RECORDING, mAudioRecord.getRecordingState());
    while (System.currentTimeMillis() - time < RECORD_TIME) {
      Thread.sleep(SLEEP_TIME);
      mAudioRecord.read(byteData, 0, BUFFER_SIZE);
    }
    mAudioRecord.stop();
    assertEquals(AudioRecord.RECORDSTATE_STOPPED, mAudioRecord.getRecordingState());
    assertTrue(mIsOnMarkerReachedCalled);
    assertTrue(mIsOnPeriodicNotificationCalled);
    reset();

    // use short array as buffer
    short[] shortData = new short[BUFFER_SIZE];
    time = System.currentTimeMillis();
    mAudioRecord.startRecording();
    assertEquals(AudioRecord.RECORDSTATE_RECORDING, mAudioRecord.getRecordingState());
    while (System.currentTimeMillis() - time < RECORD_TIME) {
      Thread.sleep(SLEEP_TIME);
      mAudioRecord.read(shortData, 0, BUFFER_SIZE);
    }
    mAudioRecord.stop();
    assertEquals(AudioRecord.RECORDSTATE_STOPPED, mAudioRecord.getRecordingState());
    assertTrue(mIsOnMarkerReachedCalled);
    assertTrue(mIsOnPeriodicNotificationCalled);
    reset();

    // use ByteBuffer as buffer
    ByteBuffer byteBuffer = ByteBuffer.allocateDirect(BUFFER_SIZE);
    time = System.currentTimeMillis();
    mAudioRecord.startRecording();
    assertEquals(AudioRecord.RECORDSTATE_RECORDING, mAudioRecord.getRecordingState());
    while (System.currentTimeMillis() - time < RECORD_TIME) {
      Thread.sleep(SLEEP_TIME);
      mAudioRecord.read(byteBuffer, BUFFER_SIZE);
    }
    mAudioRecord.stop();
    assertEquals(AudioRecord.RECORDSTATE_STOPPED, mAudioRecord.getRecordingState());
    assertTrue(mIsOnMarkerReachedCalled);
    assertTrue(mIsOnPeriodicNotificationCalled);
    reset();

    // use handler
    final Handler handler =
        new Handler(Looper.getMainLooper()) {
          @Override
          public void handleMessage(Message msg) {
            mIsHandleMessageCalled = true;
            super.handleMessage(msg);
          }
        };

    mAudioRecord.setRecordPositionUpdateListener(listener, handler);
    time = System.currentTimeMillis();
    mAudioRecord.startRecording();
    assertEquals(AudioRecord.RECORDSTATE_RECORDING, mAudioRecord.getRecordingState());
    while (System.currentTimeMillis() - time < RECORD_TIME) {
      Thread.sleep(SLEEP_TIME);
      mAudioRecord.read(byteData, 0, BUFFER_SIZE);
    }
    mAudioRecord.stop();
    assertEquals(AudioRecord.RECORDSTATE_STOPPED, mAudioRecord.getRecordingState());
    assertTrue(mIsOnMarkerReachedCalled);
    assertTrue(mIsOnPeriodicNotificationCalled);
    // The handler argument is only ever used for getting the associated Looper
    assertFalse(mIsHandleMessageCalled);

    mAudioRecord.release();
    assertEquals(AudioRecord.STATE_UNINITIALIZED, mAudioRecord.getState());
  }