Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Video title of current playing video
@ExperimentalAnimationApi
@Composable
fun VideoPlayer(
modifier: Modifier = Modifier,
gameVideos: List<VideoResultEntity>
) {
val context = LocalContext.current
val mediaItems = arrayListOf<MediaItem>()
val videoTitle = remember {
mutableStateOf(gameVideos[0].name)
}
val visibleState = remember { mutableStateOf(true) }
gameVideos.forEach {
mediaItems.add(
MediaItem.Builder()
.setUri(it.video)
.setMediaId(it.id.toString())
.setTag(it)
.setMediaMetadata(
MediaMetadata.Builder()
.setDisplayTitle(it.name)
.build()
)
.build()
)
}
val exoPlayer = remember {
SimpleExoPlayer.Builder(context).build().apply {
this.setMediaItems(mediaItems)
this.prepare()
this.playWhenReady = true
addListener(
object : Player.Listener {
override fun onEvents(
player: Player,
events: Player.Events
) {
super.onEvents(player, events)
// hide title only when player duration is at least 200ms
if (player.currentPosition >= 200)
visibleState.value = false
}
override fun onMediaItemTransition(
mediaItem: MediaItem?,
reason: Int
) {
super.onMediaItemTransition(
mediaItem,
reason
)
// everytime the media item changes show the title
visibleState.value = true
videoTitle.value =
mediaItem?.mediaMetadata
?.displayTitle.toString()
}
}
)
}
}
ConstraintLayout(modifier = modifier) {
val (title, videoPlayer) = createRefs()
AnimatedVisibility(
visible = visibleState.value,
modifier =
Modifier.constrainAs(title) {
top.linkTo(parent.top)
start.linkTo(parent.start)
end.linkTo(parent.end)
}
) {
Text(
text = videoTitle.value,
color = Color.White,
fontWeight = FontWeight.Bold,
modifier =
Modifier.padding(16.dp)
.fillMaxWidth()
.wrapContentHeight()
)
}
// player view same as before
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment