diff --git a/CHANGELOG.md b/CHANGELOG.md index 91d40c0ca..b25804580 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,7 +9,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ### Changed -- When resuming an app on Android, retrieve audio focus only if the player is not paused. +- Revised audio focus protocol on Android. When resuming an app, audio focus is retrieved only if the player is not paused. +- Changed the behaviour of the Android component supporting background playback. It is stopped but not disabled when setting `backgroundAudioConfiguration.enabled = true`. ### Fixed diff --git a/android/src/main/AndroidManifest.xml b/android/src/main/AndroidManifest.xml index d09b79189..f2406d42d 100644 --- a/android/src/main/AndroidManifest.xml +++ b/android/src/main/AndroidManifest.xml @@ -32,7 +32,7 @@ android:name="com.theoplayer.media.MediaPlaybackService" android:description="@string/background_playback_service_description" android:exported="false" - android:enabled="false" + android:enabled="true" android:foregroundServiceType="mediaPlayback"> diff --git a/android/src/main/java/com/theoplayer/ReactTHEOplayerContext.kt b/android/src/main/java/com/theoplayer/ReactTHEOplayerContext.kt index f5490ee24..7b5f40772 100644 --- a/android/src/main/java/com/theoplayer/ReactTHEOplayerContext.kt +++ b/android/src/main/java/com/theoplayer/ReactTHEOplayerContext.kt @@ -5,7 +5,6 @@ import android.content.ComponentName import android.content.Context import android.content.Intent import android.content.ServiceConnection -import android.content.pm.PackageManager import android.content.res.Configuration import android.os.Handler import android.os.IBinder @@ -74,7 +73,7 @@ class ReactTHEOplayerContext private constructor( var imaIntegration: GoogleImaIntegration? = null var castIntegration: CastIntegration? = null var wasPlayingOnHostPause: Boolean = false - var isHostPaused: Boolean = false + private var isHostPaused: Boolean = false private val isBackgroundAudioEnabled: Boolean get() = backgroundAudioConfig.enabled @@ -113,27 +112,6 @@ class ReactTHEOplayerContext private constructor( } } - private fun setPlaybackServiceEnabled(enabled: Boolean) { - // Toggle the MediaPlaybackService. - toggleComponent(enabled, ComponentName(reactContext.applicationContext, MediaPlaybackService::class.java)) - - // Also toggle any registered MediaButtonReceiver broadcast receiver. - // It will crash the app if it remains active and tries to find our disabled MediaBrowserService instance. - toggleComponent(enabled, ComponentName(reactContext.applicationContext, androidx.media.session.MediaButtonReceiver::class.java)) - } - - /** - * Enable or disable a receiver component. - */ - private fun toggleComponent(enabled: Boolean, componentName: ComponentName) { - reactContext.applicationContext.packageManager.setComponentEnabledSetting( - componentName, - if (enabled) PackageManager.COMPONENT_ENABLED_STATE_ENABLED - else PackageManager.COMPONENT_ENABLED_STATE_DISABLED, - PackageManager.DONT_KILL_APP - ) - } - private fun applyBackgroundPlaybackConfig( config: BackgroundAudioConfig, prevConfig: BackgroundAudioConfig? @@ -148,13 +126,8 @@ class ReactTHEOplayerContext private constructor( if (BuildConfig.USE_PLAYBACK_SERVICE) { if (prevConfig?.enabled != true && config.enabled) { // Enable & bind background playback - setPlaybackServiceEnabled(true) bindMediaPlaybackService() } else if (prevConfig?.enabled == true) { - // First disable the MediaPlaybackService and MediaButtonReceiver so that no more media - // button events can be captured. - setPlaybackServiceEnabled(false) - // Stop & unbind MediaPlaybackService. binder?.stopForegroundService() unbindMediaPlaybackService()