This documentation may be out-of-date. Please refer to the documentation for the latest ExoPlayer release on developer.android.com.
In ExoPlayer every piece of media is represented by a
internally, the player needs
MediaSource instances to play the content. The
player creates these from media items using a
By default the player uses a
DefaultMediaSourceFactory, which can create
instances of the following content
ProgressiveMediaSourcefor regular media files.
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.
Customizing media source creation
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:
MediaSource.Factory mediaSourceFactory = new DefaultMediaSourceFactory(context) .setDataSourceFactory(cacheDataSourceFactory) .setLocalAdInsertionComponents( adsLoaderProvider, /* adViewProvider= */ playerView); ExoPlayer player = new ExoPlayer.Builder(context) .setMediaSourceFactory(mediaSourceFactory) .build();
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.
Media source based playlist API
ExoPlayer interface defines additional playlist methods that accept
media sources rather than media items. This makes it possible to bypass the
MediaSource.Factory and pass media source instances to the
// 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();