xref: /aosp_15_r20/external/exoplayer/tree_15dc86382f17a24a3e881e52e31a810c1ea44b49/docs/dash.md (revision 30877f796caf59d855b10b687a5d6b3918d765cb)
1*30877f79SAndroid Build Coastguard Worker---
2*30877f79SAndroid Build Coastguard Workertitle: DASH
3*30877f79SAndroid Build Coastguard Worker---
4*30877f79SAndroid Build Coastguard Worker
5*30877f79SAndroid Build Coastguard Worker{% include_relative _page_fragments/supported-formats-dash.md %}
6*30877f79SAndroid Build Coastguard Worker
7*30877f79SAndroid Build Coastguard Worker## Using MediaItem ##
8*30877f79SAndroid Build Coastguard Worker
9*30877f79SAndroid Build Coastguard WorkerTo play a DASH stream, you need to depend on the DASH module.
10*30877f79SAndroid Build Coastguard Worker
11*30877f79SAndroid Build Coastguard Worker~~~
12*30877f79SAndroid Build Coastguard Workerimplementation 'com.google.android.exoplayer:exoplayer-dash:2.X.X'
13*30877f79SAndroid Build Coastguard Worker~~~
14*30877f79SAndroid Build Coastguard Worker{: .language-gradle}
15*30877f79SAndroid Build Coastguard Worker
16*30877f79SAndroid Build Coastguard WorkerYou can then create a `MediaItem` for a DASH MPD URI and pass it to the player.
17*30877f79SAndroid Build Coastguard Worker
18*30877f79SAndroid Build Coastguard Worker~~~
19*30877f79SAndroid Build Coastguard Worker// Create a player instance.
20*30877f79SAndroid Build Coastguard WorkerExoPlayer player = new ExoPlayer.Builder(context).build();
21*30877f79SAndroid Build Coastguard Worker// Set the media item to be played.
22*30877f79SAndroid Build Coastguard Workerplayer.setMediaItem(MediaItem.fromUri(dashUri));
23*30877f79SAndroid Build Coastguard Worker// Prepare the player.
24*30877f79SAndroid Build Coastguard Workerplayer.prepare();
25*30877f79SAndroid Build Coastguard Worker~~~
26*30877f79SAndroid Build Coastguard Worker{: .language-java}
27*30877f79SAndroid Build Coastguard Worker
28*30877f79SAndroid Build Coastguard WorkerIf your URI doesn't end with `.mpd`, you can pass `MimeTypes.APPLICATION_MPD`
29*30877f79SAndroid Build Coastguard Workerto `setMimeType` of `MediaItem.Builder` to explicitly indicate the type of the
30*30877f79SAndroid Build Coastguard Workercontent.
31*30877f79SAndroid Build Coastguard Worker
32*30877f79SAndroid Build Coastguard WorkerExoPlayer will automatically adapt between representations defined in the
33*30877f79SAndroid Build Coastguard Workermanifest, taking into account both available bandwidth and device capabilities.
34*30877f79SAndroid Build Coastguard Worker
35*30877f79SAndroid Build Coastguard Worker## Using DashMediaSource ##
36*30877f79SAndroid Build Coastguard Worker
37*30877f79SAndroid Build Coastguard WorkerFor more customization options, you can create a `DashMediaSource` and pass it
38*30877f79SAndroid Build Coastguard Workerdirectly to the player instead of a `MediaItem`.
39*30877f79SAndroid Build Coastguard Worker
40*30877f79SAndroid Build Coastguard Worker~~~
41*30877f79SAndroid Build Coastguard Worker// Create a data source factory.
42*30877f79SAndroid Build Coastguard WorkerDataSource.Factory dataSourceFactory = new DefaultHttpDataSource.Factory();
43*30877f79SAndroid Build Coastguard Worker// Create a DASH media source pointing to a DASH manifest uri.
44*30877f79SAndroid Build Coastguard WorkerMediaSource mediaSource =
45*30877f79SAndroid Build Coastguard Worker    new DashMediaSource.Factory(dataSourceFactory)
46*30877f79SAndroid Build Coastguard Worker        .createMediaSource(MediaItem.fromUri(dashUri));
47*30877f79SAndroid Build Coastguard Worker// Create a player instance.
48*30877f79SAndroid Build Coastguard WorkerExoPlayer player = new ExoPlayer.Builder(context).build();
49*30877f79SAndroid Build Coastguard Worker// Set the media source to be played.
50*30877f79SAndroid Build Coastguard Workerplayer.setMediaSource(mediaSource);
51*30877f79SAndroid Build Coastguard Worker// Prepare the player.
52*30877f79SAndroid Build Coastguard Workerplayer.prepare();
53*30877f79SAndroid Build Coastguard Worker~~~
54*30877f79SAndroid Build Coastguard Worker{: .language-java}
55*30877f79SAndroid Build Coastguard Worker
56*30877f79SAndroid Build Coastguard Worker## Accessing the manifest ##
57*30877f79SAndroid Build Coastguard Worker
58*30877f79SAndroid Build Coastguard WorkerYou can retrieve the current manifest by calling `Player.getCurrentManifest`.
59*30877f79SAndroid Build Coastguard WorkerFor DASH you should cast the returned object to `DashManifest`. The
60*30877f79SAndroid Build Coastguard Worker`onTimelineChanged` callback of `Player.Listener` is also called whenever
61*30877f79SAndroid Build Coastguard Workerthe manifest is loaded. This will happen once for a on-demand content, and
62*30877f79SAndroid Build Coastguard Workerpossibly many times for live content. The code snippet below shows how an app
63*30877f79SAndroid Build Coastguard Workercan do something whenever the manifest is loaded.
64*30877f79SAndroid Build Coastguard Worker
65*30877f79SAndroid Build Coastguard Worker~~~
66*30877f79SAndroid Build Coastguard Workerplayer.addListener(
67*30877f79SAndroid Build Coastguard Worker    new Player.Listener() {
68*30877f79SAndroid Build Coastguard Worker      @Override
69*30877f79SAndroid Build Coastguard Worker      public void onTimelineChanged(
70*30877f79SAndroid Build Coastguard Worker          Timeline timeline, @Player.TimelineChangeReason int reason) {
71*30877f79SAndroid Build Coastguard Worker        Object manifest = player.getCurrentManifest();
72*30877f79SAndroid Build Coastguard Worker        if (manifest != null) {
73*30877f79SAndroid Build Coastguard Worker          DashManifest dashManifest = (DashManifest) manifest;
74*30877f79SAndroid Build Coastguard Worker          // Do something with the manifest.
75*30877f79SAndroid Build Coastguard Worker        }
76*30877f79SAndroid Build Coastguard Worker      }
77*30877f79SAndroid Build Coastguard Worker    });
78*30877f79SAndroid Build Coastguard Worker~~~
79*30877f79SAndroid Build Coastguard Worker{: .language-java}
80*30877f79SAndroid Build Coastguard Worker
81*30877f79SAndroid Build Coastguard Worker## Customizing playback ##
82*30877f79SAndroid Build Coastguard Worker
83*30877f79SAndroid Build Coastguard WorkerExoPlayer provides multiple ways for you to tailor playback experience to your
84*30877f79SAndroid Build Coastguard Workerapp's needs. See the [Customization page][] for examples.
85*30877f79SAndroid Build Coastguard Worker
86*30877f79SAndroid Build Coastguard Worker[Customization page]: {{ site.baseurl }}/customization.html
87