SmoothStreaming

ExoPlayer 支援採用 FMP4 容器格式的 SmoothStreaming,媒體串流必須採用模擬形式,也就是必須在 SmoothStreaming 資訊清單中的不同 StreamIndex 元素中定義影片、音訊和文字。此外,您也必須支援內含的音訊和影片範例格式 (詳情請參閱「範例格式」一節)。

功能 有權限 留言
容器
FMP4 僅限 Demuxed 串流
隱藏式輔助字幕/字幕
TTML 內嵌於 FMP4
內容保護
PlayReady SL2000 僅 Android TV
即時播放
定期即時播放
常見媒體用戶端資料 (CMCD) 整合指南

使用 MediaItem

如要播放 SmoothStreaming 串流,您需要依賴 SmoothStreaming 模組。

Kotlin

implementation("androidx.media3:media3-exoplayer-smoothstreaming:1.3.1")

Groovy

implementation "androidx.media3:media3-exoplayer-smoothstreaming:1.3.1"

接著,您可以針對 SmoothStreaming 資訊清單 URI 建立 MediaItem,並傳遞給播放器。

Kotlin

// Create a player instance.
val player = ExoPlayer.Builder(context).build()
// Set the media item to be played.
player.setMediaItem(MediaItem.fromUri(ssUri))
// Prepare the player.
player.prepare()

Java

// Create a player instance.
ExoPlayer player = new ExoPlayer.Builder(context).build();
// Set the media item to be played.
player.setMediaItem(MediaItem.fromUri(ssUri));
// Prepare the player.
player.prepare();

如果 URI 結尾不是 .ism/Manifest,可以將 MimeTypes.APPLICATION_SS 傳遞至 MediaItem.BuildersetMimeType,明確指出內容類型。

ExoPlayer 會根據可用頻寬和裝置功能等因素,自動調整資訊清單中定義的表示法。

使用 SsMediaSource

如需更多自訂選項,您可以建立 SsMediaSource 並直接傳遞給播放器,而非 MediaItem

Kotlin

// Create a data source factory.
val dataSourceFactory: DataSource.Factory = DefaultHttpDataSource.Factory()
// Create a SmoothStreaming media source pointing to a manifest uri.
val mediaSource: MediaSource =
  SsMediaSource.Factory(dataSourceFactory).createMediaSource(MediaItem.fromUri(ssUri))
// Create a player instance.
val player = ExoPlayer.Builder(context).build()
// Set the media source to be played.
player.setMediaSource(mediaSource)
// Prepare the player.
player.prepare()

Java

// Create a data source factory.
DataSource.Factory dataSourceFactory = new DefaultHttpDataSource.Factory();
// Create a SmoothStreaming media source pointing to a manifest uri.
MediaSource mediaSource =
    new SsMediaSource.Factory(dataSourceFactory).createMediaSource(MediaItem.fromUri(ssUri));
// Create a player instance.
ExoPlayer player = new ExoPlayer.Builder(context).build();
// Set the media source to be played.
player.setMediaSource(mediaSource);
// Prepare the player.
player.prepare();

存取資訊清單

您可以呼叫 Player.getCurrentManifest 擷取目前的資訊清單。針對 SmoothStreaming,您應將傳回的物件轉換為 SsManifest。每次載入資訊清單時,系統也會呼叫 Player.ListeneronTimelineChanged 回呼。這會在隨選內容上發生一次,而直播內容也可能進行多次。下列程式碼片段說明每次載入資訊清單時,應用程式可執行的動作。

Kotlin

player.addListener(
  object : Player.Listener {
    override fun onTimelineChanged(timeline: Timeline, @TimelineChangeReason reason: Int) {
      val manifest = player.currentManifest
      if (manifest is SsManifest) {
        // Do something with the manifest.
      }
    }
  }
)

Java

player.addListener(
    new Player.Listener() {
      @Override
      public void onTimelineChanged(
          Timeline timeline, @Player.TimelineChangeReason int reason) {
        Object manifest = player.getCurrentManifest();
        if (manifest != null) {
          SsManifest ssManifest = (SsManifest) manifest;
          // Do something with the manifest.
        }
      }
    });

自訂播放方式

ExoPlayer 提供多種方式,您可以根據應用程式需求自訂播放體驗。如需示例,請參閱「自訂」頁面