Closed
Description
I can't customize the PlayerSurface
(from media3-ui-compose) size by getting the video size in the Player$Listener.onVideoSizeChanged()
method.
@OptIn(UnstableApi::class)
@Composable
fun Demo(uri: Uri) {
val TAG="VideoSizeDemo"
var curVideoSize by remember {
mutableStateOf(VideoSize.UNKNOWN)
}
val context = LocalContext.current
val exoPlayer = remember {
ExoPlayer.Builder(context).build().apply {
setMediaItem(MediaItem.fromUri(uri))
addListener(object : Player.Listener{
override fun onPlaybackStateChanged(playbackState: Int) {
Log.i(TAG, "onPlaybackStateChanged:${playbackState== Player.STATE_READY} ")
}
override fun onVideoSizeChanged(videoSize: VideoSize) {
Log.i(TAG, "onVideoSizeChanged: width:${videoSize.width} height:${videoSize.height}")
curVideoSize=videoSize
}
})
prepare()
playWhenReady = true
repeatMode = Player.REPEAT_MODE_ONE
}
}
if (curVideoSize.width != 0 && curVideoSize.height != 0) {
val ratio=curVideoSize.width*1f/curVideoSize.height
PlayerSurface(
player = exoPlayer,
surfaceType = SURFACE_TYPE_SURFACE_VIEW,
modifier = Modifier
.fillMaxWidth()
.aspectRatio(ratio),
)
}
}