1*30877f79SAndroid Build Coastguard Worker--- 2*30877f79SAndroid Build Coastguard Workertitle: Hello world! 3*30877f79SAndroid Build Coastguard Workerredirect_from: 4*30877f79SAndroid Build Coastguard Worker - /guide.html 5*30877f79SAndroid Build Coastguard Worker - /guide-v1.html 6*30877f79SAndroid Build Coastguard Worker - /getting-started.html 7*30877f79SAndroid Build Coastguard Worker--- 8*30877f79SAndroid Build Coastguard Worker 9*30877f79SAndroid Build Coastguard WorkerAnother way to get started is to work through 10*30877f79SAndroid Build Coastguard Worker[the ExoPlayer codelab](https://codelabs.developers.google.com/codelabs/exoplayer-intro/). 11*30877f79SAndroid Build Coastguard Worker{:.info} 12*30877f79SAndroid Build Coastguard Worker 13*30877f79SAndroid Build Coastguard WorkerFor simple use cases, getting started with `ExoPlayer` consists of implementing 14*30877f79SAndroid Build Coastguard Workerthe following steps: 15*30877f79SAndroid Build Coastguard Worker 16*30877f79SAndroid Build Coastguard Worker1. Add ExoPlayer as a dependency to your project. 17*30877f79SAndroid Build Coastguard Worker1. Create an `ExoPlayer` instance. 18*30877f79SAndroid Build Coastguard Worker1. Attach the player to a view (for video output and user input). 19*30877f79SAndroid Build Coastguard Worker1. Prepare the player with a `MediaItem` to play. 20*30877f79SAndroid Build Coastguard Worker1. Release the player when done. 21*30877f79SAndroid Build Coastguard Worker 22*30877f79SAndroid Build Coastguard WorkerThese steps are described in more detail below. For a complete example, refer to 23*30877f79SAndroid Build Coastguard Worker`PlayerActivity` in the [main demo app][]. 24*30877f79SAndroid Build Coastguard Worker 25*30877f79SAndroid Build Coastguard Worker## Adding ExoPlayer as a dependency ## 26*30877f79SAndroid Build Coastguard Worker 27*30877f79SAndroid Build Coastguard Worker### Add ExoPlayer modules ### 28*30877f79SAndroid Build Coastguard Worker 29*30877f79SAndroid Build Coastguard WorkerThe easiest way to get started using ExoPlayer is to add it as a gradle 30*30877f79SAndroid Build Coastguard Workerdependency in the `build.gradle` file of your app module. The following will add 31*30877f79SAndroid Build Coastguard Workera dependency to the full library: 32*30877f79SAndroid Build Coastguard Worker 33*30877f79SAndroid Build Coastguard Worker~~~ 34*30877f79SAndroid Build Coastguard Workerimplementation 'com.google.android.exoplayer:exoplayer:2.X.X' 35*30877f79SAndroid Build Coastguard Worker~~~ 36*30877f79SAndroid Build Coastguard Worker{: .language-gradle} 37*30877f79SAndroid Build Coastguard Worker 38*30877f79SAndroid Build Coastguard Workerwhere `2.X.X` is your preferred version (the latest version can be found by 39*30877f79SAndroid Build Coastguard Workerconsulting the [release notes][]). 40*30877f79SAndroid Build Coastguard Worker 41*30877f79SAndroid Build Coastguard WorkerAs an alternative to the full library, you can depend on only the library 42*30877f79SAndroid Build Coastguard Workermodules that you actually need. For example the following will add dependencies 43*30877f79SAndroid Build Coastguard Workeron the Core, DASH and UI library modules, as might be required for an app that 44*30877f79SAndroid Build Coastguard Workeronly plays DASH content: 45*30877f79SAndroid Build Coastguard Worker 46*30877f79SAndroid Build Coastguard Worker~~~ 47*30877f79SAndroid Build Coastguard Workerimplementation 'com.google.android.exoplayer:exoplayer-core:2.X.X' 48*30877f79SAndroid Build Coastguard Workerimplementation 'com.google.android.exoplayer:exoplayer-dash:2.X.X' 49*30877f79SAndroid Build Coastguard Workerimplementation 'com.google.android.exoplayer:exoplayer-ui:2.X.X' 50*30877f79SAndroid Build Coastguard Worker~~~ 51*30877f79SAndroid Build Coastguard Worker{: .language-gradle} 52*30877f79SAndroid Build Coastguard Worker 53*30877f79SAndroid Build Coastguard WorkerWhen depending on individual modules, they must all be the same version. You can 54*30877f79SAndroid Build Coastguard Workerbrowse the list of available modules on the [Google Maven ExoPlayer page][]. The 55*30877f79SAndroid Build Coastguard Workerfull library includes all of the library modules prefixed with `exoplayer-`, 56*30877f79SAndroid Build Coastguard Workerexcept for `exoplayer-transformer`. 57*30877f79SAndroid Build Coastguard Worker 58*30877f79SAndroid Build Coastguard WorkerIn addition to library modules, ExoPlayer has extension modules that depend on 59*30877f79SAndroid Build Coastguard Workerexternal libraries to provide additional functionality. Some extensions are 60*30877f79SAndroid Build Coastguard Workeravailable from the Maven repository, whereas others must be built manually. 61*30877f79SAndroid Build Coastguard WorkerBrowse the [extensions directory][] and their individual READMEs for details. 62*30877f79SAndroid Build Coastguard Worker 63*30877f79SAndroid Build Coastguard Worker### Turn on Java 8 support ### 64*30877f79SAndroid Build Coastguard Worker 65*30877f79SAndroid Build Coastguard WorkerIf not enabled already, you need to turn on Java 8 support in all `build.gradle` 66*30877f79SAndroid Build Coastguard Workerfiles depending on ExoPlayer, by adding the following to the `android` section: 67*30877f79SAndroid Build Coastguard Worker 68*30877f79SAndroid Build Coastguard Worker~~~ 69*30877f79SAndroid Build Coastguard WorkercompileOptions { 70*30877f79SAndroid Build Coastguard Worker targetCompatibility JavaVersion.VERSION_1_8 71*30877f79SAndroid Build Coastguard Worker} 72*30877f79SAndroid Build Coastguard Worker~~~ 73*30877f79SAndroid Build Coastguard Worker{: .language-gradle} 74*30877f79SAndroid Build Coastguard Worker 75*30877f79SAndroid Build Coastguard Worker### Enable multidex ### 76*30877f79SAndroid Build Coastguard Worker 77*30877f79SAndroid Build Coastguard WorkerIf your Gradle `minSdkVersion` is 20 or lower, you should 78*30877f79SAndroid Build Coastguard Worker[enable multidex](https://developer.android.com/studio/build/multidex) in order 79*30877f79SAndroid Build Coastguard Workerto prevent build errors. 80*30877f79SAndroid Build Coastguard Worker 81*30877f79SAndroid Build Coastguard Worker## Creating the player ## 82*30877f79SAndroid Build Coastguard Worker 83*30877f79SAndroid Build Coastguard WorkerYou can create an `ExoPlayer` instance using `ExoPlayer.Builder`, which provides 84*30877f79SAndroid Build Coastguard Workera range of customization options. The code below is the simplest example of 85*30877f79SAndroid Build Coastguard Workercreating an instance. 86*30877f79SAndroid Build Coastguard Worker 87*30877f79SAndroid Build Coastguard Worker~~~ 88*30877f79SAndroid Build Coastguard WorkerExoPlayer player = new ExoPlayer.Builder(context).build(); 89*30877f79SAndroid Build Coastguard Worker~~~ 90*30877f79SAndroid Build Coastguard Worker{: .language-java} 91*30877f79SAndroid Build Coastguard Worker 92*30877f79SAndroid Build Coastguard Worker### A note on threading ### 93*30877f79SAndroid Build Coastguard Worker 94*30877f79SAndroid Build Coastguard WorkerExoPlayer instances must be accessed from a single application thread. For the 95*30877f79SAndroid Build Coastguard Workervast majority of cases this should be the application's main thread. Using the 96*30877f79SAndroid Build Coastguard Workerapplication's main thread is a requirement when using ExoPlayer's UI components 97*30877f79SAndroid Build Coastguard Workeror the IMA extension. 98*30877f79SAndroid Build Coastguard Worker 99*30877f79SAndroid Build Coastguard WorkerThe thread on which an ExoPlayer instance must be accessed can be explicitly 100*30877f79SAndroid Build Coastguard Workerspecified by passing a `Looper` when creating the player. If no `Looper` is 101*30877f79SAndroid Build Coastguard Workerspecified, then the `Looper` of the thread that the player is created on is 102*30877f79SAndroid Build Coastguard Workerused, or if that thread does not have a `Looper`, the `Looper` of the 103*30877f79SAndroid Build Coastguard Workerapplication's main thread is used. In all cases the `Looper` of the thread from 104*30877f79SAndroid Build Coastguard Workerwhich the player must be accessed can be queried using 105*30877f79SAndroid Build Coastguard Worker`Player.getApplicationLooper`. 106*30877f79SAndroid Build Coastguard Worker 107*30877f79SAndroid Build Coastguard WorkerIf you see `IllegalStateException` being thrown with the message "Player is 108*30877f79SAndroid Build Coastguard Workeraccessed on the wrong thread", then some code in your app is accessing an 109*30877f79SAndroid Build Coastguard Worker`ExoPlayer` instance on the wrong thread (the exception's stack trace shows you 110*30877f79SAndroid Build Coastguard Workerwhere). 111*30877f79SAndroid Build Coastguard Worker{:.info} 112*30877f79SAndroid Build Coastguard Worker 113*30877f79SAndroid Build Coastguard WorkerFor more information about ExoPlayer's threading model, see the 114*30877f79SAndroid Build Coastguard Worker["Threading model" section of the ExoPlayer Javadoc][]. 115*30877f79SAndroid Build Coastguard Worker 116*30877f79SAndroid Build Coastguard Worker## Attaching the player to a view ## 117*30877f79SAndroid Build Coastguard Worker 118*30877f79SAndroid Build Coastguard WorkerThe ExoPlayer library provides a range of pre-built UI components for media 119*30877f79SAndroid Build Coastguard Workerplayback. These include `StyledPlayerView`, which encapsulates a 120*30877f79SAndroid Build Coastguard Worker`StyledPlayerControlView`, a `SubtitleView`, and a `Surface` onto which video is 121*30877f79SAndroid Build Coastguard Workerrendered. A `StyledPlayerView` can be included in your application's layout xml. 122*30877f79SAndroid Build Coastguard WorkerBinding the player to the view is as simple as: 123*30877f79SAndroid Build Coastguard Worker 124*30877f79SAndroid Build Coastguard Worker~~~ 125*30877f79SAndroid Build Coastguard Worker// Bind the player to the view. 126*30877f79SAndroid Build Coastguard WorkerplayerView.setPlayer(player); 127*30877f79SAndroid Build Coastguard Worker~~~ 128*30877f79SAndroid Build Coastguard Worker{: .language-java} 129*30877f79SAndroid Build Coastguard Worker 130*30877f79SAndroid Build Coastguard WorkerYou can also use `StyledPlayerControlView` as a standalone component, which is 131*30877f79SAndroid Build Coastguard Workeruseful for audio only use cases. 132*30877f79SAndroid Build Coastguard Worker 133*30877f79SAndroid Build Coastguard WorkerUse of ExoPlayer's pre-built UI components is optional. For video applications 134*30877f79SAndroid Build Coastguard Workerthat implement their own UI, the target `SurfaceView`, `TextureView`, 135*30877f79SAndroid Build Coastguard Worker`SurfaceHolder` or `Surface` can be set using `ExoPlayer`'s 136*30877f79SAndroid Build Coastguard Worker`setVideoSurfaceView`, `setVideoTextureView`, `setVideoSurfaceHolder` and 137*30877f79SAndroid Build Coastguard Worker`setVideoSurface` methods respectively. `ExoPlayer`'s `addTextOutput` method can 138*30877f79SAndroid Build Coastguard Workerbe used to receive captions that should be rendered during playback. 139*30877f79SAndroid Build Coastguard Worker 140*30877f79SAndroid Build Coastguard Worker## Populating the playlist and preparing the player ## 141*30877f79SAndroid Build Coastguard Worker 142*30877f79SAndroid Build Coastguard WorkerIn ExoPlayer every piece of media is represented by a `MediaItem`. To play a 143*30877f79SAndroid Build Coastguard Workerpiece of media you need to build a corresponding `MediaItem`, add it to the 144*30877f79SAndroid Build Coastguard Workerplayer, prepare the player, and call `play` to start the playback: 145*30877f79SAndroid Build Coastguard Worker 146*30877f79SAndroid Build Coastguard Worker~~~ 147*30877f79SAndroid Build Coastguard Worker// Build the media item. 148*30877f79SAndroid Build Coastguard WorkerMediaItem mediaItem = MediaItem.fromUri(videoUri); 149*30877f79SAndroid Build Coastguard Worker// Set the media item to be played. 150*30877f79SAndroid Build Coastguard Workerplayer.setMediaItem(mediaItem); 151*30877f79SAndroid Build Coastguard Worker// Prepare the player. 152*30877f79SAndroid Build Coastguard Workerplayer.prepare(); 153*30877f79SAndroid Build Coastguard Worker// Start the playback. 154*30877f79SAndroid Build Coastguard Workerplayer.play(); 155*30877f79SAndroid Build Coastguard Worker~~~ 156*30877f79SAndroid Build Coastguard Worker{: .language-java} 157*30877f79SAndroid Build Coastguard Worker 158*30877f79SAndroid Build Coastguard WorkerExoPlayer supports playlists directly, so it's possible to prepare the player 159*30877f79SAndroid Build Coastguard Workerwith multiple media items to be played one after the other: 160*30877f79SAndroid Build Coastguard Worker 161*30877f79SAndroid Build Coastguard Worker~~~ 162*30877f79SAndroid Build Coastguard Worker// Build the media items. 163*30877f79SAndroid Build Coastguard WorkerMediaItem firstItem = MediaItem.fromUri(firstVideoUri); 164*30877f79SAndroid Build Coastguard WorkerMediaItem secondItem = MediaItem.fromUri(secondVideoUri); 165*30877f79SAndroid Build Coastguard Worker// Add the media items to be played. 166*30877f79SAndroid Build Coastguard Workerplayer.addMediaItem(firstItem); 167*30877f79SAndroid Build Coastguard Workerplayer.addMediaItem(secondItem); 168*30877f79SAndroid Build Coastguard Worker// Prepare the player. 169*30877f79SAndroid Build Coastguard Workerplayer.prepare(); 170*30877f79SAndroid Build Coastguard Worker// Start the playback. 171*30877f79SAndroid Build Coastguard Workerplayer.play(); 172*30877f79SAndroid Build Coastguard Worker~~~ 173*30877f79SAndroid Build Coastguard Worker{: .language-java} 174*30877f79SAndroid Build Coastguard Worker 175*30877f79SAndroid Build Coastguard WorkerThe playlist can be updated during playback without the need to prepare the 176*30877f79SAndroid Build Coastguard Workerplayer again. Read more about populating and manipulating the playlist on the 177*30877f79SAndroid Build Coastguard Worker[Playlists page][]. Read more about the different options available when 178*30877f79SAndroid Build Coastguard Workerbuilding media items, such as clipping and attaching subtitle files, on the 179*30877f79SAndroid Build Coastguard Worker[Media items page][]. 180*30877f79SAndroid Build Coastguard Worker 181*30877f79SAndroid Build Coastguard WorkerPrior to ExoPlayer 2.12, the player needed to be given a `MediaSource` rather 182*30877f79SAndroid Build Coastguard Workerthan media items. From 2.12 onwards, the player converts media items to the 183*30877f79SAndroid Build Coastguard Worker`MediaSource` instances that it needs internally. Read more about this process 184*30877f79SAndroid Build Coastguard Workerand how it can be customized on the [Media sources page][]. It's still possible 185*30877f79SAndroid Build Coastguard Workerto provide `MediaSource` instances directly to the player using 186*30877f79SAndroid Build Coastguard Worker`ExoPlayer.setMediaSource(s)` and `ExoPlayer.addMediaSource(s)`. 187*30877f79SAndroid Build Coastguard Worker{:.info} 188*30877f79SAndroid Build Coastguard Worker 189*30877f79SAndroid Build Coastguard Worker## Controlling the player ## 190*30877f79SAndroid Build Coastguard Worker 191*30877f79SAndroid Build Coastguard WorkerOnce the player has been prepared, playback can be controlled by calling methods 192*30877f79SAndroid Build Coastguard Workeron the player. Some of the most commonly used methods are listed below. 193*30877f79SAndroid Build Coastguard Worker 194*30877f79SAndroid Build Coastguard Worker* `play` and `pause` start and pause playback. 195*30877f79SAndroid Build Coastguard Worker* `seekTo` allows seeking within the media. 196*30877f79SAndroid Build Coastguard Worker* `hasPrevious`, `hasNext`, `previous` and `next` allow navigating through the 197*30877f79SAndroid Build Coastguard Worker playlist. 198*30877f79SAndroid Build Coastguard Worker* `setRepeatMode` controls if and how media is looped. 199*30877f79SAndroid Build Coastguard Worker* `setShuffleModeEnabled` controls playlist shuffling. 200*30877f79SAndroid Build Coastguard Worker* `setPlaybackParameters` adjusts playback speed and audio pitch. 201*30877f79SAndroid Build Coastguard Worker 202*30877f79SAndroid Build Coastguard WorkerIf the player is bound to a `StyledPlayerView` or `StyledPlayerControlView`, 203*30877f79SAndroid Build Coastguard Workerthen user interaction with these components will cause corresponding methods on 204*30877f79SAndroid Build Coastguard Workerthe player to be invoked. 205*30877f79SAndroid Build Coastguard Worker 206*30877f79SAndroid Build Coastguard Worker## Releasing the player ## 207*30877f79SAndroid Build Coastguard Worker 208*30877f79SAndroid Build Coastguard WorkerIt's important to release the player when it's no longer needed, so as to free 209*30877f79SAndroid Build Coastguard Workerup limited resources such as video decoders for use by other applications. This 210*30877f79SAndroid Build Coastguard Workercan be done by calling `ExoPlayer.release`. 211*30877f79SAndroid Build Coastguard Worker 212*30877f79SAndroid Build Coastguard Worker[main demo app]: {{ site.release_v2 }}/demos/main/ 213*30877f79SAndroid Build Coastguard Worker[extensions directory]: {{ site.release_v2 }}/extensions/ 214*30877f79SAndroid Build Coastguard Worker[release notes]: {{ site.release_v2 }}/RELEASENOTES.md 215*30877f79SAndroid Build Coastguard Worker["Threading model" section of the ExoPlayer Javadoc]: {{ site.exo_sdk }}/ExoPlayer.html 216*30877f79SAndroid Build Coastguard Worker[Playlists page]: {{ site.baseurl }}/playlists.html 217*30877f79SAndroid Build Coastguard Worker[Media items page]: {{ site.baseurl }}/media-items.html 218*30877f79SAndroid Build Coastguard Worker[Media sources page]: {{ site.baseurl }}/media-sources.html 219*30877f79SAndroid Build Coastguard Worker[Google Maven ExoPlayer page]: https://maven.google.com/web/index.html#com.google.android.exoplayer 220