循序式掃描

ExoPlayer 可以直接播放下列容器格式的串流。 內含的音訊和視訊樣本格式也必須受到支援 (詳情請參閱「樣本格式」一節)。如要瞭解圖片容器和格式支援情形,請參閱「圖片」。

容器格式 支援 留言
MP4
M4A
FMP4
WebM
Matroska
MP3 部分串流只能使用固定位元率搜尋**
Ogg 包含 Vorbis、Opus 和 FLAC
WAV
MPEG-TS
MPEG-PS
FLV 無法搜尋*
ADTS (AAC) 只能使用固定位元率搜尋**
FLAC 使用 FLAC 程式庫ExoPlayer 程式庫中的 FLAC 擷取器***
AMR 只能使用固定位元率搜尋**

* 容器未提供中繼資料 (例如樣本索引),因此媒體播放器無法有效率地執行搜尋,導致系統不支援搜尋。如果需要搜尋,建議使用更合適的容器格式。

** 這些擷取器有 FLAG_ENABLE_CONSTANT_BITRATE_SEEKING 標記,可啟用使用固定位元率假設的近似搜尋功能。這項功能預設為停用。如要為所有支援這項功能的擷取器啟用這項功能,最簡單的方法是使用 DefaultExtractorsFactory.setConstantBitrateSeekingEnabled,如這裡所述。

*** FLAC 程式庫擷取器會輸出原始音訊,架構可在所有 API 級別處理這類音訊。ExoPlayer 程式庫 FLAC 擷取器會輸出 FLAC 音訊影格,因此必須有 FLAC 解碼器 (例如處理 FLAC 的 MediaCodec 解碼器 (API 級別 27 以上版本必須具備),或是啟用 FLAC 的 FFmpeg 程式庫)。如果應用程式是使用 FLAC 程式庫建構,DefaultExtractorsFactory 會使用擴充功能擷取器。否則會使用 ExoPlayer 程式庫擷取器。

使用 MediaItem

如要播放漸進式串流,請使用媒體 URI 建立 MediaItem,並將其傳遞至播放器。

Kotlin

// Create a player instance.
val player = ExoPlayer.Builder(context).build()
// Set the media item to be played.
player.setMediaItem(MediaItem.fromUri(progressiveUri))
// 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(progressiveUri));
// Prepare the player.
player.prepare();

使用 ProgressiveMediaSource

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

Kotlin

// Create a data source factory.
val dataSourceFactory: DataSource.Factory = DefaultHttpDataSource.Factory()
// Create a progressive media source pointing to a stream uri.
val mediaSource: MediaSource =
  ProgressiveMediaSource.Factory(dataSourceFactory)
    .createMediaSource(MediaItem.fromUri(progressiveUri))
// 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 progressive media source pointing to a stream uri.
MediaSource mediaSource =
    new ProgressiveMediaSource.Factory(dataSourceFactory)
        .createMediaSource(MediaItem.fromUri(progressiveUri));
// 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();

自訂播放設定

ExoPlayer 提供多種方式,讓您根據應用程式需求調整播放體驗。如需範例,請參閱自訂頁面