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