@@ -8,6 +8,9 @@ import android.app.Application
88import androidx.lifecycle.AndroidViewModel
99import androidx.lifecycle.viewModelScope
1010import androidx.media3.common.Player
11+ import androidx.mediarouter.media.MediaControlIntent
12+ import androidx.mediarouter.media.MediaRouteSelector
13+ import ch.srgssr.androidx.mediarouter.compose.MediaRouteButton
1114import ch.srgssr.pillarbox.cast.PillarboxCastPlayer
1215import ch.srgssr.pillarbox.cast.getCastContext
1316import ch.srgssr.pillarbox.cast.isCastSessionAvailableAsFlow
@@ -29,23 +32,31 @@ import kotlinx.coroutines.flow.stateIn
2932 * @param application The application context.
3033 */
3134class CastShowcaseViewModel (application : Application ) : AndroidViewModel(application) {
35+ private val castContext = application.getCastContext()
3236 private val castPlayer = PillarboxCastPlayer (application)
3337 private val localPlayer = PillarboxExoPlayer (application)
3438
3539 /* *
3640 * The current player, it can be either a [PillarboxCastPlayer] or a [PillarboxExoPlayer].
3741 */
38- val currentPlayer = castPlayer.isCastSessionAvailableAsFlow().map {
39- if (it) castPlayer else localPlayer
40- }
42+ val currentPlayer = castPlayer.isCastSessionAvailableAsFlow()
43+ .map { if (it) castPlayer else localPlayer }
4144 .distinctUntilChanged()
4245 .onEach { switchPlayer(it) }
4346 .stateIn(
4447 viewModelScope,
4548 SharingStarted .WhileSubscribed (5_000 ),
46- if (application.getCastContext() .isConnected()) castPlayer else localPlayer
49+ if (castContext .isConnected()) castPlayer else localPlayer
4750 )
4851
52+ /* *
53+ * The [MediaRouteSelector] to use on the [MediaRouteButton].
54+ */
55+ val routeSelector = castContext.mergedSelector ? : MediaRouteSelector .Builder ()
56+ .addControlCategory(MediaControlIntent .CATEGORY_LIVE_VIDEO )
57+ .addControlCategory(MediaControlIntent .CATEGORY_REMOTE_PLAYBACK )
58+ .build()
59+
4960 override fun onCleared () {
5061 castPlayer.release()
5162 localPlayer.release()
0 commit comments