Closed yanivshaked closed 2 years ago
Setting the state to AudioProcessingState.idle
is what you want to do to stop the service, this will work in onTaskRemoved
. The idea is that when you call stop
on just_audio's player, the state of it is broadcasted to service's state, essentially stopping it.
I agree this is currently far from being clear, which is why we have https://github.com/ryanheise/audio_service/issues/655 to document this better.
Looking at the code, calling BaseAudioHandler.stop()
sets the state to AudioProcessingState.idle
:
playbackState.add(playbackState.nvalue!
.copyWith(processingState: AudioProcessingState.idle));
I have tried the following to stop the audio service:
BaseAudioHandler.stop()
when application lifecycle state has changed to AppLifecycleState.detached
BaseAudioHandler.stop()
from onTaskRemoved
.
In both cases, the audio stops but the audio service is still running. When trying to launch the application again, it crashes with the following error:
E/AndroidRuntime(13736): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.twtech.twelveqa/com.ryanheise.audioservice.AudioServiceActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'io.flutter.plugin.common.BinaryMessenger io.flutter.embedding.engine.plugins.FlutterPlugin$FlutterPluginBinding.getBinaryMessenger()' on a null object reference
E/AndroidRuntime(13736): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3622)
E/AndroidRuntime(13736): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3786)
E/AndroidRuntime(13736): at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
E/AndroidRuntime(13736): at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
E/AndroidRuntime(13736): at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
E/AndroidRuntime(13736): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2257)
E/AndroidRuntime(13736): at android.os.Handler.dispatchMessage(Handler.java:106)
E/AndroidRuntime(13736): at android.os.Looper.loop(Looper.java:233)
E/AndroidRuntime(13736): at android.app.ActivityThread.main(ActivityThread.java:8030)
E/AndroidRuntime(13736): at java.lang.reflect.Method.invoke(Native Method)
E/AndroidRuntime(13736): at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:631)
E/AndroidRuntime(13736): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:978)
E/AndroidRuntime(13736): Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'io.flutter.plugin.common.BinaryMessenger io.flutter.embedding.engine.plugins.FlutterPlugin$FlutterPluginBinding.getBinaryMessenger()' on a null object reference
E/AndroidRuntime(13736): at com.crazecoder.openfile.OpenFilePlugin.onAttachedToActivity(OpenFilePlugin.java:400)
E/AndroidRuntime(13736): at io.flutter.embedding.engine.FlutterEngineConnectionRegistry.attachToActivityInternal(FlutterEngineConnectionRegistry.java:362)
E/AndroidRuntime(13736): at io.flutter.embedding.engine.FlutterEngineConnectionRegistry.attachToActivity(FlutterEngineConnectionRegistry.java:344)
E/AndroidRuntime(13736): at io.flutter.embedding.android.FlutterActivityAndFragmentDelegate.onAttach(FlutterActivityAndFragmentDelegate.java:176)
E/AndroidRuntime(13736): at io.flutter.embedding.android.FlutterActivity.onCreate(FlutterActivity.java:430)
E/AndroidRuntime(13736): at android.app.Activity.performCreate(Activity.java:8006)
E/AndroidRuntime(13736): at android.app.Activity.performCreate(Activity.java:7990)
E/AndroidRuntime(13736): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1329)
E/AndroidRuntime(13736): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3595)
E/AndroidRuntime(13736): ... 11 more
E/sqlite3_android(13736): ONEPLUS_NAME_PARTS_MATCH SQLITE_OK
D/TransportRuntime.SQLiteEventStore(13736): Storing event with priority=HIGHEST, name=FIREBASE_CRASHLYTICS_REPORT for destination cct
D/TransportRuntime.JobInfoScheduler(13736): Scheduling upload for context TransportContext(cct, HIGHEST, MSRodHRwczovL2NyYXNobHl0aWNzcmVwb3J0cy1wYS5nb29nbGVhcGlzLmNvbS92MS9maXJlbG9nL2xlZ2FjeS9iYXRjaGxvZ1xBSXphU3lCcnBTWVQ0RkZMMDlyZUhKaTZIOUZZZGVpU25VVE92Mk0=) with jobId=-1042206307 in 1000ms(Backend next call timestamp 1635765832535). Attempt 1
I/Process (13736): Sending signal. PID: 13736 SIG: 9
After the crash, I can start the application.
This seems another issue, can you file a new issue for that, following the bug template?
Sure, closing this one in favor of a bug report
@nt4f04uNd Unfortunately, I am unable to create a minimal reproduction project for this issue, so I am unable to submit a new bug template. What do you suggest?
What plugins are you using in your project?
@yanivshaked a good first attempt to build a repro project is to just fork this project and modify the example by adding all of your dependencies to it. Sometimes even the presence of those dependencies will create the right conditions to reproduce the bug.
Many plugins. I've just tried adding all of my plugins to the example project, but it still works.
Unfortunately in that case, you might want to try debugging the open_file plugin yourself (which is the plugin that is actually crashing, according to your error):
at com.crazecoder.openfile.OpenFilePlugin.onAttachedToActivity(OpenFilePlugin.java:400)
So what exactly is on line 400 of OpenFilePlugin.java
?
Bravo!! I missed that. Seems like this issue is referenced and solved here: https://github.com/crazecoder/open_file/issues/162
Testing now.
It works 👍 Still, the application misbehaves; Once I'm closing it from the background, I would expect it to "cleanly" start when launching it again. Instead, it returns to the last page I was viewing on the application before closing it. That gives extra weight to my original feature request, how to completely stop the audio service from running in background?
I think @nt4f04uNd 's first response is appropriate here, and i would suggest thumbing up that other issue to improve the documentation.
I have followed @nt4f04uNd response and set the state to AudioProcessingState.idle
but problem remains.
Just to make sure we are on the same page here, following is the scenario:
onTaskRemoved
I'm calling BaseAudioHandler.stop()
which sets the state to AudioProcessingState.idle
.Is this behavior the expected behavior? I would expect the application to start from scratch.
Hi @yanivshaked there are more variables in play that influence the behaviour than what you have described above so this sort of thing is difficult to answer without a repro case. But I would say in general that what is defined as "expected" is what the documentation says. I wouldn't want to get into what's expected without having this explicit in the documentation, and hence:
Then I can get to work either implementing the new behaviour or correcting existing behaviour.
The documentation is clear to me now (setting state to AudioProcessingState.idle
to remove player from notification), and it doesn't support the behavior I expect.
I can easily create a reproduction project for the above scenario.
I am not sure about semantics, since the original feature request (this issue) is to the best of my understanding what must be done to support the expected behavior.
I think a new issue is better because technically,
Feature proposal
API to stop the audio service from running, once the user closes the application from background. Using
onTaskRemoved
to stop the audio is not enough, I would like to completely remove the audio service.Motivating use case(s)
Application crashes when trying to launch it again after closure from background if audio service is still running.
In a new issue, you may be able to better describe the motivating scenario of not retaining the main activity's state after re-entering the main activity after the task was removed.
Ack
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs, or use StackOverflow if you need help with audio_service.
Feature proposal
API to stop the audio service from running, once the user closes the application from background. Using
onTaskRemoved
to stop the audio is not enough, I would like to completely remove the audio service.Motivating use case(s)
Application crashes when trying to launch it again after closure from background if audio service is still running.