SmoothStreaming

ExoPlayer 支援 FMP4 容器格式的 SmoothStreaming。媒體串流必須經過解多工處理,也就是說,影片、音訊和文字必須在 SmoothStreaming 資訊清單中,以不同的 StreamIndex 元素定義。內含的音訊和影片樣本格式也必須支援 (詳情請參閱「樣本格式」一節)。

功能 支援 留言
容器
FMP4 僅限解多工串流
隱藏式輔助字幕/字幕
TTML 嵌入 FMP4
內容保護
PlayReady SL2000 僅 Android TV
即時播放
一般直播播放
通用媒體用戶端資料 (CMCD) 整合指南

使用 MediaItem

如要播放 SmoothStreaming 串流,您必須依附於 SmoothStreaming 模組。

Kotlin

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

Groovy

implementation "androidx.media3:media3-exoplayer-smoothstreaming:1.7.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 提供多種方式,讓您根據應用程式需求調整播放體驗。如需範例,請參閱自訂頁面