Android SoundPool not playing upon resuming activity

414 views Asked by At

I have two sounds, one for when each team wins that I load into the SoundPool in onCreate(). This is the method I call in 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);
}

Now I start another activity, and when that activity finishes I resume the first activity where I loaded the sounds using flags for the intent.

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

When this activity resumes, I try to play a sound, but the sound won't play.

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);
    }
}

The sounds should be loaded because I never destroyed the first activity, so I don't think that's the reason. Also, I created a button that would play the sound when clicked, to see if it was something with my sound file, but that worked fine.

0

There are 0 answers