We use chromecast android sdk to cast some video files. we have our own implementation of play/pause/seek using RemoteMediaClient
(play()
, pause()
, seek(Long)
).
Also, we do allow chromecast to show notifications on the same device from which casting is happening using CastMediaOptions.Builder#setNotificationOptions(NotificationOptions)
.
To listen to notification controls we use CastMediaOptions.Builder()#setMediaIntentReceiverClassName(MediaIntentReceiver)
which works completely fine when we control video from cast notification of the same device from which casting is done.
As we know Chromecast allows us to control casting media from all android devices in the same wifi network. when we try to play/pause the video from another device in same network MediaIntentReceiver
won't receive anything and we are unable to change the player state inside our app.
Below is code for referance
OptionProvider
class CastOptionsProvider : OptionsProvider {
override fun getCastOptions(context: Context): CastOptions {
val notificationOptions = NotificationOptions.Builder()
.build()
val mediaOptions = CastMediaOptions.Builder()
.setNotificationOptions(notificationOptions)
.setMediaIntentReceiverClassName(FitbuddMediaIntentReceiver::class.java.name)
.build()
return CastOptions.Builder()
.setReceiverApplicationId(CastMediaControlIntent.DEFAULT_MEDIA_RECEIVER_APPLICATION_ID)
.setCastMediaOptions(mediaOptions)
.setStopReceiverApplicationWhenEndingSession(true)
.build()
}
override fun getAdditionalSessionProviders(context: Context): List<SessionProvider>? {
return null
}
}
MediaIntentReceiver (registered inside manifest too)
class FitbuddMediaIntentReceiver : MediaIntentReceiver() {
//this is getting call when playback toggle from same device's notification
//which is casting media but
//wont get call when playback is toggled from notification from another device
//inside same network.
override fun onReceiveActionTogglePlayback(currentSession: Session) {
"CAST -> onReceiveActionTogglePlayback ->".dumpError()
super.onReceiveActionTogglePlayback(currentSession)
//send event to active activity
}
}
We found that setMediaIntentReceiverClassName
only give you event when interacting with notification which created by using CastMediaOptions.Builder()#setNotificationOptions().
So to check player state, when changed from other network device, we use RemoteMediaClient.addProgressListener(RemoteMediaClient.ProgressListener)
which does not provide player state but does provide progress of cast receiver after given period. On onProgressUpdated
we check for RemoteMediaClient.playerState
so our ui is up to date with cast receiver state.