0
メインメニューに戻るために戻るボタンを押したときに音楽が停止する問題があります。ゲームは10回中に9回クラッシュし、以下に投稿するIllegalStateExceptionを返します。音楽はGameScreenで初期化され、GameScreenのonBackPressedメソッドで処理しようとするとクラッシュします。ここに私のMusicManagerクラスとGameScreenの重要な部分があります。どんな助けでも大歓迎です! onBackPressed時にMediaPlayerを停止できません
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);
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;
}
}
...
@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)
ありがとうございました。 –
@HarryReidうれしい –