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.Builder
的 setMimeType
,明確指出內容類型。
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.Listener
的 onTimelineChanged
回呼。對隨選內容而言,這種情況只會發生一次,直播內容也可能會發生這種情況。下列程式碼片段說明每當載入資訊清單時,應用程式可以執行哪些操作。
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 提供多種方式,讓您根據應用程式的需求打造符合需求的播放體驗。請參閱自訂頁面中的範例。