加载新屏幕时,MediaPlayer声音不会停止

时间:2017-04-28 17:37:07

标签: android audio onbackpressed

我正试图在我的安卓游戏上发声。我有一个菜单屏幕的声音和游戏屏幕的新声音。每当我进入游戏画面时,声音就会播放,但当我按回到菜单屏幕时,声音会继续。

    public void update(ElapsedTime elapsedTime) { 
if (mPlayButtonBound.contains((int) touchEvent.x, (int) touchEvent.y)) { 

mSoundManager.playSound(2);    
mGame.getScreenManager().removeScreen(this.getName());     
CardDemoGameScreen cardDemoGameScreen = new CardDemoGameScreen(mGame); 
mGame.getScreenManager().addScreen(cardDemoGameScreen);     
mMusicManager.pause1();    
mMusicManager.play(); 

  } 

}

        @Override
        public boolean onBackPressed() {
            // If we are already at the menu screen then exit
            if(mScreenManager.getCurrentScreen().getName().equals("MenuScreen"))
                return false;
            // Go back to the menu screen

            getScreenManager().removeScreen(mScreenManager.getCurrentScreen().getName());

            MenuScreen menuScreen = new MenuScreen(this);
            getScreenManager().addScreen(menuScreen);
            musicManager mMusicManager = new musicManager(this);
            mMusicManager.stop();
            return true;
        }
    }

MusicManager资料

public class musicManager extends GameScreen实现了MediaPlayer.OnCompletionListener {

AudioManager amg;

private MediaPlayer mMediaPlayer;
private static MediaPlayer mMediaPlayer1;

private String mAssetFile;
private boolean mIsPrepared = false;
private boolean mIsPrepared1 = false;

private String mouseClick;
private Music clicksound;

// private Context context = mGame.getActivity()。getApplicationContext();

//  @param assetDescriptor

public musicManager(Game game) {
    super("musicManager", game);

    mMediaPlayer = new MediaPlayer();
    mMediaPlayer1 = new MediaPlayer();

    try{
        AssetManager assetManager = mGame.getActivity().getAssets();
        AssetFileDescriptor assetDescriptor = assetManager.openFd("Sounds/GameScreen1.wav");
        AssetFileDescriptor assetDescriptor1 = assetManager.openFd(("Sounds/Game-Menu.mp3"));

            //assetDescriptor = assets.openFd("clicksound");
        mMediaPlayer.setDataSource(assetDescriptor.getFileDescriptor(),
                    assetDescriptor.getStartOffset(),
                    assetDescriptor.getLength());

       mMediaPlayer1.setDataSource(assetDescriptor1.getFileDescriptor(),
                assetDescriptor1.getStartOffset(),
                assetDescriptor1.getLength());

            // Prep the audio for playback
        mMediaPlayer.prepare();
        mMediaPlayer1.prepare();
        mIsPrepared = true;
        mIsPrepared1 = true;
        mMediaPlayer.setOnCompletionListener(this);
        mMediaPlayer1.setOnCompletionListener(this);

    } catch (IOException e) {
        String errorTag = "Error:";
        String errorMessage = "Music clip cannot be loaded.";
        Log.w(errorTag, errorMessage);
    }
}

public  void play() {
    if (mMediaPlayer.isPlaying())
        return;
    try {
        synchronized (this) {
            // Start the clip, preparing it if needed
            if (!mIsPrepared)
                mMediaPlayer.prepare();
            mMediaPlayer.start();
        }
    } catch (Exception e) {
        String errorTag = "Error:";
        String errorMessage = "Music clip cannot be played.";
        Log.w(errorTag, errorMessage);
    }
}

public  void play1() {
    if (mMediaPlayer1.isPlaying())
        return;
    try {
        synchronized (this) {
            // Start the clip, preparing it if needed
            if (!mIsPrepared1)
                mMediaPlayer1.prepare();
            mMediaPlayer1.start();
        }
    } catch (Exception e) {
        String errorTag = "Error:";
        String errorMessage = "Music clip cannot be played.";
        Log.w(errorTag, errorMessage);
    }
}
/**
 * Stop the music clip.
 */
public void stop() {
    mMediaPlayer.stop();
    synchronized (this) {
        mIsPrepared = false;
    }
}
public void stop1() {
    mMediaPlayer1.stop();
    synchronized (this) {
        mIsPrepared1 = false;
    }
}

/**
 * Pause the music clip.
 */
public void pause() {
    if (mMediaPlayer.isPlaying())
        mMediaPlayer.pause();
}

public void pause1() {
    if (mMediaPlayer1.isPlaying())
        mMediaPlayer1.pause();
}

/**
 * Determine if the music clip will loop
 *
 * @param looping
 *            Boolean true to loop, false for play once.
 */
public void setLooping(boolean looping) {
    mMediaPlayer.setLooping(looping);
}

public void setLooping1(boolean looping) {
    mMediaPlayer1.setLooping(looping);
}

/**
 * Set the playback volume
 *
 * @param volume
 *            Playback volume (0-1)
 */
public void setVolume(float volume) {
    mMediaPlayer.setVolume(volume, volume);
    mMediaPlayer1.setVolume(volume, volume);
}

/**
 * Set the playback volume
 *
 * @param leftVolume
 *            Left channel playback volume (0-1)
 * @param rightVolume
 *            Right channel playback volume (0-1)
 */
public void setVolume(float leftVolume, float rightVolume) {
    mMediaPlayer.setVolume(leftVolume, rightVolume);
}

public boolean isPlaying() {
    return mMediaPlayer.isPlaying();
}
public boolean isPlaying1() {
    return mMediaPlayer1.isPlaying();
}

public boolean isLooping() {
    return mMediaPlayer.isLooping();
}
public boolean isLooping1() {
    return mMediaPlayer1.isLooping();
}


public void dispose() {
    if (mMediaPlayer.isPlaying())
        mMediaPlayer.stop();
    mMediaPlayer.release();
}

public void dispose1() {
    if (mMediaPlayer1.isPlaying())
        mMediaPlayer1.stop();
    mMediaPlayer1.release();
}

@Override
public void onCompletion(MediaPlayer player) {
    synchronized (this) {
        mIsPrepared = false;
    }
}

0 个答案:

没有答案