简体   繁体   中英

End of playback detection in AudioTrack is not working

This is the code I am using:

void playSound(){
    final AudioTrack audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC,
            sampleRate, AudioFormat.CHANNEL_OUT_MONO,
            AudioFormat.ENCODING_PCM_16BIT, generatedSnd.length,
            AudioTrack.MODE_STATIC);
    // audioTrack.setNotificationMarkerPosition(generatedSnd.length/2);
    audioTrack.setNotificationMarkerPosition(1);
    audioTrack.setPlaybackPositionUpdateListener(new AudioTrack.OnPlaybackPositionUpdateListener() {
        @Override
        public void onPeriodicNotification(AudioTrack track) {
            // nothing to do
        }

        @Override
        public void onMarkerReached(AudioTrack track) {
            // Log.d(LOG_TAG, "Audio track end of file reached...");
            // messageHandler.sendMessage(messageHandler.obtainMessage(PLAYBACK_END_REACHED));
            Log.d("MY LOG", "Audio track end of file reached...");
            bitPatternValTextView.setText("End of play");
        }
    });
    audioTrack.write(generatedSnd, 0, generatedSnd.length);
    audioTrack.play();
}  

The playSound() function is defined inside

public class InSituActivity extends ActionBarActivity{...}

My assumption was that when the playback of the AudioTrack reached the Nth audio sample (each N being a short or 2 bytes) then audioTrack.setNotificationMarkerPosition(N); would cause onMarkerReached(AudioTrack track) to be called. However as shown in my example code I have tried both generatedSnd.length/2 (generatedSnd[] is an array of bytes that contains the audio samples I want to play) as well as using small constant values such 1, 10 or 20, and onMarkerReached() does not seem to get called in any of those situations. What is wrong here and how do I fix it? I want to be able to play another sound when the current AudioTrack finishes playback. The playSound() function itself will be called in a thread (seperate from the UI thread).

Here is where I got the code from:
How to tell when AudioTrack object has finished playing?

Other references:
AudioTrack: how to detect end of sound?
Confusion about Thread life and AudioTrack in android java

maybe too late for an answer, but this worked for me. I was not getting onMarkerReached() callback either. I just increased the static stream size by a large number. May not work for MODE_STREAM or for different buffer sizes. Works for all the buffer sizes I need which is few 10s of seconds max for my use cases. Try changing this line in your code and see what happens:

int magic_number = 1000; // try with different values. Maybe make it 10% of generatedSnd.length?
final AudioTrack audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC,
        sampleRate, AudioFormat.CHANNEL_OUT_MONO,
        AudioFormat.ENCODING_PCM_16BIT, generatedSnd.length,
        AudioTrack.MODE_STATIC + magic_number);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM