gloo
gloo

Reputation: 2580

Android media player is playing over itself

I have a game with a single main music theme that loops. On android if the user presses the home button the music will pause as desired, but if the user then reopens the app immediately the app will reset but two instances of the music theme will now play over each other and out of sync (as if the original instance was still playing in the background but silently). How do I prevent the duplicate instances of my theme track?

EDIT: ok the issue was that onSurfaceChanged was creating a new instance of my game context, which of course created a new instance of the theme music.

Here is my Audio class:

public class Audio extends Token {
private MediaPlayer theme;
private SoundPool sound;
private int[] note = new int[17];
private int i = -1;
boolean toggle = false;
private int death, gliss, themeLoop;
//private float t;
boolean mute = false;
public Audio(Relic relic) {
    theme = MediaPlayer.create(relic.context(), R.raw.theme_start);
    theme.setLooping(true);

    sound = new SoundPool.Builder().setMaxStreams(6).build();

    note[0] = sound.load(relic.context(), R.raw.note1, 1);
    note[1] = sound.load(relic.context(), R.raw.note2, 1);
    note[2] = sound.load(relic.context(), R.raw.note3, 1);
    note[3] = sound.load(relic.context(), R.raw.note4, 1);
    note[4] = sound.load(relic.context(), R.raw.note5, 1);
    note[5] = sound.load(relic.context(), R.raw.note6, 1);
    note[6] = sound.load(relic.context(), R.raw.note7, 1);
    note[7] = sound.load(relic.context(), R.raw.note8, 1);
    note[8] = sound.load(relic.context(), R.raw.note9, 1);
    note[9] = sound.load(relic.context(), R.raw.note10, 1);
    note[10] = sound.load(relic.context(), R.raw.note11, 1);
    note[11] = sound.load(relic.context(), R.raw.note12, 1);
    note[12] = sound.load(relic.context(), R.raw.note13, 1);
    note[13] = sound.load(relic.context(), R.raw.note14, 1);
    note[14] = sound.load(relic.context(), R.raw.note15, 1);
    note[15] = sound.load(relic.context(), R.raw.note16, 1);
    note[16] = sound.load(relic.context(), R.raw.note17, 1);

    death = sound.load(relic.context(), R.raw.death, 1);
    gliss = sound.load(relic.context(), R.raw.gliss, 1);

    playTheme();
}
public void playTheme() {
    play(theme);
}
public void playNote() {
    i = (i+1) % 17;
    play(note[i]);
}
public void addPoints(int value) {
    if (value > 1) {
        play(gliss);
    } else {
        playNote();
    }
}
public void gameover() {
    play(death);
}
private void play(int i) {
    if (!mute) {
        sound.play(i, 1, 1, 1, 0, 1);
    }
}
private void play(MediaPlayer player) {
    if(!mute) {
        player.start();
    }
}
private void pause(MediaPlayer player) {
    player.pause();
}
public void mute() {
    mute = true;
    pause(theme);
}
public void unmute() {
    mute = false;
    play(theme);
}
public void pause() {
    // this is called from Activity.onPause
    theme.stop();
    theme.release();
    theme = null;
 }
}

Upvotes: 0

Views: 111

Answers (1)

Ishaan Javali
Ishaan Javali

Reputation: 1713

What you can do is try stopping the mp3 altogether rather than pausing it. So you can override the onPause and onStart methods like so:

@Override
public void onPause(){
   super.onPause();
   myMP3.stop();
}

@Override
public void onResume(){
   super.onResume();
   myMP3.start();
}

Now, when your app pauses, (or is temporarily closed), it will stop the mp3 completely. When your app resumes, it will start the mp3 again. I hope this helps!

Upvotes: 1

Related Questions