Compose Material 3

media3-ui-compose-material3 程式庫提供 Player 可組合項,可顯示播放器控制項、進度列和目前的媒體內容。它會在內部處理狀態管理和 Material 3 樣式。如要進一步瞭解應使用哪個程式庫,請參閱「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)
  }
}

自訂 Material 3 元件

雖然 media3-ui-compose-material3 提供的元件遵循 Material 3 設計,但您仍可完全控管主題設定。只要將播放器 UI 包裝在 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)
}

可用的 Material 3 元件

media3-ui-compose-material3 程式庫提供一組預先建構的可組合項,適用於常見的播放器控制項。以下是可直接在應用程式中使用的部分元件:

元件 說明
Player 播放器,包含控制項、進度列、字幕,以及影片或圖片的內容框架。
PlayPauseButton 這個按鈕可切換播放和暫停狀態,並顯示緩衝、重播和重試狀態。
SeekBackButton 按鈕,可依定義的增量倒轉。
SeekForwardButton 按鈕,可依定義的增量向前跳轉。
NextButton 跳轉到下一個媒體項目的按鈕。
PreviousButton 跳轉到上一個媒體項目的按鈕。
RepeatButton 這個按鈕可切換重複播放模式。
ShuffleButton 切換隨機播放模式的按鈕。
MuteButton 可將播放器設為靜音或取消靜音的按鈕。
LiveButton 這個按鈕會指出直播是否在直播點播放,輕觸後會跳轉至直播點。
PositionAndDurationText 顯示目前位置和總時長的文字可組合項。
PositionText 顯示目前位置的文字可組合項。
DurationText 顯示總時長的文字可組合項。
RemainingDurationText 顯示剩餘時間的文字可組合項。
ProgressSlider 顯示播放進度並允許使用者跳轉的滑桿。
BufferingIndicator 播放器緩衝處理時顯示的進度指標。

這份清單未列出所有合作夥伴,如要查看所有可用元件,請參閱程式庫的 API 參考資料。

您可能需要另外兩個與介面管理相關的預先建構可組合項,由於不具備 Material Design 主題設定,因此位於 media3-ui-compose 模組中。

元件 說明
ContentFrame 媒體內容介面,可處理顯示比例管理、調整大小、快門、圖片和字幕版位。
PlayerSurface 在 AndroidView 中包裝 SurfaceView 和 TextureView 的原始介面。