Soundpool 示例尚未准备好
我有一个 .wav 文件,我想在游戏中使用它,目前我正在游戏中每个活动的 onCreate()
中加载声音。
soundCount = soundpool.load(this,R.raw.count, 1);
活动开始后就会播放声音。
soundpool.play(soundCount, 0.9f, 0.9f, 1, -1, 1f);
问题是有时我会遇到错误“sample x not ready”
。 是否可以在启动游戏时加载 .wav 文件一次并将其保留在内存中并在以后的游戏中使用它?或者是否可以等待 1-2 秒声音加载完成?
I have a .wav file that I'd like to use across my game, currently I am loading the sound in onCreate()
of each activity in the game.
soundCount = soundpool.load(this,R.raw.count, 1);
The sound will be played once the activity starts.
soundpool.play(soundCount, 0.9f, 0.9f, 1, -1, 1f);
Problem is at times I will hit the error "sample x not ready"
.
Is it possible to load the .wav file once upon starting the game and keep it in memory and use it later across the game? Or is it possible to wait for 1-2 seconds for the sound to load finish?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
您需要通过
SoundPool.setOnLoadCompleteListener
添加监听器来等待它完成。You'll need to wait for it to finish by adding a listener via
SoundPool.setOnLoadCompleteListener
.由于我的项目与Android 1.5兼容并且我无法使用setOnLoadCompleteListener,所以我决定延迟声音的播放。
我的源代码如下:
// (..)
Since my project is compatible with Android 1.5 and I couldn't use setOnLoadCompleteListener, I resolved making the play of sound delayed.
My source code follows:
// (..)
如果可能的话,您应该使用 setOnLoadCompleteListener ...如果没有,请在对“play”的调用周围包裹一个 while 循环。例如:
这将以 10 毫秒的间隔重试“播放”1000 次。当然,这应该在非 ui 线程上运行,但仍然不理想。但也许比等待任意时间并期望池准备好要强一点。
you should use setOnLoadCompleteListener if possible ... if not, wrap a while loop around your call to 'play'. something like:
this will retry 'play' 1000 times on a 10ms interval. this should be run on a non-ui thread of course, and is still not ideal. but maybe a little stronger than waiting an arbitrary time and expecting the pool to be ready.
SoundPool 库使用 MediaPlayer 服务将音频解码为原始 16 位 PCM 单声道或立体声流,我们可以简单地将其称为流的准备,这需要一些时间,具体取决于声音文件的大小。如果我们尝试在该过程结束之前播放声音,我们会收到“样本 x 未准备好”错误。
对于这个问题有两种解决方案:
然后播放
请注意,这种情况没有例外,或者应用程序在没有任何 try-catch 的情况下不会崩溃
The SoundPool library uses the MediaPlayer service to decode the audio into a raw 16-bit PCM mono or stereo stream which we simply can call preparation of Stream, that takes some times depending on size of sound file. And if we try to play the sound before that process is over, we get "sample x not ready" error.
There are two solutions for this
and then play it
Note that there is no exception for this condition or application is not crashing without any try-catch
// 在构造函数中
// 然后在你想要播放声音的地方输入
// In the constructor
// then where ever you want to play the sound, type
我用简单的 do-while cicle 解决了问题。如果成功,方法 play() 返回非零的streamID,如果失败则返回零。因此,检查返回值就足够了。
I solved with problem with simple do-while cicle. The method play() return non-zero streamID if successful, zero if failed. So, it's sufficient to check the return value.
这绝对适合你!
This would work for you definitely !
这听起来很疯狂,但不要立即播放声音。给您的应用几秒钟的时间来初始化 SoundPool。在我的应用程序中,这正是问题所在;我添加了一个大约 3 秒的假“加载”屏幕,然后一切正常。 (我什至不需要预加载声音。)
This sounds crazy, but don't play sounds right away. Give your app a couple of seconds to initialize the SoundPool. In my app, this was exactly the issue; I added a fake "loading" screen of ~3 seconds, and then everything worked. (I didn't even need to preload the sounds.)