媒体来源
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
在 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
实例。如果有多个客户 ID
需要合并自定义设置,
。
ClippingMediaSource
:允许将媒体剪辑到指定的时间戳范围。
如果这是唯一的修改,最好使用
MediaItem.ClippingConfiguration
。
FilteringMediaSource
:过滤适用于指定类型的可用轨道,
例如,公开一个包含两个音频的文件中的视频轨道
以及视频。如果这是唯一的修改,最好使用
跟踪选择参数。
MergingMediaSource
:合并多个媒体源以并行播放。在
几乎在所有情况下,建议您使用
将 adjustPeriodTimeOffsets
和 clipDurations
设置为 true,以确保
来源的开始时间和结束时间相同。如果完成此修改
旁加载字幕,最好使用
MediaItem.SubtitleConfiguration
。
ConcatenatingMediaSource2
:合并要播放的多个媒体来源
。用户可见的媒体结构公开了
Timeline.Window
,表示它看起来像是单个商品。如果
并进行了多项修改,播放了多项
建议使用播放列表 API 方法,例如
Player.addMediaItem
。
SilenceMediaSource
:在指定时长内生成静音
有助于填补数据缺口。
AdsMediaSource
:使用客户端广告插播功能扩展媒体来源
功能。如需了解详情,请参阅广告插播指南。
ServerSideAdInsertionMediaSource
:使用服务器端广告扩展媒体来源
插入功能。如需了解详情,请参阅广告插播指南。
本页面上的内容和代码示例受内容许可部分所述许可的限制。Java 和 OpenJDK 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-27。
[null,null,["最后更新时间 (UTC):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."]]