1---
2title: Media sources
3redirect_from:
4  - /mediasource.html
5---
6
7In ExoPlayer every piece of media is represented by a `MediaItem`. However
8internally, the player needs `MediaSource` instances to play the content. The
9player creates these from media items using a `MediaSource.Factory`.
10
11By default the player uses a `DefaultMediaSourceFactory`, which can create
12instances of the following content `MediaSource` implementations:
13
14* `DashMediaSource` for [DASH][].
15* `SsMediaSource` for [SmoothStreaming][].
16* `HlsMediaSource` for [HLS][].
17* `ProgressiveMediaSource` for [regular media files][].
18* `RtspMediaSource` for [RTSP][].
19
20`DefaultMediaSourceFactory` can also create more complex media sources depending
21on the properties of the corresponding media items. This is described in more
22detail on the [Media items page]({{ site.baseurl }}/media-items.html).
23
24For apps that need media source setups that are not supported by the
25default configuration of the player, there are several options for
26customization.
27
28## Customizing media source creation ##
29
30When building the player, a `MediaSource.Factory` can be injected. For example,
31if an app wants to insert ads and use a `CacheDataSource.Factory` to support
32caching, an instance of `DefaultMediaSourceFactory` can be configured to match
33these requirements and injected during player construction:
34
35~~~
36MediaSource.Factory mediaSourceFactory =
37    new DefaultMediaSourceFactory(cacheDataSourceFactory)
38        .setAdsLoaderProvider(adsLoaderProvider)
39        .setAdViewProvider(playerView);
40ExoPlayer player = new ExoPlayer.Builder(context)
41    .setMediaSourceFactory(mediaSourceFactory)
42    .build();
43~~~
44{: .language-java}
45
46The
47[`DefaultMediaSourceFactory` JavaDoc]({{ site.baseurl }}/doc/reference/com/google/android/exoplayer2/source/DefaultMediaSourceFactory.html)
48describes the available options in more detail.
49
50It's also possible to inject a custom `MediaSource.Factory` implementation, for
51example to support creation of a custom media source type. The factory's
52`createMediaSource(MediaItem)` will be called to create a media source for each
53media item that is
54[added to the playlist]({{ site.baseurl }}/playlists.html).
55
56## Media source based playlist API ##
57
58The [`ExoPlayer`] interface defines additional playlist methods that accept
59media sources rather than media items. This makes it possible to bypass the
60player's internal `MediaSource.Factory` and pass media source instances to the
61player directly:
62
63~~~
64// Set a list of media sources as initial playlist.
65exoPlayer.setMediaSources(listOfMediaSources);
66// Add a single media source.
67exoPlayer.addMediaSource(anotherMediaSource);
68
69// Can be combined with the media item API.
70exoPlayer.addMediaItem(/* index= */ 3, MediaItem.fromUri(videoUri));
71
72exoPlayer.prepare();
73exoPlayer.play();
74~~~
75{: .language-java}
76
77[DASH]: {{ site.baseurl }}/dash.html
78[SmoothStreaming]: {{ site.baseurl }}/smoothstreaming.html
79[HLS]: {{ site.baseurl }}/hls.html
80[RTSP]: {{ site.baseurl }}/rtsp.html
81[regular media files]: {{ site.baseurl }}/progressive.html
82[`ExoPlayer`]: {{ site.baseurl }}/doc/reference/com/google/android/exoplayer2/ExoPlayer.html
83