MediaPlayer#Pause

时间:2018-06-29 14:34:12

标签: java android android-mediaplayer illegalstateexception

我在Google控制台和启动前报告中上传了我的应用程序,仅在2台设备中出现了此问题 java.lang.IllegalStateException

FATAL EXCEPTION: ControllerMessenger
Process: com.wolframite.manos.crack_the_code, PID: 11744
java.lang.IllegalStateException
at android.media.MediaPlayer.isPlaying(Native Method)
at com.wolframite.manos.crack_the_code.Music$1.onReceive(Music.java:36)
at android.support.v4.content.LocalBroadcastManager.executePendingBroadcasts(LocalBroadcastManager.java:311)
at android.support.v4.content.LocalBroadcastManager.access$000(LocalBroadcastManager.java:47)
at android.support.v4.content.LocalBroadcastManager$1.handleMessage(LocalBroadcastManager.java:120)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.support.test.espresso.base.Interrogator.a(Interrogator.java:19)
at android.support.test.espresso.base.UiControllerImpl.a(UiControllerImpl.java:142)
at android.support.test.espresso.base.UiControllerImpl.a(UiControllerImpl.java:134)
at android.support.test.espresso.base.UiControllerImpl.a(UiControllerImpl.java:34)
at android.support.test.espresso.action.MotionEvents.a(MotionEvents.java:74)
at android.support.test.espresso.action.MotionEvents.a(MotionEvents.java:52)
at android.support.test.espresso.action.Tap.c(Tap.java:9)
at android.support.test.espresso.action.Tap.a(Tap.java:19)
at android.support.test.espresso.action.Tap$1.b(Tap.java:2)
at android.support.test.espresso.action.GeneralClickAction.perform(GeneralClickAction.java:22)
at android.support.test.espresso.ViewInteraction$SingleExecutionViewAction.perform(ViewInteraction.java:9)
at android.support.test.espresso.ViewInteraction.a(ViewInteraction.java:78)
at android.support.test.espresso.ViewInteraction.a(ViewInteraction.java:94)
at android.support.test.espresso.ViewInteraction$1.call(ViewInteraction.java:3)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.Handler.handleCallback(Handler.java:746)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5459)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)

我不知道该怎么办。 这是什么问题,我还需要上传更多内容来帮助解决此问题。

当第36行是player.pause()时,当应用程序进入后台停止播放音乐并且当应用程序进入前台恢复播放音乐时,该音乐类用于在应用程序中播放音乐并接收广播。

public class Music extends Service {
private MediaPlayer player;

@Override
public IBinder onBind(Intent intent) {
    return null;
}
public void onCreate() {
    player = MediaPlayer.create(this, R.raw.music);
    player.setLooping(true);
    LocalBroadcastManager.getInstance(this).registerReceiver(StateReceiver, new IntentFilter("status"));

}
private final BroadcastReceiver StateReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        String status = intent.getStringExtra("status");
        if (parseInt(String.valueOf(status)) == 0) {
            player.pause();
        } else if (parseInt(String.valueOf(status)) == 1) {
            if (player != null)
                player.start();
            else {
                player = MediaPlayer.create(Music.this, R.raw.music);
                player.setLooping(true);
                player.start();
            }
        } else if(player != null){
            player.stop();
            player.release();
        }

    }
};
public int onStartCommand(Intent intent, int flags, int startId) {
    player.start();
    return Service.START_NOT_STICKY;
}
public void onDestroy() {
    player.stop();
    player.release();
    LocalBroadcastManager.getInstance(this).unregisterReceiver(StateReceiver);
    stopSelf();
    super.onDestroy();

}

1 个答案:

答案 0 :(得分:2)

为了避免这些类型的错误,您可以创建一个实用程序类,该实用程序类充当MediaPlayer方法周围的安全包装,有时会引发意外的异常并使您的应用程序崩溃。

我在名为MediaPlayerUtils的类中使用了代码,例如以下示例,以避免这些类型的行为,并为我的应用程序增加了一层安全性:

public static void pause(MediaPlayer mediaPlayer) {
    if (mediaPlayer != null) {
        try {
            if (mediaPlayer.isPlaying()) {
                mediaPlayer.pause();
            }
        } catch (Exception e) {
            Log.w(MediaPlayerUtil.class.getName(),
                    String.format("Failed to stop media player: %s", e));
        }
    }
}

然后您必须使用代替:

player.pause(); // not safe

此代码:

MediaPlayerUtils.pause(player);  // safe

您可以为MediaPlayer.stop()添加类似的方法,例如:;

public static void stop(MediaPlayer mediaPlayer) {
    if (mediaPlayer != null) {
        try {
            if (mediaPlayer.isPlaying()) {
                mediaPlayer.stop();
            }
            resetRelease(mediaPlayer);
        } catch (Exception e) {
            Log.e(MediaPlayerUtil.class.getName(),
                    String.format("Failed to stop media player: %s", e));
        }
    }
}