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

Intercept notification events #40

Merged
merged 5 commits into from
Aug 23, 2022
Merged

Conversation

mpivchev
Copy link
Contributor

@mpivchev mpivchev commented Aug 23, 2022

This PR enables notification events to be intercepted. Conveniently, this also intercepts media session events without any additional code.

RNTP also needs a PR to use this, coming soonTM.

*
* For this observable to send events, set [interceptPlayerActionsTriggeredExternally][com.doublesymmetry.kotlinaudio.models.PlayerConfig.interceptPlayerActionsTriggeredExternally] to true.
*/
var onPlayerActionTriggeredExternally = _onPlayerActionTriggeredExternally.asSharedFlow()
Copy link
Contributor Author

Choose a reason for hiding this comment

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

You can now opt in to have the player actions be intercepted. For RNTP this will be true by default, but since KA is used as a general audio library, it's good to have the option.


mediaSession.isActive = true

notificationManager = NotificationManager(context, exoPlayer, mediaSession.sessionToken, notificationEventHolder)
val playerToUse = if (playerConfig.interceptPlayerActionsTriggeredExternally) forwardingPlayer else exoPlayer
Copy link
Contributor Author

@mpivchev mpivchev Aug 23, 2022

Choose a reason for hiding this comment

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

Depending on the flag, we can choose to intercept(forward) Player actions via ForwardingPlayer. This will intercept(forward) actions triggered by the media session and notification.

private fun createForwardingPlayer(): ForwardingPlayer {
return object : ForwardingPlayer(exoPlayer) {
override fun play() {
playerEventHolder.updateOnMediaSessionCallbackTriggered(MediaSessionCallback.PLAY)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

If ForwardingPlayer is used, the player actions will trigger our custom player events.

@@ -443,34 +477,4 @@ abstract class BaseAudioPlayer internal constructor(private val context: Context
)
}
}

inner class MediaSessionListener : MediaSessionCompat.Callback() {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

We don't need to explicitly override media session events, since our ForwardingPlayer already intercepts events sent by both media session and notification.

@mpivchev mpivchev requested a review from dcvz August 23, 2022 13:54
Copy link
Contributor

@dcvz dcvz left a comment

Choose a reason for hiding this comment

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

LGTM 👍

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.

2 participants