Android SoundPool 不起作用 - 只需提供点击声音



我有六个按钮,想要根据点击播放不同的.mp3文件。我已经实现了这样的onClick方法:

SoundPool sp = new SoundPool(1, AudioManager.STREAM_MUSIC, 0);
if(view = screamButton) {
     soundId = sp.load(getContext(), R.raw.scream, 1);
}
// Some else if - statements
sp.play(soundId, 5, 5, 0, 0, 1);

我还尝试创建一个AudioManager来设置音量,但这并没有改变任何事情。

可能出了什么问题?

Hank

我遇到了和你一样的问题。

我的解决方案是:

在OnCreate中放入:

    //The 20 is the maximum let the audio reproduce at the same time 
    sp = new SoundPool(20, AudioManager.STREAM_MUSIC, 0);
    //volume control from the cellphone:
    this.setVolumeControlStream(AudioManager.STREAM_MUSIC);
    //load the audio
    soundId= sp.load(this,R.raw.scream,1);

并且,最后,当你想要声音再现时放上这个:

     sp.play(soundId, 1, 1, 0, 0, 1);

它对我来说还可以。

我希望能帮助你。

致以问候,皮耶罗。

最新更新