之前我們看過get position,今天來看看set position。
*****************************************源碼*************************************************
//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();
}
**********************************************************************************************
源碼路徑:
frameworks\base\media\tests\mediaframeworktest\src\com\android\mediaframeworktest\functional\MediaAudioTrackTest.java
#######################說明################################
//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();
}
###########################################################
&&&&&&&&&&&&&&&&&&&&&&&總結&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
set position改變的其實是audio_track_cblk_t結構體中的server成員。
也就是AudioMixer中從audio_track_cblk_t中copy資料時的起始地址。
這兒設定position有個限制,就是不能跑到user前面去,
即不能讓取資料的其真實位址超出已寫資料的範圍。
在播放器中,大幅度拖動進度條,改變的應該是讀取檔案時的地址,
而不是audio_track_cblk_t對象中的地址。
另外,只能在stop的狀態下才能改變position。
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
摘自:江風的專欄