背景音乐(MediaManager.createBackgroundMedia)在应用程序关闭/打开时导致崩溃

时间:2018-11-15 11:34:01

标签: android codenameone

我的应用程序(这是游戏)中的背景音乐播放出现问题,由于某种原因,如果您将应用程序从Android的近期应用程序列表中删除,则应用程序崩溃,切换到另一个应用程序可以正常工作,换句话说,如果我放了应用程序在后台运行,然后恢复运行,音乐停止并恢复(请参见下面的代码),但是如果我杀死了该应用程序,则会崩溃

据我所知,这仅发生在Android 6上,我已经在LG Nexus 5和Samsung Galaxy S7上进行了测试,有时候应用程序在被杀死时不会崩溃,但是在之后启动时却会崩溃

在Android 8上不会发生这种情况

init()

String fileName = getAppHomePath() + "bgmusic.mp3";
try {
    InputStream inputStream = Display.getInstance().getResourceAsStream(getClass(), "/bgmusic.mp3");
    OutputStream outputStream = openFileOutputStream(fileName);
    Util.copy(inputStream, outputStream);
    backgroundMedia = MediaManager.createBackgroundMedia(fileName);
    backgroundMedia.setVolume(50);
    backgroundMedia.play();
} catch(IOException e) {
    Log.e(e);
}

start()

if (backgroundMedia != null) {
    if (!backgroundMedia.isPlaying()) backgroundMedia.play();
}

stop()

if (backgroundMedia != null) {
    if (backgroundMedia.isPlaying()) backgroundMedia.pause();
}

destroy()

if (backgroundMedia != null) {
    backgroundMedia.cleanup();
}

destroy()最初没有代码,我把它放在那里,以防万一在应用被杀死时被调用,但无论如何它总是崩溃

我还从设备获取了(DDMS)日志

