in app/src/main/java/com/amazon/ivs/multiple/players/ui/secondlayout/SecondLayoutViewModel.kt [30:77]
fun initPlayers(context: Context, playerViews: List<PlayerUIModel>) {
if (playerSet.isNotEmpty()) return
SecondLayoutStream.values().forEachIndexed { index, streamModel ->
updateBufferingState(index, true)
val player = MediaPlayer(context)
val listener = player.init(
index,
{ videoSizeState ->
playerSet.find { it.index == videoSizeState.index }?.let { player ->
if (player.width != videoSizeState.width || player.height != videoSizeState.height) {
player.width = videoSizeState.width
player.height = videoSizeState.height
_onSizeChanged.tryEmit(player.index)
}
}
},
{ state, playerIndex ->
when (state) {
Player.State.BUFFERING -> {
updateBufferingState(playerIndex, true)
}
Player.State.READY -> {
player.qualities.firstOrNull { it.name == streamModel.maxQuality }?.let { quality ->
player.setAutoMaxQuality(quality)
}
}
Player.State.PLAYING -> {
updateBufferingState(playerIndex, false)
_onPlaying.tryEmit(true)
}
else -> { /* Ignored */ }
}
if (playerSet.all { playerModel -> playerModel.player.state != Player.State.PLAYING }) {
_onPlaying.tryEmit(false)
}
},
{ exception ->
_onError.tryEmit(exception)
}
)
player.setSurface(playerViews.getOrNull(index)?.surface)
player.load(Uri.parse(streamModel.uri))
player.play()
playerSet.add(PlayerModel(index, player, listener))
}
}