Android Audio代码分析16 - testPlaybackHeadPositionAfterPause函数
play, stop, flush这几个函数,今天来看看pause函数。
*****************************************源码*************************************************
//Test case 4: getPlaybackHeadPosition() is > 0 after play(); pause();
@LargeTest
public void testPlaybackHeadPositionAfterPause() throws Exception {
// constants for test
final String TEST_NAME = "testPlaybackHeadPositionAfterPause";
final int TEST_SR = 22050;
final int TEST_CONF = AudioFormat.CHANNEL_OUT_STEREO;
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,
minBuffSize, TEST_MODE);
byte data[] = new byte[minBuffSize/2];
//-------- test --------------
assumeTrue(TEST_NAME, track.getState() == AudioTrack.STATE_INITIALIZED);
track.write(data, 0, data.length);
track.write(data, 0, data.length);
track.play();
Thread.sleep(100);
track.pause();
int pos = track.getPlaybackHeadPosition();
log(TEST_NAME, "position ="+ pos);
assertTrue(TEST_NAME, pos > 0);
//-------- tear down --------------
track.release();
}
**********************************************************************************************
源码路径:
frameworks\base\media\tests\mediaframeworktest\src\com\android\mediaframeworktest\functional\MediaAudioTrackTest.java
#######################说明################################
//Test case 4: getPlaybackHeadPosition() is > 0 after play(); pause();
@LargeTest
public void testPlaybackHeadPositionAfterPause() throws Exception {
// constants for test
final String TEST_NAME = "testPlaybackHeadPositionAfterPause";
final int TEST_SR = 22050;
final int TEST_CONF = AudioFormat.CHANNEL_OUT_STEREO;
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,
minBuffSize, TEST_MODE);
byte data[] = new byte[minBuffSize/2];
//-------- test --------------
assumeTrue(TEST_NAME, track.getState() == AudioTrack.STATE_INITIALIZED);
track.write(data, 0, data.length);
track.write(data, 0, data.length);
track.play();
Thread.sleep(100);
track.pause();
++++++++++++++++++++++++++++++pause++++++++++++++++++++++++++++++++++
/**
* Pauses the playback of the audio data.
* @throws IllegalStateException
*/
public void pause()
throws IllegalStateException {
if (mState != STATE_INITIALIZED) {
throw(new IllegalStateException("pause() called on uninitialized AudioTrack."));
}
//logd("pause()");
// pause playback
synchronized(mPlayStateLock) {
native_pause();
+++++++++++++++++++++++++++++++android_media_AudioTrack_pause+++++++++++++++++++++++++++++++++
static void
android_media_AudioTrack_pause(JNIEnv *env, jobject thiz)
{
AudioTrack *lpTrack = (AudioTrack *)env->GetIntField(
thiz, javaAudioTrackFields.nativeTrackInJavaObj);
if (lpTrack == NULL ) {
jniThrowException(env, "java/lang/IllegalStateException",
"Unable to retrieve AudioTrack pointer for pause()");
return;
}
lpTrack->pause();
++++++++++++++++++++++++++++++AudioTrack::pause++++++++++++++++++++++++++++++++++
void AudioTrack::pause()
{
LOGV("pause");
if (android_atomic_and(~1, &mActive) == 1) {
// mAudioTrack在函数AudioTrack::createTrack中被赋值,其最终指向的其实是一个TrackHandle对象
mAudioTrack->pause();
++++++++++++++++++++++++++++AudioFlinger::TrackHandle::pause++++++++++++++++++++++++++++++++++++
void AudioFlinger::TrackHandle::pause() {
mTrack->pause();
++++++++++++++++++++++++++++AudioFlinger::PlaybackThread::Track::pause++++++++++++++++++++++++++++++++++++
void AudioFlinger::PlaybackThread::Track::pause()
{
LOGV("pause(%d)
补充:移动开发 , Android ,