Android Audio Code Analysis 16-testplaybackheadpositionafterpause Function

Source: Internet
Author: User

Play, stop, and flush functions. Let's take a look at the pause functions today.

**************************************** * Source Code ************************************** ***********

    //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();    }

**************************************** **************************************** **************
Source code path:
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 = audiotr Ack. 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, minbuffsize, test_mode ); byte data [] = new byte [minbuffsize/2]; // -------- test -------------- assumetrue (test_name, TRAC K. getstate () = audiotrack. state_initialized); track. write (data, 0, Data. length); track. write (data, 0, Data. length); track. play (); thread. sleep (1, 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 voidandroid_media_audiotrack_pause (jnienv * ENV, jobject thiz) {audiotrack * lptrack = (audiotrack *) ENV-> Geti Ntfield (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 is assigned a value in the function audiotrack: createtrack, which ultimately points to a trackhandle object maudiotrack-> pause (); // ++ audioflinger: trackhandle :: pause ++ void audioflinger:: trackhandle: Pause () {mtrack-> pause (); // ++ audioflinger: playbackthread: Track :: pause ++ void audioflinger:: playb Ackthread: Track: Pause () {logv ("Pause (% d), calling thread % d", mname, ipcthreadstate: Self ()-> getcallingpid ()); sp <threadbase> thread = mthread. promote (); If (thread! = 0) {mutex: autolock _ L (thread-> mlock); If (mstate = active | mstate = resuming) {mstate = pausing; logv ("active/resuming => pausing (% d) on thread % P", mname, thread. get (); If (! Isoutputtrack ()) {// ++ isoutputtrack ++ ++ bool isoutputtrack () const {return (mstreamtype = audiosystem: num_stream_types);} // ---------------------------- isoutputtrack -------------------------------------- thread-> mlock. unlock (); // The audiosystem: stopoutput (), (audiosystem: stream_type) mstreamtype, msessionid); thread-> mlock. lock () ;}}// ------------------------------ audioflinger: playbackthread: Track: Pause handler} // -------------------------- audioflinger: trackhandle :: pause audio }}// your audiotrack: Pause audio} // your android_media_audiotrack_pause audio mplaystate = playstate_paused ;}// ------------------------ pause audio int Pos = track. getplaybackheadposition (); log (test_name, "position =" + POS); asserttrue (test_name, POS> 0); // -------- tear down -------------- track. release (); // ++ release ++ ++/*** releases the native audiotrack resources. */Public void release () {// even though native_release () stops the native audiotrack, we need to stop // audiotrack subclasses too. try {stop ();} catch (illegalstateexception ISE) {// don't raise an exception, we're releasing the resources .} native_release (); // ++ android_media_audiotrack_native_release ++ ++ static void android_media_audiotrack_native_release (jnienv * ENV, jobject thiz) {// do everything a call to finalize wocould android_media_audiotrack_native_finalize (ENV, thiz ); ++ android_media_audiotrack_native_finalize ++ ++ // static void android_media_audiotrack_native_finalize (jnienv * ENV, jobject thiz) {// logv ("android_media_audiotrack_native_finalize jobject: % x \ n", (INT) thiz); // Delete the audiotrack object audiotrack * lptrack = (audiotrack *) env-> getintfield (thiz, javaaudiotrackfields. nativetrackinjavaobj); If (lptrack) {// logv ("deleting lptrack: % x \ n", (INT) lptrack); lptrack-> stop (); Delete lptrack ;} // Delete the JNI data audiotrackjnistorage * pjnistorage = (audiotrackjnistorage *) ENV-> getintfield (thiz, javaaudiotrackfields. jnidata); If (pjnistorage) {// Delete global refs created in native_setup env-> deleteglobalref (pjnistorage-> mcallbackdata. audiotrack_class); env-> deleteglobalref (pjnistorage-> mcallbackdata. audiotrack_ref); // logv ("deleting pjnistorage: % x \ n", (INT) pjnistorage); Delete pjnistorage ;}} // ----------------------------- android_media_audiotrack_native_finalize resources // + reset the Native Resources in the Java object so any attempt to access // them after a call to release fails. env-> setintfield (thiz, javaaudiotrackfields. nativetrackinjavaobj, 0); env-> setintfield (thiz, javaaudiotrackfields. jnidata, 0);} // ----------------------------- android_media_audiotrack_native_release initialize mstate = state_uninitialized;} // ---------------------------- release success ----------------------------------}

######################################## ###################

& Summary &&&&&&&&&&&&&&&& &&&&&&&&&&&&&&&&
Like stop, pause calls the audiosystem: stopoutput function.
The difference is that stop will reset, that is, clear the user and server in audio_track_cblk_t to 0.
But pause does not.

This means that when you play the video, press pause. The progress bar stops at the current position. If you press stop, the progress bar returns to the initial state.
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& &&&&&&&&&&&&&&&&&&&

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.