了解在 Android 1.6/2.0/2.1 上使用 SoundPool 加载声音是否成功
在 Android 2.2+ 上,有一个名为 SoundPool.OnLoadCompleteListener 的东西,允许知道声音是否已是否加载成功。
我的目标是较低的 API 版本(最好是 1.6,但也可以选择 2.1),并且我需要知道声音是否已正确加载(因为它是由用户选择的)。正确的做法是什么?
我希望不要使用 MediaPlayer 加载声音一次,如果使用 SoundPool 正确的话?!
On Android 2.2+ there is something called SoundPool.OnLoadCompleteListener allowing to know whether a sound has been loaded successfully or not.
I am targeting lower API version (ideally 1.6 but could go for 2.1) and I need to know whether a sound has been loaded correctly (as it is selected by the user). What's the proper way to do it?
I hope not load the sound once with MediaPlayer and if correct with SoundPool?!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我实现了一种至少适用于 Android 2.1 的兼容
OnLoadCompleteListener
类。构造函数采用
SoundPool
对象,并且已调用SoundPool.load(..)
的声音必须使用OnLoadCompleteListener.addSound(soundId)< 注册/代码>。此后,收听者定期尝试播放请求的声音(音量为零)。如果成功,它会调用您的
onLoadComplete
实现,就像在 Android 2.2+ 版本中一样。这是一个用法示例:
这是来源:
I implemented a kind-of-compatible
OnLoadCompleteListener
class that works at least for Android 2.1.The constructor takes a
SoundPool
object, and sounds for which theSoundPool.load(..)
has been called must be registered withOnLoadCompleteListener.addSound(soundId)
. After this, the listener periodically attempts to play the requested sounds (at zero volume). If successful, it calls your implementation ofonLoadComplete
, like in the Android 2.2+ version.Here's a usage example:
And here's the source:
SoundPool 确实异步加载文件。遗憾的是,在 API8 级别之前,没有 API 可以检查加载是否完全。
正如您所说,从 Android API8 开始,可以通过 OnLoadCompleteListener 检查加载是否完成。下面是一个小示例:Android 声音教程。
SoundPool does load the file asynchronously. Before API8 level there is unfortunately no API to check if the loading has been completely.
As you said as of Android API8 it is possible to check if the loading complete via a OnLoadCompleteListener. Here is a small example for this: Android sounds tutorial.