From 4f14b6af3b997da3a7a59318e2855c4f4ab2b156 Mon Sep 17 00:00:00 2001 From: Tom Van Laerhoven Date: Fri, 2 Aug 2024 15:00:49 +0200 Subject: [PATCH 1/5] Drop need for MediaBrowserService --- .../theoplayer/media/MediaPlaybackService.kt | 47 +------------------ 1 file changed, 1 insertion(+), 46 deletions(-) diff --git a/android/src/main/java/com/theoplayer/media/MediaPlaybackService.kt b/android/src/main/java/com/theoplayer/media/MediaPlaybackService.kt index e6dd91e69..62a014e81 100644 --- a/android/src/main/java/com/theoplayer/media/MediaPlaybackService.kt +++ b/android/src/main/java/com/theoplayer/media/MediaPlaybackService.kt @@ -6,16 +6,12 @@ import android.content.pm.ServiceInfo import android.graphics.Bitmap import android.os.Binder import android.os.Build -import android.os.Bundle import android.os.IBinder -import android.support.v4.media.MediaBrowserCompat import android.support.v4.media.session.MediaSessionCompat import android.support.v4.media.session.PlaybackStateCompat -import android.text.TextUtils import android.util.Log import androidx.core.app.ServiceCompat import androidx.core.content.ContextCompat -import androidx.media.MediaBrowserServiceCompat import androidx.media.session.MediaButtonReceiver import com.theoplayer.BuildConfig import com.theoplayer.ReactTHEOplayerContext @@ -23,15 +19,13 @@ import com.theoplayer.android.api.player.Player import com.theoplayer.android.connector.mediasession.MediaSessionConnector import com.theoplayer.android.connector.mediasession.MediaSessionListener -private const val BROWSABLE_ROOT = "/" -private const val EMPTY_ROOT = "@empty@" private const val STOP_SERVICE_IF_APP_REMOVED = true private const val NOTIFICATION_ID = 1 private const val TAG = "MediaPlaybackService" -class MediaPlaybackService : MediaBrowserServiceCompat() { +class MediaPlaybackService : Service() { private lateinit var notificationManager: NotificationManager private lateinit var notificationBuilder: MediaNotificationBuilder @@ -175,9 +169,6 @@ class MediaPlaybackService : MediaBrowserServiceCompat() { // Set mediaSession active setActive(BuildConfig.EXTENSION_MEDIASESSION) } - - // Set the MediaBrowserServiceCompat's media session. - sessionToken = mediaSession.sessionToken } private fun stopForegroundService() { @@ -208,41 +199,6 @@ class MediaPlaybackService : MediaBrowserServiceCompat() { mediaSessionConnector.removeListener(mediaSessionListener) } - override fun onGetRoot( - clientPackageName: String, - clientUid: Int, - rootHints: Bundle? - ): BrowserRoot { - // (Optional) Control the level of access for the specified package name. - // You'll need to write your own logic to do this. - return if (allowBrowsing(clientPackageName, clientUid)) { - // Returns a root ID that clients can use with onLoadChildren() to retrieve - // the content hierarchy. - BrowserRoot(BROWSABLE_ROOT, null) - } else { - // Clients can connect, but this BrowserRoot is an empty hierachy - // so onLoadChildren returns nothing. This disables the ability to browse for content. - BrowserRoot(EMPTY_ROOT, null) - } - } - - @Suppress("UNUSED_PARAMETER") - private fun allowBrowsing(clientPackageName: String, clientUid: Int): Boolean { - // Only allow browsing from the same package - return TextUtils.equals(clientPackageName, packageName) - } - - override fun onLoadChildren( - parentId: String, - result: Result> - ) { - if (parentId == EMPTY_ROOT) { - result.sendResult(null) - return - } - result.sendResult(emptyList()) - } - private fun updateNotification() { player?.let { if (it.isPaused) { @@ -254,7 +210,6 @@ class MediaPlaybackService : MediaBrowserServiceCompat() { } private fun updateNotification(@PlaybackStateCompat.State playbackState: Int) { - // When a service is playing, it should be running in the foreground. // This lets the system know that the service is performing a useful function and should // not be killed if the system is low on memory. From a31093ed001ec2f40a95743ee192da11a483c6ff Mon Sep 17 00:00:00 2001 From: Tom Van Laerhoven Date: Mon, 5 Aug 2024 10:22:47 +0200 Subject: [PATCH 2/5] Remove notification when setting an undefined source --- .../java/com/theoplayer/media/MediaPlaybackService.kt | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/android/src/main/java/com/theoplayer/media/MediaPlaybackService.kt b/android/src/main/java/com/theoplayer/media/MediaPlaybackService.kt index 62a014e81..19e68f7e7 100644 --- a/android/src/main/java/com/theoplayer/media/MediaPlaybackService.kt +++ b/android/src/main/java/com/theoplayer/media/MediaPlaybackService.kt @@ -200,12 +200,11 @@ class MediaPlaybackService : Service() { } private fun updateNotification() { - player?.let { - if (it.isPaused) { - updateNotification(PlaybackStateCompat.STATE_PAUSED) - } else { - updateNotification(PlaybackStateCompat.STATE_PLAYING) - } + val player = player + when { + player?.source == null -> updateNotification(PlaybackStateCompat.STATE_STOPPED) + !player.isPaused -> updateNotification(PlaybackStateCompat.STATE_PLAYING) + else -> updateNotification(PlaybackStateCompat.STATE_PAUSED) } } From 3d8fa4a9af1a8260ef6e0dbb197f1d66a3f31c9f Mon Sep 17 00:00:00 2001 From: Tom Van Laerhoven Date: Tue, 6 Aug 2024 09:42:26 +0200 Subject: [PATCH 3/5] Drop MediaBrowserService intent filter --- android/src/main/AndroidManifest.xml | 1 - 1 file changed, 1 deletion(-) diff --git a/android/src/main/AndroidManifest.xml b/android/src/main/AndroidManifest.xml index 6740a54ed..b861e0b68 100644 --- a/android/src/main/AndroidManifest.xml +++ b/android/src/main/AndroidManifest.xml @@ -38,7 +38,6 @@ android:enabled="true" android:foregroundServiceType="mediaPlayback"> - From 24834aa8b3a5a19259690598fc616eb301a88223 Mon Sep 17 00:00:00 2001 From: Tom Van Laerhoven Date: Tue, 6 Aug 2024 09:43:23 +0200 Subject: [PATCH 4/5] Add changelog entry --- CHANGELOG.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 96dd94107..25ec4e106 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,14 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. - Added the fast-forward & rewind buttons for the Android notification when `mediaControl.mediaSessionEnabled` is set to `true`. +### Fixed + +- Fixed an issue on Android where the notification would not disappear when setting an undefined source. + +### Changed + +- Replaced the `MediaBrowserService` with a regular `Service` to facilitate background playback on Android. + ## [7.7.1] - 24-07-29 ### Fixed From e8607cbee99be658164047db8a1fb102a4459850 Mon Sep 17 00:00:00 2001 From: Tom Van Laerhoven Date: Tue, 6 Aug 2024 11:38:36 +0200 Subject: [PATCH 5/5] Drop mediaButton receiver --- android/src/main/AndroidManifest.xml | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/android/src/main/AndroidManifest.xml b/android/src/main/AndroidManifest.xml index b861e0b68..0ddc6ee7a 100644 --- a/android/src/main/AndroidManifest.xml +++ b/android/src/main/AndroidManifest.xml @@ -37,18 +37,8 @@ android:exported="false" android:enabled="true" android:foregroundServiceType="mediaPlayback"> - - - - - - - - -