我在谷歌控制台和发布前报告中上传了我的应用程序,仅在 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();
}
为了避免这些类型的错误,你可以创建一个实用程序类,它充当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
你可以将类似的方法添加到 forMediaPlayer.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));
}
}
}