无法停止媒体播放器时按下后退



当我按下后退按钮返回主菜单时,我在让音乐停止时遇到了一些困难。该游戏在10中崩溃了9次,并返回了IllegalstateException,我将在下面发布。音乐是在游戏屏幕中初始化的,当我尝试以onbackspersed方法将其崩溃时,它会崩溃。这是我的MusicManager课程和游戏屏幕的重要部分。任何帮助将不胜感激!

public class MusicManager implements MediaPlayer.OnCompletionListener{
AudioManager amg;
private MediaPlayer mMediaPlayer;
private MediaPlayer mMediaPlayer1;
private String mAssetFile;
private boolean mIsPrepared = false;
private boolean mIsPrepared1 = false;
private ScreenManager mScreenManager;
public MusicManager(Game game, ScreenManager mScreenManager) {
    super();
mScreenManager = new ScreenManager();
    mMediaPlayer = new MediaPlayer();
    mMediaPlayer1 = new MediaPlayer();
    try{            AssetManager assetManager = game.getActivity().getAssets();
        AssetFileDescriptor assetDescriptor = assetManager.openFd("Sounds/GameScreen1.ogg");
        AssetFileDescriptor assetDescriptor1 = assetManager.openFd(("Sounds/Game-Menu.ogg"));
            //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) {
            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) {
            if (!mIsPrepared1)
                mMediaPlayer1.prepare();
                mMediaPlayer1.start();
        }
    } catch (Exception e) {
        String errorTag = "Error:";
        String errorMessage = "Music clip1 cannot be played.";
        Log.w(errorTag, errorMessage);
    }
}
public void stop() {
    mMediaPlayer.stop();
    synchronized (this) {
        mIsPrepared = false;
    }
}
public void stop1() {
    mMediaPlayer1.stop();
    synchronized (this) {
        mIsPrepared1 = false;
    }
}
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 MediaPlayer MediaPlayer(){
    return mMediaPlayer;
}
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);
}
public void setVolume1(float 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;
    }
}

GamesCreen

public class CardDemoGameScreen extends GameScreen {
 private SoundManager mSoundManager;
private MusicManager mMusicManager;
private ScreenManager mScreenManager;
private MediaPlayer mediaPlayer;
public CardDemoGameScreen(Game game) {
    super("CardDemoGameScreen", game);
    mediaPlayer = new MediaPlayer();
    mSoundManager = new SoundManager(game);
    mScreenManager = new ScreenManager();
    mMusicManager = new MusicManager(game, mScreenManager);
 public void update(ElapsedTime elapsedTime) {
    //NOTE: elapsed time has time between frames AND time since program 
started
    mMusicManager.play();
   // mMusicManager.isPlaying();
    mMusicManager.setLooping(true);

        mPlayer.update(elapsedTime);
        mPlayer.updateLayer(mLayerViewport, 0, LEVEL_WIDTH, 0, 
LEVEL_HEIGHT);
        mPlayer.HandleTouch(mScreenViewport, mLayerViewport, worldZoom);
        mPlayer.updateHandLayout(LEVEL_WIDTH, LEVEL_HEIGHT);

...

 @Override
public boolean onBackPressed() {
   mMusicManager.dispose();
    return true;
}

stacktrace

05-06 20:04:44.371 30964-31104/uk.ac.qub.eeecs.gage E/AndroidRuntime: FATAL EXCEPTION: Thread-2901
                                                                  Process: uk.ac.qub.eeecs.gage, PID: 30964
                                                                  java.lang.IllegalStateException
                                                                      at android.media.MediaPlayer.isPlaying(Native Method)
                                                                      at uk.ac.qub.eeecs.game.cardDemo.MusicManager.play(MusicManager.java:64)
                                                                      at uk.ac.qub.eeecs.game.cardDemo.CardDemoGameScreen.update(CardDemoGameScreen.java:177)
                                                                      at uk.ac.qub.eeecs.gage.Game.doUpdate(Game.java:330)
                                                                      at uk.ac.qub.eeecs.gage.Game.access$200(Game.java:26)
                                                                      at uk.ac.qub.eeecs.gage.Game$GameLoop.run(Game.java:527)
                                                                      at java.lang.Thread.run(Thread.java:818)

请更改这样的处理方法,并捕获一些例外:并尝试将mMediaPlayer.release()更改为reset()

public void dispose() {
if (mMediaPlayer != null) {
    if (mMediaPlayer.isPlaying()) {
        try {
            //mMediaPlayer1.stop();
            //mMediaPlayer.release()
            mMediaPlayer.reset();
        } catch (Exception e) {
            e.printStackTrace();
          }
       }
    }
}

相关内容

  • 没有找到相关文章

最新更新