Android 安卓媒体播放器正在自我调节

Android 安卓媒体播放器正在自我调节,android,audio,android-mediaplayer,Android,Audio,Android Mediaplayer,我有一个游戏只有一个循环的主音乐主题。在android上,如果用户按下home(主页)按钮,音乐将根据需要暂停,但如果用户随后立即重新打开应用程序,应用程序将重置,但音乐主题的两个实例现在将相互播放,并且不同步(就好像原始实例仍在后台播放,但没有声音)。如何防止主题曲目的重复实例 编辑:好的问题是,onSurfaceChanged正在创建我的游戏上下文的新实例,这当然创建了主题音乐的新实例 这是我的音频课程: public class Audio extends Token { private

我有一个游戏只有一个循环的主音乐
主题。在android上,如果用户按下home(主页)按钮,音乐将根据需要暂停,但如果用户随后立即重新打开应用程序,应用程序将重置,但音乐主题的两个实例现在将相互播放,并且不同步(就好像原始实例仍在后台播放,但没有声音)。如何防止主题曲目的重复实例

编辑:好的问题是,
onSurfaceChanged
正在创建我的游戏上下文的新实例,这当然创建了主题音乐的新实例

这是我的音频课程:

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

你能做的是尝试完全停止mp3,而不是暂停它。因此,您可以覆盖
onPause
onStart
方法,如下所示:

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

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

现在,当你的应用程序暂停(或暂时关闭)时,它将完全停止mp3。当你的应用程序恢复时,它将再次启动mp3。我希望这有帮助

我的回答是否让您了解了一个可能的答案gloo?我已经在从
onPause
调用
theme.stop()
。这当然会使曲目停止,但问题是当应用程序重新打开时。@gloo您是否也使用了onStart(),我最初从
onResume()
调用了
theme.start()
,但我观察到的行为是相同的,即有两份曲目播放不同步。结果我只是做了一些愚蠢的事情。可以结束一个问题吗?