这段代码允许我播放一段视频,通过设置按钮(齿轮图标),我可以打开视频轨道选择器对话框,但是当我改变大小时,播放器会卡在屏幕上的一个循环中,视频会调整到屏幕的一部分。
这是ExoplayerScreen。
@UnstableApi
@Preview(device = "id:pixel_5", showSystemUi = true, showBackground = true)
@Composable
fun ExoplayerScreenPreview() {
ExoplayerScreen()
}
@UnstableApi
@Composable
fun ExoplayerScreen() {
MyStatusBar(color = colorResource(id = R.color.colorPrimaryDark))
Scaffold(topBar = { MyToolBar() }, content = { padding ->
ExoplayerScreenContent(Modifier.padding(padding))
})
}
@UnstableApi
@Composable
fun ExoplayerScreenContent(modifier: Modifier) {
Box(
modifier = modifier
.fillMaxWidth()
.height(230.dp)
) {
VideoScreen()
}
}
这是视频屏幕
@SuppressLint("OpaqueUnitKey")
@androidx.annotation.OptIn(androidx.media3.common.util.UnstableApi::class)
@Composable
fun VideoScreen() {
val context = LocalContext.current
val playerView = PlayerView(context)
val exoPlayer = remember {
ExoPlayer.Builder(context).build().apply {
setMediaItem(
MediaItem.fromUri(
"https://demo.unified-streaming.com/k8s/features/stable/video/tears-of-steel/tears-of-steel.ism/.m3u8"
)
)
prepare()
play()
videoScalingMode = C.VIDEO_SCALING_MODE_SCALE_TO_FIT_WITH_CROPPING
}
}
var isPlaying by remember {
mutableStateOf(false)
}
exoPlayer.addListener(object : Player.Listener {
override fun onIsPlayingChanged(isPlayingValue: Boolean) {
isPlaying = isPlayingValue
}
})
DisposableEffect(Box {
AndroidView(modifier = Modifier.fillMaxSize(), factory = {
playerView.apply {
// Resizes the video in order to be able to fill the whole screen
resizeMode = AspectRatioFrameLayout.RESIZE_MODE_FIT
player = exoPlayer
// Hides the default Player Controller
useController = false
// Fills the whole screen
layoutParams = FrameLayout.LayoutParams(MATCH_PARENT, MATCH_PARENT)
}
})
VideoLayout(
isPlaying = isPlaying,
onPlay = { exoPlayer.play() },
onPause = { exoPlayer.pause() },
exoPlayer = exoPlayer,
)
}) {
onDispose { exoPlayer.release() }
}
}
这是视频布局
@Composable
fun VideoLayout(
isPlaying: Boolean,
onPlay: () -> Unit,
onPause: () -> Unit,
exoPlayer: ExoPlayer,
) {
Box(modifier = Modifier
.fillMaxSize()
.clickable {
if (isPlaying) {
onPause()
} else {
onPlay()
}
}) {
AnimatedVisibility(
visible = !isPlaying, enter = fadeIn(tween(200)), exit = fadeOut(tween(200))
) {
Box(
modifier = Modifier
.fillMaxSize()
.background(
Color.Black.copy(alpha = 0.6f)
), contentAlignment = Alignment.Center
) {
Icon(
imageVector = Icons.Rounded.Pause, contentDescription = null, tint = Color.White
)
}
Box(
modifier = Modifier
.fillMaxSize()
.padding(vertical = 15.dp, horizontal = 20.dp),
contentAlignment = Alignment.BottomEnd
) {
val context = LocalContext.current
Column(
verticalArrangement = Arrangement.spacedBy(15.dp)
) {
IconButton(onClick = { trackSeleccion(exoPlayer, context) }) {
Icon(
imageVector = Icons.Rounded.Settings,
contentDescription = null,
tint = Color.White
)
}
IconButton(onClick = { /*TODO*/ }) {
Icon(
imageVector = Icons.Rounded.Comment,
contentDescription = null,
tint = Color.White
)
}
IconButton(onClick = { /*TODO*/ }) {
Icon(
imageVector = Icons.Rounded.IosShare,
contentDescription = null,
tint = Color.White
)
}
}
}
}
}
}
这是赛道选择
@androidx.annotation.OptIn(androidx.media3.common.util.UnstableApi::class)
fun trackSeleccion(exoplayer: ExoPlayer, context: Context) {
val trackSelector = exoplayer.trackSelector as DefaultTrackSelector
val mappedTrackInfo = trackSelector.currentMappedTrackInfo
if (mappedTrackInfo != null) {
val rendererIndex = 2
val rendererType = mappedTrackInfo.getRendererType(rendererIndex)
val allowAdaptiveSelections =
rendererType == C.TRACK_TYPE_VIDEO || (rendererType == C.TRACK_TYPE_AUDIO && mappedTrackInfo.getTypeSupport(
C.TRACK_TYPE_VIDEO
) == MappingTrackSelector.MappedTrackInfo.RENDERER_SUPPORT_NO_TRACKS)
val builder =
TrackSelectionDialogBuilder(context, "Selección de pistas", exoplayer, rendererIndex)
builder.setShowDisableOption(false)
builder.setAllowAdaptiveSelections(allowAdaptiveSelections)
builder.setOverrides(exoplayer.trackSelectionParameters.overrides)
builder.build().show()
}
exoplayer.videoScalingMode = C.VIDEO_SCALING_MODE_SCALE_TO_FIT_WITH_CROPPING
Log.e("trackSeleccion", "trackSeleccion: ")
}
红色方块是播放器开始播放视频时,蓝色方块是我将视频轨道更改为较低分辨率时
我试过这个代码,但是当改变分辨率时,我不能让视频保持在屏幕的宽度,它只会改变质量。
我正在使用Media3。
2条答案
按热度按时间kjthegm61#
要实现所需的行为,即视频在更改分辨率时保持在屏幕的宽度上,您可以使用ExoPlayer的纵横比处理功能。您可能还需要调整布局和配置。以下是实现这一目标的一些建议:
1.调整
VideoScreen
和Layout
:修改您的
VideoScreen
可组合组件,以包含 PackagePlayerView
的AspectRatioFrameLayout
。此帧布局允许您控制视频的宽高比。确保将resizeMode
设置为AspectRatioFrameLayout.RESIZE_MODE_FILL
,这将使视频填充整个帧,同时保持正确的长宽比。1.调整
trackSeleccion
功能:在
trackSeleccion
函数中,在更改视频轨道后,您可以强制调整视频播放器的大小,以确保其适合屏幕宽度。您可以使用exoPlayer.videoScalingMode
来控制视频缩放行为。将其设置为C.VIDEO_SCALING_MODE_SCALE_TO_FIT_WITH_CROPPING
,以确保视频在保持宽高比的同时填满屏幕宽度。bvjxkvbb2#
@Hisham的推荐
最后,我用下面的代码解决了这个问题: