-
Notifications
You must be signed in to change notification settings - Fork 409
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
MediaSessionService never destroyed #389
Comments
This issue also exists in media3 1.1.0-alpha01. NB I use a custom player based on SimpleBasePlayer. Also, I see that MediaSession.Callback onConnect() and onPostConnect() are called twice for some reason, within a few milliseconds. This may not be related, the double call also occurs when there is no player interaction, and in this case onDestroy() is called. I create the Controller just once, so this looks like a (separate?) bug. |
This starts with |
@graymind75 Thanks for the link. I'm not entirely sure it's the same issue. There is no visible notification in my case after controller release. Also, just to clarify, all interaction in my case comes from the controller, the user does not interact with the notification. |
Then again, just hypothesizing, perhaps one of those duplicate onConnect() calls is coming from the notification? (I just use the default notification mechanism, zero customization.) They look very similar, same package, same UID (10159), but small difference in connectionHints. I'm attaching debugger screenshots for the 2 ControllerInfo objects, in onConnect() call order. There is only a single onDisconnected() call. (Using media3 1.1.0-alpha01) |
Thanks for your report. I'll look into this more closely next week. Just writing to let you quickly know about the reason of the second connect: it comes from an internal controller in |
Just another note :) Once the player is playing, the service starts into the foreground. Once the service is started (in the foreground), the system doesn't stop the service anymore, instead the app needs to stop the service. https://developer.android.com/guide/components/services#Stopping So if you at the moment when you release the session and player (step 5 above) call |
@marcbaechinger that did the trick, thanks. Quite the hack. Hope you guys will be able to improve the API on that front. Will have to think about how to best incorporate this cleanly. I'm very familiar with service lifecycles. Media3 seems to take care of the grunt work for me, it's surprising that it does not handle stopping the service. I just hope I won't have to recreate the whole service start/stop/foreground mess myself again, it required some delicate logic in my MediaBrowserServiceCompat version. |
What MediaSessionService documentation says:
The "released" link points at |
@marcbaechinger I am curious about plans to address this issue, so I can move forward with my implementation. Current behavior is a violation of the documented API contract. Alternatives I see:
|
Thanks for your reasoning. We are thinking about what the best option would be. It's good to know the requirements for your use case. We will have to think about other use cases as well. I don't think we can have a one-size-fit-all solution I'm afraid so it will probably be some modes, or similar that a developer can choose when building/creating the session/service. Not sure about details now. We are currently in the design stage of this. Currently, as in with the current version 1.0.2, an app needs to stop the service itself as you already mentioned. Please note that the internal controller connects to the session, but does not bind to the service. This is a small but important detail. Further, on API levels where System UI does not connect to the session (I think from top of my head this is below API 29), we can not simply stop the service when all controller disconnect because apps that want to keep the service playing when the activity is removed, need a way where the service keeps running in the foreground without any controllers connected,. These are some cases that we need to take into account as well. To not remove any of these options for apps, the Media3 library does currently neither stop the service nor does it disconnect any controllers or release a session automatically. However, I agree with you that we can take some burden from developer if we have some life-cycle modes that developers can opt-in to make Media3 handle some common cases. Besides this there will be, probably by default, a manual mode that keeps the current behavior. Many thanks again for your contribution and the detailed reasoning. That's very valuable for us. We will keep you posted when we push some related changes. |
I forgot:
We will mark this controller and document this properly. Thanks for raising awareness that this is difficult to identify. |
@marcbaechinger Thank you for adding all that detail. For the time being I may indeed try to keep the service started until I know I really will not not need it again, and then stop it at that time (plus listen for trimMemory, etc.). My preferences here have evolved over the course of our discussion. I actually might choose to ensure it will indeed be started by always issuing a prepare() or such, rather than letting this depend on "meaningful user interaction" like play/pause. Which seems to point at a certain Service/session start/stop responsibility misalignment again. I know exactly when I want the Service and the session to start and when I want them to stop. But it's really the media API that starts everything, sometimes, and stops some things, at some other times. I want to control starting and stopping everything, always, so I think I need to carefully negotiate with the media API about both starting and stopping. Unless I decide to assume full control of all this (notifications, service start/stop, foregrounding) myself again, which I don't really want, either. |
The `MediaNotificationManager` registers an internal controller to each session. This change marks this controller through its connection hints and provides an API for apps to hide implementation details of the marking. Issue: #389 PiperOrigin-RevId: 549712768
I have just run into this issue myself. I am glad to read here confirmation that |
True! Sorry to make you dig. I updated the documentation. I added a section about implementing the service lifecycle on the DAC page about 'Background playback with a MediaSessionService'. This includes calling |
I have the opposite problem. I am not able to continue playing when the app is swiped away.
My MainAcitivty:
Whenever I close the app, playback stops and the notification disappears. How do I keep the player running in the background? |
@colorgold: i suggest you file a new issue, to avoid this one getting too confusing, since you might need an opposite resolution for an opposite problem. |
@marcbaechinger Is there any workaround to solve this problem? In |
Media3 Version
Media3 1.0.1
Devices that reproduce the issue
Android emulator API 33
Devices that do not reproduce the issue
No response
Reproducible in the demo app?
Not tested
Reproduction steps
I first mentioned this issue in #346.
MediaSession.Callback.onDisconnected()
Notably this issue does not occur when there is no player interaction, i.e. Player and MediaSession created but not really used, e.g. no call to prepare().
Expected result
MediaSessionService.onDestroy() called shortly after MediaController released.
Actual result
MediaSessionService.onDestroy() never called.
Media
N/A
Bug Report
adb bugreport
to dev.exoplayer@gmail.com after filing this issue.The text was updated successfully, but these errors were encountered: