media3-ui-compose-material3 库提供了一个 Player 可组合项,用于显示播放器控件、进度条和当前媒体内容。它会在内部处理状态管理和 Material3 样式。如需详细了解应使用哪个库,请参阅 Media3 Compose 概览。
// The library provides styled UI components Row { SeekBackButton(player) PlayPauseButton(player) SeekForwardButton(player) }
// You can rearrange the composables into a layout that suits your needs @Composable fun PlayerProgressControlsLeftAligned(player: Player) { Row { PositionAndDurationText(player) ProgressSlider(player) } } @Composable fun PlayerProgressControlsCenterAligned(player: Player) { Row { PositionText(player) ProgressSlider(player) DurationText(player) } }
自定义 Material3 组件
虽然 media3-ui-compose-material3 提供了遵循 Material3 设计的组件,但您仍然可以完全控制主题设置。您可以通过将播放器界面封装在 MaterialTheme 中,自定义颜色、排版和形状。
例如,如需更改 PlayPauseButton 的颜色,您可以提供自定义 colorScheme:
MaterialTheme( colorScheme = lightColorScheme( primary = Color.Red, // Change the primary color for the button onPrimary = Color.White, ) ) { // The PlayPauseButton will now use the custom colors PlayPauseButton(player) }
可用的 Material3 组件
media3-ui-compose-material3 库提供了一组预构建的可组合项,用于实现常见的播放器控件。以下是一些您可以直接在应用中使用的组件:
| 组件 | 说明 |
|---|---|
Player |
一个播放器,其中包含控件、进度条、字幕以及用于播放视频或展示图片的画面。 |
PlayPauseButton |
一个可在播放和暂停之间切换,并显示缓冲、重放和重试状态的按钮。 |
SeekBackButton |
用于按定义的增量向后跳转的按钮。 |
SeekForwardButton |
用于按定义的增量快进的按钮。 |
NextButton |
用于跳转到下一个媒体文件的按钮。 |
PreviousButton |
用于跳转到上一媒体文件的按钮。 |
RepeatButton |
用于循环切换重复模式的按钮。 |
ShuffleButton |
用于切换随机播放模式的按钮。 |
MuteButton |
用于将播放器静音和取消静音的按钮。 |
LiveButton |
一个按钮,用于指示直播播放是否处于直播边缘,并在点按时跳转到直播边缘。 |
PositionAndDurationText |
一个文本可组合项,用于显示当前位置和总时长。 |
PositionText |
一个显示当前位置的文本可组合项。 |
DurationText |
一个文本可组合项,用于显示总时长。 |
RemainingDurationText |
一个显示剩余时长的文本可组合项。 |
ProgressSlider |
一个滑块,用于显示播放进度并允许用户跳转到特定位置。 |
BufferingIndicator |
在播放器缓冲时显示的进度指示器。 |
此列表并未详尽列出所有错误。如需了解所有可用组件,请参阅库的 API 参考文档。
您可能需要的另外两个预构建可组合项与 Surface 管理相关,它们位于 media3-ui-compose 模块中,因为它们不具备 Material 主题设置。
| 组件 | 说明 |
|---|---|
ContentFrame |
用于媒体内容的 Surface,可处理宽高比管理、大小调整、快门、封面和字幕槽。 |
PlayerSurface |
在 AndroidView 中封装 SurfaceView 和 TextureView 的原始 Surface。 |