//Test case 3: setPlaybackHeadPosition() on paused track @LargeTest public void testSetPlaybackHeadPositionPaused() throws Exception { // constants for test final String TEST_NAME = "testSetPlaybackHeadPositionPaused"; final int TEST_SR = 22050; final int TEST_CONF = AudioFormat.CHANNEL_OUT_MONO; final int TEST_FORMAT = AudioFormat.ENCODING_PCM_16BIT; final int TEST_MODE = AudioTrack.MODE_STREAM; final int TEST_STREAM_TYPE = AudioManager.STREAM_MUSIC; //-------- initialization -------------- int minBuffSize = AudioTrack.getMinBufferSize(TEST_SR, TEST_CONF, TEST_FORMAT); AudioTrack track = new AudioTrack(TEST_STREAM_TYPE, TEST_SR, TEST_CONF, TEST_FORMAT, 2*minBuffSize, TEST_MODE); byte data[] = new byte[minBuffSize]; //-------- test -------------- assumeTrue(TEST_NAME, track.getState() == AudioTrack.STATE_INITIALIZED); track.write(data, 0, data.length); track.write(data, 0, data.length); track.play(); track.pause(); assumeTrue(TEST_NAME, track.getPlayState() == AudioTrack.PLAYSTATE_PAUSED); assertTrue(TEST_NAME, track.setPlaybackHeadPosition(10) == AudioTrack.SUCCESS); //-------- tear down -------------- track.release(); }
//Test case 3: setPlaybackHeadPosition() on paused track @LargeTest public void testSetPlaybackHeadPositionPaused() throws Exception { // constants for test final String TEST_NAME = "testSetPlaybackHeadPositionPaused"; final int TEST_SR = 22050; final int TEST_CONF = AudioFormat.CHANNEL_OUT_MONO; final int TEST_FORMAT = AudioFormat.ENCODING_PCM_16BIT; final int TEST_MODE = AudioTrack.MODE_STREAM; final int TEST_STREAM_TYPE = AudioManager.STREAM_MUSIC; //-------- initialization -------------- int minBuffSize = AudioTrack.getMinBufferSize(TEST_SR, TEST_CONF, TEST_FORMAT); AudioTrack track = new AudioTrack(TEST_STREAM_TYPE, TEST_SR, TEST_CONF, TEST_FORMAT, 2*minBuffSize, TEST_MODE); byte data[] = new byte[minBuffSize]; //-------- test -------------- assumeTrue(TEST_NAME, track.getState() == AudioTrack.STATE_INITIALIZED); track.write(data, 0, data.length); track.write(data, 0, data.length); track.play(); track.pause(); assumeTrue(TEST_NAME, track.getPlayState() == AudioTrack.PLAYSTATE_PAUSED); assertTrue(TEST_NAME, track.setPlaybackHeadPosition(10) == AudioTrack.SUCCESS); // +++++++++++++++++++++++++++++setPlaybackHeadPosition+++++++++++++++++++++++++++++++++++ /** * Sets the playback head position. The track must be stopped for the position to be changed. * @param positionInFrames playback head position expressed in frames * @return error code or success, see {@link #SUCCESS}, {@link #ERROR_BAD_VALUE}, * {@link #ERROR_INVALID_OPERATION} */ public int setPlaybackHeadPosition(int positionInFrames) { synchronized(mPlayStateLock) { if ((mPlayState == PLAYSTATE_STOPPED) || (mPlayState == PLAYSTATE_PAUSED)) { return native_set_position(positionInFrames); // ++++++++++++++++++++++++++++android_media_AudioTrack_set_position++++++++++++++++++++++++++++++++++++ static jint android_media_AudioTrack_set_position(JNIEnv *env, jobject thiz, jint position) { AudioTrack *lpTrack = (AudioTrack *)env->GetIntField( thiz, javaAudioTrackFields.nativeTrackInJavaObj); if (lpTrack) { return android_media_translateErrorCode( lpTrack->setPosition(position) ); // ++++++++++++++++++++++++++++AudioTrack::setPosition++++++++++++++++++++++++++++++++++++ status_t AudioTrack::setPosition(uint32_t position) { Mutex::Autolock _l(mCblk->lock); if (!stopped()) return INVALID_OPERATION; if (position > mCblk->user) return BAD_VALUE; mCblk->server = position; mCblk->flags |= CBLK_FORCEREADY_ON; return NO_ERROR; } // ----------------------------AudioTrack::setPosition------------------------------------ } else { jniThrowException(env, "java/lang/IllegalStateException", "Unable to retrieve AudioTrack pointer for setPosition()"); return AUDIOTRACK_ERROR; } } // ----------------------------android_media_AudioTrack_set_position------------------------------------ } else { return ERROR_INVALID_OPERATION; } } } // -----------------------------setPlaybackHeadPosition----------------------------------- //-------- tear down -------------- track.release(); }
而不是audio_track_cblk_t对象中的地址。
另外,只能在stop的状态下才能改变position。