11-15 14:51:42.431: E/ActivityThread(4952): Activity com.manyukhin.cerebrate.words.synonyms.WordsSynonymsStub has leaked ServiceConnection com.codename1.impl.android.e$3@df3ed35 that was originally bound here
11-15 14:51:42.431: E/ActivityThread(4952): android.app.ServiceConnectionLeaked: Activity com.manyukhin.cerebrate.words.synonyms.WordsSynonymsStub has leaked ServiceConnection com.codename1.impl.android.e$3@df3ed35 that was originally bound here
11-15 14:51:42.431: E/ActivityThread(4952):     at android.app.LoadedApk$ServiceDispatcher.<init>(LoadedApk.java:1092)
11-15 14:51:42.431: E/ActivityThread(4952):     at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:986)
11-15 14:51:42.431: E/ActivityThread(4952):     at android.app.ContextImpl.bindServiceCommon(ContextImpl.java:1303)
11-15 14:51:42.431: E/ActivityThread(4952):     at android.app.ContextImpl.bindService(ContextImpl.java:1286)
11-15 14:51:42.431: E/ActivityThread(4952):     at android.content.ContextWrapper.bindService(ContextWrapper.java:604)
11-15 14:51:42.431: E/ActivityThread(4952):     at com.codename1.impl.android.e.g(AndroidImplementation.java:3133)
11-15 14:51:42.431: E/ActivityThread(4952):     at com.codename1.o.q.d(Display.java:4152)
11-15 14:51:42.431: E/ActivityThread(4952):     at com.codename1.media.c.a(MediaManager.java:65)
11-15 14:51:42.431: E/ActivityThread(4952):     at com.manyukhin.cerebrate.words.synonyms.v.a(Unknown Source)
11-15 14:51:42.431: E/ActivityThread(4952):     at com.manyukhin.cerebrate.words.synonyms.WordsSynonymsStub.a(WordsSynonymsStub.java:133)
11-15 14:51:42.431: E/ActivityThread(4952):     at com.manyukhin.cerebrate.words.synonyms.WordsSynonymsStub$1.run(WordsSynonymsStub.java:114)
11-15 14:51:42.431: E/ActivityThread(4952):     at com.codename1.o.q.m(Display.java:1137)
11-15 14:51:42.431: E/ActivityThread(4952):     at com.codename1.o.q.j(Display.java:932)
11-15 14:51:42.431: E/ActivityThread(4952):     at com.codename1.o.ai.run(RunnableWrapper.java:120)
11-15 14:51:42.431: E/ActivityThread(4952):     at com.codename1.impl.b$1.run(CodenameOneThread.java:60)
11-15 14:51:42.431: E/ActivityThread(4952):     at java.lang.Thread.run(Thread.java:818)
11-15 14:51:42.548: E/PBSessionCacheImpl(3716): sessionId[16344683113135691] not persisted.
11-15 14:51:42.628: E/ACDB-LOADER(198): Error: ACDB AudProc vol returned = -19
11-15 14:51:43.635: E/AndroidRuntime(5154): FATAL EXCEPTION: main
11-15 14:51:43.635: E/AndroidRuntime(5154): Process: com.manyukhin.cerebrate.words.synonyms, PID: 5154
11-15 14:51:43.635: E/AndroidRuntime(5154): java.lang.RuntimeException: Unable to start service com.codename1.media.AudioService@134375 with null: java.lang.NullPointerException: Attempt to invoke virtual method 'android.os.Bundle android.content.Intent.getExtras()' on a null object reference
11-15 14:51:43.635: E/AndroidRuntime(5154):     at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3027)
11-15 14:51:43.635: E/AndroidRuntime(5154):     at android.app.ActivityThread.-wrap17(ActivityThread.java)
11-15 14:51:43.635: E/AndroidRuntime(5154):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1442)
11-15 14:51:43.635: E/AndroidRuntime(5154):     at android.os.Handler.dispatchMessage(Handler.java:102)
11-15 14:51:43.635: E/AndroidRuntime(5154):     at android.os.Looper.loop(Looper.java:148)
11-15 14:51:43.635: E/AndroidRuntime(5154):     at android.app.ActivityThread.main(ActivityThread.java:5417)
11-15 14:51:43.635: E/AndroidRuntime(5154):     at java.lang.reflect.Method.invoke(Native Method)
11-15 14:51:43.635: E/AndroidRuntime(5154):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
11-15 14:51:43.635: E/AndroidRuntime(5154):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
11-15 14:51:43.635: E/AndroidRuntime(5154): Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.os.Bundle android.content.Intent.getExtras()' on a null object reference
11-15 14:51:43.635: E/AndroidRuntime(5154):     at com.codename1.media.AudioService.onStartCommand(AudioService.java:59)
11-15 14:51:43.635: E/AndroidRuntime(5154):     at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3010)
11-15 14:51:43.635: E/AndroidRuntime(5154):     ... 8 more

据我所知createBackgroundMedia()启动Service,然后在应用程序被杀死时不停止它,有什么解决方法吗?

编辑

在Android 4.3、5.0和6.0上已确认

问题

我将代码更改为以下内容:

start()

String fileName = getAppHomePath() + "bgmusic.mp3";
try {
    InputStream inputStream = Display.getInstance().getResourceAsStream(getClass(), "/bgmusic.mp3");
    OutputStream outputStream = openFileOutputStream(fileName);
    Util.copy(inputStream, outputStream);
    backgroundMedia = MediaManager.createBackgroundMedia(fileName);
    backgroundMedia.setVolume(50);
    backgroundMedia.play();
} catch(IOException e) {
    Log.e(e);
}    

stop()

if (backgroundMedia != null) {
    backgroundMedia.cleanup();
}

我认为这会有所帮助,因为backgroundMedia.cleanup()应该停止播放并释放包括后台服务在内的所有相关资源,但这没有帮助,问题仍然存在

0 个答案:

没有答案