Android Kotlin ExoPlayer 计算视频比例并设置setVideoSurfaceView
要计算视频的比例并设置setVideoSurfaceView,你可以使用以下代码:
// 获取视频的宽度和高度
val videoWidth = player.videoFormat.width
val videoHeight = player.videoFormat.height
// 计算视频的比例
val videoAspectRatio = videoWidth.toFloat() / videoHeight.toFloat()
// 获取SurfaceView的宽度和高度
val surfaceWidth = surfaceView.width
val surfaceHeight = surfaceView.height
// 计算SurfaceView的比例
val surfaceAspectRatio = surfaceWidth.toFloat() / surfaceHeight.toFloat()
// 根据视频比例和SurfaceView比例调整SurfaceView的尺寸和位置
if (videoAspectRatio > surfaceAspectRatio) {
val scaledWidth = surfaceHeight * videoAspectRatio
surfaceView.layoutParams.width = scaledWidth.toInt()
surfaceView.layoutParams.height = surfaceHeight
surfaceView.x = (surfaceWidth - scaledWidth) / 2
} else {
val scaledHeight = surfaceWidth / videoAspectRatio
surfaceView.layoutParams.width = surfaceWidth
surfaceView.layoutParams.height = scaledHeight.toInt()
surfaceView.y = (surfaceHeight - scaledHeight) / 2
}
// 更新SurfaceView的尺寸和位置
surfaceView.requestLayout()
请确保在调用此代码之前,已经初始化了ExoPlayer并设置了视频源。这段代码会根据视频的宽度和高度以及SurfaceView的宽度和高度计算视频的比例和SurfaceView的比例,然后根据比例调整SurfaceView的尺寸和位置。最后,调用requestLayout()方法来更新SurfaceView的尺寸和位置
原文地址: http://www.cveoy.top/t/topic/iC4S 著作权归作者所有。请勿转载和采集!