[Android] Fix the system media controls not working anymore when the PlaybackService is killed

Nicolas Pomepuy git at videolan.org
Wed Feb 8 14:00:30 UTC 2023


vlc-android | branch: master | Nicolas Pomepuy <nicolas at videolabs.io> | Tue Feb  7 14:29:43 2023 +0100| [de029c8d94dd9eae49dd1e783daf760124abe456] | committer: Duncan McNamara

Fix the system media controls not working anymore when the PlaybackService is killed

Fixes #2843
Fixes #2846

> https://code.videolan.org/videolan/vlc-android/commit/de029c8d94dd9eae49dd1e783daf760124abe456
---

 .../src/org/videolan/vlc/MediaBrowserInstance.kt   | 29 ++++++++++++++++++++++
 .../src/org/videolan/vlc/PlaybackService.kt        |  7 ++++++
 2 files changed, 36 insertions(+)

diff --git a/application/vlc-android/src/org/videolan/vlc/MediaBrowserInstance.kt b/application/vlc-android/src/org/videolan/vlc/MediaBrowserInstance.kt
new file mode 100644
index 0000000000..9b0fea4947
--- /dev/null
+++ b/application/vlc-android/src/org/videolan/vlc/MediaBrowserInstance.kt
@@ -0,0 +1,29 @@
+package org.videolan.vlc
+
+import android.content.ComponentName
+import android.content.Context
+import android.support.v4.media.MediaBrowserCompat
+import org.videolan.tools.SingletonHolder
+import org.videolan.vlc.MediaBrowserInstance.init
+
+object MediaBrowserInstance :
+    SingletonHolder<MediaBrowserCompat, Context>({ init(it.applicationContext) }) {
+
+    private lateinit var mediaBrowser: MediaBrowserCompat
+
+    fun init(context: Context): MediaBrowserCompat {
+        return MediaBrowserCompat(
+            context,
+            ComponentName(context, PlaybackService::class.java),
+            object : MediaBrowserCompat.ConnectionCallback() {
+                override fun onConnected() {}
+
+                override fun onConnectionSuspended() {}
+
+                override fun onConnectionFailed() {}
+            }, null
+        ).apply {
+            connect()
+        }
+    }
+}
\ No newline at end of file
diff --git a/application/vlc-android/src/org/videolan/vlc/PlaybackService.kt b/application/vlc-android/src/org/videolan/vlc/PlaybackService.kt
index 95fb8c0017..ced87dc42b 100644
--- a/application/vlc-android/src/org/videolan/vlc/PlaybackService.kt
+++ b/application/vlc-android/src/org/videolan/vlc/PlaybackService.kt
@@ -138,6 +138,12 @@ class PlaybackService : MediaBrowserServiceCompat(), LifecycleOwner, CoroutineSc
     private val mediaFactory = FactoryManager.getFactory(IMediaFactory.factoryId) as IMediaFactory
     private lateinit var carConnectionHandler:CarConnectionHandler
 
+    /**
+     * Binds a [MediaBrowserCompat] to the service to allow receiving the
+     * [MediaSessionCompat.Callback] callbacks even if the service is killed
+     */
+    lateinit var mediaBrowserCompat:MediaBrowserCompat
+
     private val receiver = object : BroadcastReceiver() {
         private var wasPlaying = false
         override fun onReceive(context: Context, intent: Intent) {
@@ -644,6 +650,7 @@ class PlaybackService : MediaBrowserServiceCompat(), LifecycleOwner, CoroutineSc
         headSetDetection.observe(this, Observer { detectHeadset(it) })
         equalizer.observe(this, Observer { setEqualizer(it) })
         serviceFlow.value = this
+        mediaBrowserCompat = MediaBrowserInstance.getInstance(this)
     }
 
     @OptIn(ObsoleteCoroutinesApi::class)



More information about the Android mailing list