Skip to content
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

Maintenance/android playbackservice #221

Merged
merged 5 commits into from
Oct 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion android/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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">
<intent-filter>
<action android:name="android.media.browse.MediaBrowserService" />
Expand Down
29 changes: 1 addition & 28 deletions android/src/main/java/com/theoplayer/ReactTHEOplayerContext.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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?
Expand All @@ -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()
Expand Down
Loading