Android Audio Code Analysis 19-setPosition Function

Source: Internet
Author: User

We have read get position before. Let's take a look at set position today.


**************************************** * Source Code ************************************** ***********
// 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 = 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 ();
}
**************************************** **************************************** **************
Source code path:
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 = 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 ();
}
######################################## ###################


& Summary &&&&&&&&&&&&&&&& &&&&&&&&&&&&&&&&
Set position is actually a server member in the audio_track_cblk_t struct.
That is, the starting address when AudioMixer copies data from audio_track_cblk_t.
There is a limit on setting position here, that is, it cannot run before the user,
That is, the actual address of the retrieved data cannot exceed the range of the written data.
In the player, you can drag the progress bar to change the address when reading the file,
Instead of the address in the audio_track_cblk_t object.


In addition, the position can only be changed in the stop State.
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& &&&&&&&&&&&&&&&&&&&

From: Jiangfeng's column

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.