AudioTrack:如何检测声音的结束? [英] AudioTrack: how to detect end of sound?

查看:2750
本文介绍了AudioTrack:如何检测声音的结束?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用 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.

有在我的项目的例子:
<一href=\"https://github.com/mine260309/android-sexypsf-player/blob/psf2/src/com/mine/psf/sexypsf/MineSexyPsfPlayer.java\">https://github.com/mine260309/android-sexypsf-player/blob/psf2/src/com/mine/psf/sexypsf/MineSexyPsfPlayer.java

这篇关于AudioTrack:如何检测声音的结束?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