Background music (MediaManager.createBackgroundMedia) causes crashes at app closing/opening

梦想与她 提交于 2020-01-14 04:16:08

问题


I have an issue with background music playback in my app (it's a game), for some reason app crashes if you remove app from Android's recent apps list, switching to another app works fine, in other words, if I put app in background and then resume, it works, music stops and resumes (please see the code below), but if I kill the app then it crashes

As far as I know this only happens on Android 6, I have tested on LG Nexus 5 and Samsung Galaxy S7, sometimes app doesn't crash when you kill it, but does crash when you start it after

This doesn't happen on 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();
}

initially there was no code in destroy(), I put it there just in case it gets called at the moment app gets killed, but it keeps crashing anyway

I also got (DDMS) logs from device

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

As I understood createBackgroundMedia() starts Service, and then doesn't stops it when app gets killed, is there any workaround for that ?

EDIT

issue confirmed on Android 4.3, 5.0 and 6.0

I have changed the code to the following:

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();
}

I thought that this will help, as backgroundMedia.cleanup() should stop playback and free-up all related resources including background service, but this didn't help, issue is still there

来源:https://stackoverflow.com/questions/53318556/background-music-mediamanager-createbackgroundmedia-causes-crashes-at-app-clos

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!