AudioTrack: how to detect end of sound?
I'm using AudioTrack
to play a PCM WAV sound file on Android (see also here). Streaming mode, worker thread. I'm wondering how do I detect the end of sound so that I can release and free the AudioTrack
开发者_运维问答. The write()
call, it seems, is blocking - but does it block until the playback of the provided data is complete? Not so sure.
Is it me, or the docs on this class are woefully inadequate? If you happen to have a link to a nice unofficial guide to AudioTrack
, I'd be willing to see one.
From my experience, AudioTrack.write()
call does block until the data is written into HW's audio buffer.
So after write()
finishes, the audio is guranteed to be played. Basically it works well.
To detect the end of audio more precisely, use AudioTrack.setNotificationMarkerPosition()
and AudioTrack.setPlaybackPositionUpdateListener()
. Note setNotificationMarkerPosition()
count the audio samples (not the bytes), so if your audio is 16-bit sample, the position will be dataSize/2.
There is an example in my project: https://github.com/mine260309/android-sexypsf-player/blob/psf2/src/com/mine/psf/sexypsf/MineSexyPsfPlayer.java
精彩评论