我在做一个警报系统。报警器工作正常,但声音不起作用。我正在做一个小测试,我不知道问题出在哪里..在Alarm活动中,我有以下代码:
setVolumeControlStream(AudioManager.STREAM_MUSIC);
soundPool = new SoundPool(20, AudioManager.STREAM_MUSIC, 0);
try {
AssetManager assetManager = getAssets();
AssetFileDescriptor descriptor = assetManager.openFd("sound.ogg");
mySoundId = soundPool.load(descriptor, 1);
} catch (IOException e) {
Log.d(TAG,"We've problem's!!, "
+ e.getMessage());
}
soundPool.play(mySoundId, 1, 1, 0, 0, 1);没有错误..但是音乐不起作用..相反,如果我在底部的作品中编写剧本!!有人能帮帮我吗??
发布于 2012-09-03 14:45:29
我刚试过你的代码,发现了问题所在。这是SoundPool中的一个常见问题。您需要等待声音加载完成后才能播放它。这就是为什么soundPool.play()返回零的原因。声音还没有准备好。
在API8中,引入了setOnLoadCompleteListener(),它将解决这个问题:
soundPool.setOnLoadCompleteListener(new OnLoadCompleteListener() {
public void onLoadComplete(SoundPool soundPool, int sampleId,int status) {
//play the sound.
}
});然而,如果你的目标是较低的API,你不能确定你的声音是否已经准备好播放。在这种情况下,我建议您使用MediaPlayer。
https://stackoverflow.com/questions/12242939
复制相似问题