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