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

Android: mediaSession - support simple background playback as well #542

Conversation

123mpozzi
Copy link
Contributor

@123mpozzi 123mpozzi commented Oct 23, 2024

Description

As of the current integration, background playback is only supported through media session.
This PR allows to run playback in the background without having to create any media session.

Related work

#541 implements background-playback independently from media session.
Since media session is a subset of background playback, the idea is to unify and re-use most of the logic

Changes

Checklist

  • 🗒 CHANGELOG entry

@123mpozzi 123mpozzi self-assigned this Oct 23, 2024
@123mpozzi 123mpozzi changed the base branch from development to feature/android-enable-lock-screen-controls October 23, 2024 15:25
Comment on lines +65 to +70
<service
android:name="com.bitmovin.player.reactnative.services.BackgroundPlaybackService"
android:foregroundServiceType="mediaPlayback"
android:exported="true">
</service>

Copy link
Contributor Author

@123mpozzi 123mpozzi Oct 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note
This is also essential for properly updating the PlayerView after the app has been minimized. In fact, lifecycle changes alone don’t seem to be sufficient without this, as the entire PlayerView may otherwise appear black when the app is re-opened.

Comment on lines +84 to +88
isBackgroundPlaybackEnabled = if (isMediaSessionPlaybackEnabled) {
isMediaSessionPlaybackEnabled
} else {
playerConfigJson?.getMap("playbackConfig")?.getBoolean("isBackgroundPlaybackEnabled") ?: false
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Open Question about Config Design

Currently, I am making the media session flag forcibly treat the background flag as true when enabled, but that still has to be decided.

This way makes sense for me since media-session is a form of background-playback.

import android.os.IBinder
import com.bitmovin.player.api.Player
import com.bitmovin.player.api.media.session.MediaSession
import com.bitmovin.player.api.media.session.MediaSessionService
import com.bitmovin.player.reactnative.PlayerServiceBinder

class MediaSessionPlaybackService : MediaSessionService() {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Open Question about Services Design

Unfortunately, I don't see a clean way of unifying the two services (maybe someone more proficient in Kotlin can help tho 🙏 )

I see mainly 2 drawbacks when unifying the services:

  • a media session is always initialized in onCreate. Even if I pass data via the intent, onBind will be called after onCreate from my understanding, so the 'fake' media session is already created
  • here we inherit from MediaSessionService(). This is not necessary for simple background-playback. Simple backgorund playback, on the other hand, only needs Service(), without the additional wrappers

@@ -52,3 +57,5 @@ class MediaSessionConnectionManager(val context: ReactApplicationContext) {
playerModule: PlayerModule? = context.playerModule,
): Player = playerModule?.getPlayerOrNull(nativeId) ?: throw IllegalArgumentException("Invalid PlayerId $nativeId")
}

open class PlayerServiceBinder(open var player: Player?) : Binder()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure if this is the best way for extending Binder() for our use-case. I would like input here from someone more knowledgeable in Kotlin 🙏

…re/android-extend-mediasession-for-simple-bg-playback

# Conflicts:
#	android/src/main/java/com/bitmovin/player/reactnative/BackgroundPlaybackManager.kt
@123mpozzi 123mpozzi merged commit d534946 into feature/android-enable-lock-screen-controls Oct 25, 2024
5 of 6 checks passed
@123mpozzi 123mpozzi deleted the feature/android-extend-mediasession-for-simple-bg-playback branch October 25, 2024 13:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant