@@ -8,6 +8,9 @@ import android.app.Application
8
8
import androidx.lifecycle.AndroidViewModel
9
9
import androidx.lifecycle.viewModelScope
10
10
import androidx.media3.common.Player
11
+ import androidx.mediarouter.media.MediaControlIntent
12
+ import androidx.mediarouter.media.MediaRouteSelector
13
+ import ch.srgssr.androidx.mediarouter.compose.MediaRouteButton
11
14
import ch.srgssr.pillarbox.cast.PillarboxCastPlayer
12
15
import ch.srgssr.pillarbox.cast.getCastContext
13
16
import ch.srgssr.pillarbox.cast.isCastSessionAvailableAsFlow
@@ -29,23 +32,31 @@ import kotlinx.coroutines.flow.stateIn
29
32
* @param application The application context.
30
33
*/
31
34
class CastShowcaseViewModel (application : Application ) : AndroidViewModel(application) {
35
+ private val castContext = application.getCastContext()
32
36
private val castPlayer = PillarboxCastPlayer (application)
33
37
private val localPlayer = PillarboxExoPlayer (application)
34
38
35
39
/* *
36
40
* The current player, it can be either a [PillarboxCastPlayer] or a [PillarboxExoPlayer].
37
41
*/
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 }
41
44
.distinctUntilChanged()
42
45
.onEach { switchPlayer(it) }
43
46
.stateIn(
44
47
viewModelScope,
45
48
SharingStarted .WhileSubscribed (5_000 ),
46
- if (application.getCastContext() .isConnected()) castPlayer else localPlayer
49
+ if (castContext .isConnected()) castPlayer else localPlayer
47
50
)
48
51
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
+
49
60
override fun onCleared () {
50
61
castPlayer.release()
51
62
localPlayer.release()
0 commit comments