Android SoundPool在恢复活动时无法播放

时间:2013-12-09 20:25:00

标签: android audio soundpool

我有两个声音,一个用于每个团队获胜,我加载到onCreate()中的SoundPool。这是我在onCreate()

中调用的方法
private void loadSounds() {
    wins = new SoundPool(1, AudioManager.STREAM_MUSIC, 0);
    oneWinsID = wins.load(this, R.raw.teamonewins, 1);
    twoWinsID = wins.load(this, R.raw.teamtwowins, 2);
}

现在我开始另一个活动,当该活动结束时,我恢复了第一个活动,我使用标志为意图加载声音。

private void end(){
    Intent intent = new Intent(this, TitleScreen.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    startActivity(intent);  
}

当此活动恢复时,我会尝试播放声音,但声音无法播放。

private void playTeamWins(int soundID){
    if(SettingsActivity.areSoundsOn()){
      AudioManager audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
      float actualVolume = (float) audioManager
          .getStreamVolume(AudioManager.STREAM_MUSIC);
      float maxVolume = (float) audioManager
          .getStreamMaxVolume(AudioManager.STREAM_MUSIC);
      float volume = actualVolume / maxVolume;
      wins.play(soundID, volume, volume, 1, 0, 1.0f);
    }
}

应该加载声音因为我从未销毁过第一个活动,所以我认为这不是原因。此外,我创建了一个按钮,可以在点击时播放声音,看看它是否与我的声音文件有关,但效果很好。

0 个答案:

没有答案
相关问题