AudioTrack:如何检测声音的结束?声音、结束、AudioTrack

由网友(独草孤花。)分享简介:我用 AudioTrack 玩Android上的一个PCM WAV声音文件(见的这里)。流模式,工作线程。我不知道我怎样检测声音的结束,这样我可以释放并释放 AudioTrack 。在的write()通话,似乎是阻塞 - 但是否阻塞,直到提供的数据播放完毕?不那么肯定。I'm using AudioTrack to...

我用 AudioTrack 玩Android上的一个PCM WAV声音文件(见的这里)。流模式,工作线程。我不知道我怎样检测声音的结束,这样我可以释放并释放 AudioTrack 。在的write()通话,似乎是阻塞 - 但是否阻塞,直到提供的数据播放完毕?不那么肯定。

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.

是我,还是这个类的文档是远远不够?如果你碰巧有一个链接到一个不错的非官方指南 AudioTrack ,我很愿意看到的。

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.

推荐答案

从我的经验, AudioTrack.write()调用不做块,直到数据被写入到HW的音频缓冲区。

From my experience, AudioTrack.write() call does block until the data is written into HW's audio buffer.

所以经过的write()结束,音频存在保证进行播放。基本上,它工作得很好。

So after write() finishes, the audio is guranteed to be played. Basically it works well.

要检测的声音更precisely年底,使用 AudioTrack.setNotificationMarkerPosition() AudioTrack.setPlaybackPositionUpdateListener()。注 setNotificationMarkerPosition()算音频样本(不是字节),因此,如果您的音频是16位的样品,这个位置是数据大小/ 2。

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.

有在我的项目的例子:https://github.com/mine260309/android-sexypsf-player/blob/psf2/src/com/mine/psf/sexypsf/MineSexyPsfPlayer.java

阅读全文

相关推荐

最新文章