媒體來源
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
在 ExoPlayer 中,每個媒體都以 MediaItem
表示。不過
內部,播放器需要 MediaSource
執行個體才能播放內容。
播放器會使用 MediaSource.Factory
根據媒體項目建立這些內容。
根據預設,玩家會使用 DefaultMediaSourceFactory
,
下列內容 MediaSource
實作的執行個體:
DefaultMediaSourceFactory
也可以建立更複雜的媒體來源,具體取決於
相應媒體項目的屬性中一探究竟。詳細說明
詳細介紹
媒體項目頁面。
如果應用程式需要媒體來源設定,但不受
系統會提供多個選項
。
建構播放器時,可以插入 MediaSource.Factory
。例如:
如果應用程式要插入廣告,並使用 CacheDataSource.Factory
支援
DefaultMediaSourceFactory
的執行個體可以設定為
這些規定會在建立播放器時插入:
Kotlin
val mediaSourceFactory: MediaSource.Factory =
DefaultMediaSourceFactory(context)
.setDataSourceFactory(cacheDataSourceFactory)
.setLocalAdInsertionComponents(adsLoaderProvider, playerView)
val player = ExoPlayer.Builder(context).setMediaSourceFactory(mediaSourceFactory).build()
Java
MediaSource.Factory mediaSourceFactory =
new DefaultMediaSourceFactory(context)
.setDataSourceFactory(cacheDataSourceFactory)
.setLocalAdInsertionComponents(adsLoaderProvider, /* adViewProvider= */ playerView);
ExoPlayer player =
new ExoPlayer.Builder(context).setMediaSourceFactory(mediaSourceFactory).build();
DefaultMediaSourceFactory
JavaDoc
就會詳細說明可用的選項
您也可以插入自訂的 MediaSource.Factory
實作,
都支援建立自訂媒體來源類型。工廠的
系統會呼叫 createMediaSource(MediaItem)
,為每個項目建立媒體來源
屬於
已新增至播放清單。
ExoPlayer
介面定義了其他接受的播放清單方法
媒體資源而非媒體項目您可以略過
播放器的內部 MediaSource.Factory
,並將媒體來源例項傳遞至
播放器:
Kotlin
// Set a list of media sources as initial playlist.
exoPlayer.setMediaSources(listOfMediaSources)
// Add a single media source.
exoPlayer.addMediaSource(anotherMediaSource)
// Can be combined with the media item API.
exoPlayer.addMediaItem(/* index= */ 3, MediaItem.fromUri(videoUri))
exoPlayer.prepare()
exoPlayer.play()
Java
// Set a list of media sources as initial playlist.
exoPlayer.setMediaSources(listOfMediaSources);
// Add a single media source.
exoPlayer.addMediaSource(anotherMediaSource);
// Can be combined with the media item API.
exoPlayer.addMediaItem(/* index= */ 3, MediaItem.fromUri(videoUri));
exoPlayer.prepare();
exoPlayer.play();
ExoPlayer 提供多個 MediaSource
實作以修改和撰寫
其他 MediaSource
執行個體。如果有多個
自訂動作必須結合在一起
而且使用簡單的設定路徑
而負責任的 AI 技術做法
有助於達成這項目標
ClippingMediaSource
:允許將媒體剪輯至指定的時間戳記範圍。
如果這是唯一的修改,則建議使用
MediaItem.ClippingConfiguration
。
FilteringMediaSource
:篩選指定類型可用的音軌,
例如,從含有這兩種音訊的檔案公開視訊軌。
和影片如果這是唯一的修改,則建議使用
追蹤選取參數。
MergingMediaSource
:合併多個媒體來源以平行播放。於
在所有情況下,最好使用
adjustPeriodTimeOffsets
和 clipDurations
設為 true,以確保全部
個來源的開始時間和結束時間相同。如已修改完畢
建議使用側載字幕
MediaItem.SubtitleConfiguration
。
ConcatenatingMediaSource2
:合併多個要播放的媒體來源
。使用者能看見的媒體結構
提供單一的
Timeline.Window
,代表類似單一項目。如果這是
修改了可播放多個不該外觀的項目
單一影片,不過建議使用 playlist API 方法:
Player.addMediaItem
。
SilenceMediaSource
:在指定的時間長度內產生靜音
以彌補資料落差
AdsMediaSource
:透過用戶端廣告插播擴充媒體來源
即便沒有技術背景,也能因這些工具的功能而受益詳情請參閱廣告插入指南。
ServerSideAdInsertionMediaSource
:使用伺服器端廣告擴充媒體來源
插入功能詳情請參閱廣告插入指南。
這個頁面中的內容和程式碼範例均受《內容授權》中的授權所規範。Java 與 OpenJDK 是 Oracle 和/或其關係企業的商標或註冊商標。
上次更新時間:2025-07-27 (世界標準時間)。
[null,null,["上次更新時間:2025-07-27 (世界標準時間)。"],[],[],null,["# Media sources\n\nIn ExoPlayer, every piece of media is represented by a `MediaItem`. However\ninternally, the player needs `MediaSource` instances to play the content. The\nplayer creates these from media items using a `MediaSource.Factory`.\n\nBy default the player uses a `DefaultMediaSourceFactory`, which can create\ninstances of the following content `MediaSource` implementations:\n\n- `DashMediaSource` for [DASH](/guide/topics/media/exoplayer/dash).\n- `SsMediaSource` for [SmoothStreaming](/guide/topics/media/exoplayer/smoothstreaming).\n- `HlsMediaSource` for [HLS](/guide/topics/media/exoplayer/hls).\n- `ProgressiveMediaSource` for [regular media files](/guide/topics/media/exoplayer/progressive).\n- `RtspMediaSource` for [RTSP](/guide/topics/media/exoplayer/rtsp).\n\n`DefaultMediaSourceFactory` can also create more complex media sources depending\non the properties of the corresponding media items. This is described in more\ndetail on the\n[Media items page](/guide/topics/media/exoplayer/media-items).\n\nFor apps that need media source setups that are not supported by the\ndefault configuration of the player, there are several options for\ncustomization.\n\nCustomizing media source creation\n---------------------------------\n\nWhen building the player, a `MediaSource.Factory` can be injected. For example,\nif an app wants to insert ads and use a `CacheDataSource.Factory` to support\ncaching, an instance of `DefaultMediaSourceFactory` can be configured to match\nthese requirements and injected during player construction: \n\n### Kotlin\n\n```kotlin\n val mediaSourceFactory: MediaSource.Factory =\n DefaultMediaSourceFactory(context)\n .setDataSourceFactory(cacheDataSourceFactory)\n .setLocalAdInsertionComponents(adsLoaderProvider, playerView)\n val player = ExoPlayer.Builder(context).setMediaSourceFactory(mediaSourceFactory).build()\n```\n\n### Java\n\n```java\nMediaSource.Factory mediaSourceFactory =\n new DefaultMediaSourceFactory(context)\n .setDataSourceFactory(cacheDataSourceFactory)\n .setLocalAdInsertionComponents(adsLoaderProvider, /* adViewProvider= */ playerView);\nExoPlayer player =\n new ExoPlayer.Builder(context).setMediaSourceFactory(mediaSourceFactory).build();\n```\n\n\u003cbr /\u003e\n\nThe\n[`DefaultMediaSourceFactory` JavaDoc](/reference/androidx/media3/exoplayer/source/DefaultMediaSourceFactory)\ndescribes the available options in more detail.\n\nIt's also possible to inject a custom `MediaSource.Factory` implementation, for\nexample to support creation of a custom media source type. The factory's\n`createMediaSource(MediaItem)` will be called to create a media source for each\nmedia item that is\n[added to the playlist](/guide/topics/media/exoplayer/playlists).\n\nMedia source based playlist API\n-------------------------------\n\nThe [`ExoPlayer`](/reference/androidx/media3/exoplayer/ExoPlayer) interface defines additional playlist methods that accept\nmedia sources rather than media items. This makes it possible to bypass the\nplayer's internal `MediaSource.Factory` and pass media source instances to the\nplayer directly: \n\n### Kotlin\n\n```kotlin\n// Set a list of media sources as initial playlist.\nexoPlayer.setMediaSources(listOfMediaSources)\n// Add a single media source.\nexoPlayer.addMediaSource(anotherMediaSource)\n\n// Can be combined with the media item API.\nexoPlayer.addMediaItem(/* index= */ 3, MediaItem.fromUri(videoUri))\n\nexoPlayer.prepare()\nexoPlayer.play()\n```\n\n### Java\n\n```java\n// Set a list of media sources as initial playlist.\nexoPlayer.setMediaSources(listOfMediaSources);\n// Add a single media source.\nexoPlayer.addMediaSource(anotherMediaSource);\n\n// Can be combined with the media item API.\nexoPlayer.addMediaItem(/* index= */ 3, MediaItem.fromUri(videoUri));\n\nexoPlayer.prepare();\nexoPlayer.play();\n```\n\n\u003cbr /\u003e\n\nAdvanced media source composition\n---------------------------------\n\nExoPlayer provides multiple `MediaSource` implementations to modify and compose\nother `MediaSource` instances. These are most useful in cases where multiple\ncustomizations have to be combined and none of the simpler setup paths are\nsufficient.\n\n- `ClippingMediaSource`: Allows to clip media to a specified timestamp range. If this is the only modification, it's preferable to use [`MediaItem.ClippingConfiguration`](/guide/topics/media/exoplayer/media-items#clipping-media) instead.\n- `FilteringMediaSource`: Filters available tracks to the specified types, for example, just exposing the video track from a file that contains both audio and video. If this is the only modification, it's preferable to use [track selection parameters](/guide/topics/media/exoplayer/track-selection#disabling-track) instead.\n- `MergingMediaSource`: Merges multiple media sources to play in parallel. In almost all cases, it's advisable to call the constructor with `adjustPeriodTimeOffsets` and `clipDurations` set to true to ensure all sources start and end at the same time. If this modification is done to add side-loaded subtitles, it's preferable to use [`MediaItem.SubtitleConfiguration`](/guide/topics/media/exoplayer/media-items#sideloading-subtitle) instead.\n- `ConcatenatingMediaSource2`: Merges multiple media source to play consecutively. The user-visible media structure exposes a single `Timeline.Window`, meaning that it looks like a single item. If this modification is done to play multiple items that are not supposed to look like a single one, it's preferable to use the [playlist API](/guide/topics/media/exoplayer/playlists) methods like `Player.addMediaItem` instead.\n- `SilenceMediaSource`: Generates silence for a specified duration that is useful to fill gaps.\n- `AdsMediaSource`: Extends a media source with client-side ad insertion capabilities. Refer to the [ad insertion guide](/guide/topics/media/exoplayer/ad-insertion) for details.\n- `ServerSideAdInsertionMediaSource`: Extends a media source with server-side ad insertion capabilities. Refer to the [ad insertion guide](/guide/topics/media/exoplayer/ad-insertion) for details."]]