Skip to content

How to stop the service and remove the notification when connected with MediaController? #73

Closed
@Waldmann375

Description

@Waldmann375

I mean, release the MediaSession, stop the MediaSessionService and remove the notification.

The problem is that just calling stopService(intent) and releasing the MediaController from the activity does not work. I suspect it's because the service keeps being used by the MediaSession.

I know that I should release the mediasession, but the mediasession belongs to the sevice, which I can't control from Activity.
I can't even get into the service's onDestroy callback to release the mediasession.

So my current workaround is to release the MediaController in activity's onDestroy(), so the process stops by a fatal crash:

E/AndroidRuntime: FATAL EXCEPTION: main
    java.lang.NullPointerException: Attempt to invoke interface method 'void androidx.media3.session.IMediaSession.flushCommandQueue(androidx.media3.session.IMediaController)' on a null object reference
        at androidx.media3.session.MediaControllerImplBase$FlushCommandQueueHandler.handleMessage(MediaControllerImplBase.java:3041)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:255)
        at android.app.ActivityThread.main(ActivityThread.java:8212)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:632)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1049)

It does the job, but I believe there should be a better way for such a simple task.

Metadata

Metadata

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions