我愿意接受建议。我正在编写一个应用程序,该应用程序需要在应用程序的所有活动之间播放音乐,并在用户点击“主页”或通过回击并退出我的应用程序时“退出”应用程序时暂停。所以这就是我所做的——我创建了一个包含 MediaPlayer 静态实例的“SoundController”类,如下所示:
private static MediaPlayer _musicPlayer;
public static void init(Context ctx){
_musicPlayer = new MediaPlayer();
AssetFileDescriptor descriptor;
try {
descriptor = ctx.getAssets().openFd("something.ogg");
_musicPlayer.setDataSource(descriptor.getFileDescriptor(), descriptor.getStartOffset(), descriptor.getLength());
_musicPlayer.prepare();
_musicPlayer.setLooping(true);
} catch (IOException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
}
}
我在我的应用程序中对每个活动的“onResume()”和“onPause()”调用“.start()”和“.pause()”,如下所示:
@Override
public void onResume(){
System.out.println("got in onResume for mainActivity");
SoundController.playMusic();
super.onResume();
}
@Override
public void onPause(){
System.out.println("got in onPause for mainActivity");
SoundController.pauseMusic();
super.onPause();
}
@Override
public void onDestroy(){
System.out.println("got in onDestroy for mainActivity");
SoundController.tearDown();
super.onDestroy();
}
SoundController 方法如下:
public static void playMusic(){
if(!_musicPlayer.isPlaying()){
_musicPlayer.start();
}
}
public static void pauseMusic(){
if(_musicPlayer.isPlaying()){
_musicPlayer.pause();
}
}
public static void tearDown(){
if(_musicPlayer.isPlaying()){
_musicPlayer.stop();
_musicPlayer.release();
}
}
但由于某种原因,我注意到几个错误,例如:
- 即使在应用程序退出后音乐也会继续播放,如果我做的事情足够快,比如我启动应用程序,很快就回家,然后再次启动应用程序,然后按“返回”键退出应用程序......
- 该应用程序在尝试退出时崩溃,因为出于某种原因,onPause() 在 onDestroy().... 之后被调用。(我将它记录在我的 logcat 中,在 kindle fire 上)
是不是我的媒体播放器的静态实例在我的应用程序被销毁之前就以某种方式被破坏了?
在我的应用程序退出后音乐继续播放很长时间的情况下,“_mediaPlayer.start()”是否有延迟启动,所以它绕过了应用程序退出时的检查?
对不起,很长的帖子。非常感谢!!!
==================================================== =====
编辑这里的崩溃日志:
09-12 14:44:06.304: I/System.out(12191): got in onPause for mainActivity
09-12 14:44:06.366: I/System.out(12191): got in onResume for mainActivity
09-12 14:44:06.726: I/System.out(12191): got in onDestroy for mainActivity
09-12 14:44:07.499: I/System.out(12191): got in onPause for mainActivity
09-12 14:44:07.507: D/AndroidRuntime(12191): Shutting down VM
09-12 14:44:07.507: W/dalvikvm(12191): threadid=1: thread exiting with uncaught exception (group=0x40015560)
09-12 14:44:07.507: E/AndroidRuntime(12191): FATAL EXCEPTION: main
09-12 14:44:07.507: E/AndroidRuntime(12191): java.lang.RuntimeException: Unable to pause activity {com.blah/com.blah.mainActivity}: java.lang.IllegalStateException
09-12 14:44:07.507: E/AndroidRuntime(12191): at android.app.ActivityThread.performPauseActivity(ActivityThread.java:2354)
09-12 14:44:07.507: E/AndroidRuntime(12191): at android.app.ActivityThread.performPauseActivity(ActivityThread.java:2311)
09-12 14:44:07.507: E/AndroidRuntime(12191): at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:2291)
09-12 14:44:07.507: E/AndroidRuntime(12191): at android.app.ActivityThread.access$1700(ActivityThread.java:117)
09-12 14:44:07.507: E/AndroidRuntime(12191): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:942)
09-12 14:44:07.507: E/AndroidRuntime(12191): at android.os.Handler.dispatchMessage(Handler.java:99)
09-12 14:44:07.507: E/AndroidRuntime(12191): at android.os.Looper.loop(Looper.java:130)
09-12 14:44:07.507: E/AndroidRuntime(12191): at android.app.ActivityThread.main(ActivityThread.java:3683)
09-12 14:44:07.507: E/AndroidRuntime(12191): at java.lang.reflect.Method.invokeNative(Native Method)
09-12 14:44:07.507: E/AndroidRuntime(12191): at java.lang.reflect.Method.invoke(Method.java:507)
09-12 14:44:07.507: E/AndroidRuntime(12191): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:850)
09-12 14:44:07.507: E/AndroidRuntime(12191): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:608)
09-12 14:44:07.507: E/AndroidRuntime(12191): at dalvik.system.NativeStart.main(Native Method)
09-12 14:44:07.507: E/AndroidRuntime(12191): Caused by: java.lang.IllegalStateException
09-12 14:44:07.507: E/AndroidRuntime(12191): at android.media.MediaPlayer.isPlaying(Native Method)
09-12 14:44:07.507: E/AndroidRuntime(12191): at com.blah.controllers.SoundController.pauseMusic(SoundController.java:63)
09-12 14:44:07.507: E/AndroidRuntime(12191): at com.blah.mainActivity.onPause(mainActivity.java:53)
09-12 14:44:07.507: E/AndroidRuntime(12191): at android.app.Activity.performPause(Activity.java:3887)
09-12 14:44:07.507: E/AndroidRuntime(12191): at android.app.Instrumentation.callActivityOnPause(Instrumentation.java:1191)
09-12 14:44:07.507: E/AndroidRuntime(12191): at android.app.ActivityThread.performPauseActivity(ActivityThread.java:2341)
09-12 14:44:07.507: E/AndroidRuntime(12191): ... 12 more