Media sources
Stay organized with collections
Save and categorize content based on your preferences.
In ExoPlayer, every piece of media is represented by a MediaItem
. However
internally, the player needs MediaSource
instances to play the content. The
player creates these from media items using a MediaSource.Factory
.
By default the player uses a DefaultMediaSourceFactory
, which can create
instances of the following content MediaSource
implementations:
DefaultMediaSourceFactory
can also create more complex media sources depending
on the properties of the corresponding media items. This is described in more
detail on the
Media items page.
For apps that need media source setups that are not supported by the
default configuration of the player, there are several options for
customization.
When building the player, a MediaSource.Factory
can be injected. For example,
if an app wants to insert ads and use a CacheDataSource.Factory
to support
caching, an instance of DefaultMediaSourceFactory
can be configured to match
these requirements and injected during player construction:
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();
The
DefaultMediaSourceFactory
JavaDoc
describes the available options in more detail.
It's also possible to inject a custom MediaSource.Factory
implementation, for
example to support creation of a custom media source type. The factory's
createMediaSource(MediaItem)
will be called to create a media source for each
media item that is
added to the playlist.
The ExoPlayer
interface defines additional playlist methods that accept
media sources rather than media items. This makes it possible to bypass the
player's internal MediaSource.Factory
and pass media source instances to the
player directly:
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 provides multiple MediaSource
implementations to modify and compose
other MediaSource
instances. These are most useful in cases where multiple
customizations have to be combined and none of the simpler setup paths are
sufficient.
ClippingMediaSource
: Allows to clip media to a specified timestamp range.
If this is the only modification, it's preferable to use
MediaItem.ClippingConfiguration
instead.
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 instead.
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
instead.
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 methods like
Player.addMediaItem
instead.
SilenceMediaSource
: Generates silence for a specified duration that is
useful to fill gaps.
AdsMediaSource
: Extends a media source with client-side ad insertion
capabilities. Refer to the ad insertion guide for details.
ServerSideAdInsertionMediaSource
: Extends a media source with server-side ad
insertion capabilities. Refer to the ad insertion guide for details.
Content and code samples on this page are subject to the licenses described in the Content License. Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
Last updated 2024-02-09 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2024-02-09 UTC."],[],[],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."]]