AudioTrack: how to detect end of sound?

*爱你&永不变心* 提交于 2019-12-22 18:46:09

问题


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.


回答1:


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



来源:https://stackoverflow.com/questions/7642704/audiotrack-how-to-detect-end-of-sound

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!