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. 54*30877f79SAndroid Build Coastguard Worker 55*30877f79SAndroid Build Coastguard WorkerThe available library modules are listed below. Adding a dependency to the full 56*30877f79SAndroid Build Coastguard WorkerExoPlayer library is equivalent to adding dependencies on all of the library 57*30877f79SAndroid Build Coastguard Workermodules individually. 58*30877f79SAndroid Build Coastguard Worker 59*30877f79SAndroid Build Coastguard Worker* `exoplayer-core`: Core functionality (required). 60*30877f79SAndroid Build Coastguard Worker* `exoplayer-dash`: Support for DASH content. 61*30877f79SAndroid Build Coastguard Worker* `exoplayer-hls`: Support for HLS content. 62*30877f79SAndroid Build Coastguard Worker* `exoplayer-rtsp`: Support for RTSP content. 63*30877f79SAndroid Build Coastguard Worker* `exoplayer-smoothstreaming`: Support for SmoothStreaming content. 64*30877f79SAndroid Build Coastguard Worker* `exoplayer-transformer`: Media transformation functionality. 65*30877f79SAndroid Build Coastguard Worker* `exoplayer-ui`: UI components and resources for use with ExoPlayer. 66*30877f79SAndroid Build Coastguard Worker 67*30877f79SAndroid Build Coastguard WorkerIn addition to library modules, ExoPlayer has extension modules that depend on 68*30877f79SAndroid Build Coastguard Workerexternal libraries to provide additional functionality. Some extensions are 69*30877f79SAndroid Build Coastguard Workeravailable from the Maven repository, whereas others must be built manually. 70*30877f79SAndroid Build Coastguard WorkerBrowse the [extensions directory][] and their individual READMEs for details. 71*30877f79SAndroid Build Coastguard Worker 72*30877f79SAndroid Build Coastguard WorkerMore information on the library and extension modules that are available can be 73*30877f79SAndroid Build Coastguard Workerfound on the [Google Maven ExoPlayer page][]. 74*30877f79SAndroid Build Coastguard Worker 75*30877f79SAndroid Build Coastguard Worker### Turn on Java 8 support ### 76*30877f79SAndroid Build Coastguard Worker 77*30877f79SAndroid Build Coastguard WorkerIf not enabled already, you need to turn on Java 8 support in all `build.gradle` 78*30877f79SAndroid Build Coastguard Workerfiles depending on ExoPlayer, by adding the following to the `android` section: 79*30877f79SAndroid Build Coastguard Worker 80*30877f79SAndroid Build Coastguard Worker~~~ 81*30877f79SAndroid Build Coastguard WorkercompileOptions { 82*30877f79SAndroid Build Coastguard Worker targetCompatibility JavaVersion.VERSION_1_8 83*30877f79SAndroid Build Coastguard Worker} 84*30877f79SAndroid Build Coastguard Worker~~~ 85*30877f79SAndroid Build Coastguard Worker{: .language-gradle} 86*30877f79SAndroid Build Coastguard Worker 87*30877f79SAndroid Build Coastguard Worker### Enable multidex ### 88*30877f79SAndroid Build Coastguard Worker 89*30877f79SAndroid Build Coastguard WorkerIf your Gradle `minSdkVersion` is 20 or lower, you should 90*30877f79SAndroid Build Coastguard Worker[enable multidex](https://developer.android.com/studio/build/multidex) in order 91*30877f79SAndroid Build Coastguard Workerto prevent build errors. 92*30877f79SAndroid Build Coastguard Worker 93*30877f79SAndroid Build Coastguard Worker## Creating the player ## 94*30877f79SAndroid Build Coastguard Worker 95*30877f79SAndroid Build Coastguard WorkerYou can create an `ExoPlayer` instance using `ExoPlayer.Builder`, which provides 96*30877f79SAndroid Build Coastguard Workera range of customization options. The code below is the simplest example of 97*30877f79SAndroid Build Coastguard Workercreating an instance. 98*30877f79SAndroid Build Coastguard Worker 99*30877f79SAndroid Build Coastguard Worker~~~ 100*30877f79SAndroid Build Coastguard WorkerExoPlayer player = new ExoPlayer.Builder(context).build(); 101*30877f79SAndroid Build Coastguard Worker~~~ 102*30877f79SAndroid Build Coastguard Worker{: .language-java} 103*30877f79SAndroid Build Coastguard Worker 104*30877f79SAndroid Build Coastguard Worker### A note on threading ### 105*30877f79SAndroid Build Coastguard Worker 106*30877f79SAndroid Build Coastguard WorkerExoPlayer instances must be accessed from a single application thread. For the 107*30877f79SAndroid Build Coastguard Workervast majority of cases this should be the application's main thread. Using the 108*30877f79SAndroid Build Coastguard Workerapplication's main thread is a requirement when using ExoPlayer's UI components 109*30877f79SAndroid Build Coastguard Workeror the IMA extension. 110*30877f79SAndroid Build Coastguard Worker 111*30877f79SAndroid Build Coastguard WorkerThe thread on which an ExoPlayer instance must be accessed can be explicitly 112*30877f79SAndroid Build Coastguard Workerspecified by passing a `Looper` when creating the player. If no `Looper` is 113*30877f79SAndroid Build Coastguard Workerspecified, then the `Looper` of the thread that the player is created on is 114*30877f79SAndroid Build Coastguard Workerused, or if that thread does not have a `Looper`, the `Looper` of the 115*30877f79SAndroid Build Coastguard Workerapplication's main thread is used. In all cases the `Looper` of the thread from 116*30877f79SAndroid Build Coastguard Workerwhich the player must be accessed can be queried using 117*30877f79SAndroid Build Coastguard Worker`Player.getApplicationLooper`. 118*30877f79SAndroid Build Coastguard Worker 119*30877f79SAndroid Build Coastguard WorkerIf you see `IllegalStateException` being thrown with the message "Player is 120*30877f79SAndroid Build Coastguard Workeraccessed on the wrong thread", then some code in your app is accessing an 121*30877f79SAndroid Build Coastguard Worker`ExoPlayer` instance on the wrong thread (the exception's stack trace shows you 122*30877f79SAndroid Build Coastguard Workerwhere). You can temporarily opt out from these exceptions being thrown by 123*30877f79SAndroid Build Coastguard Workercalling `ExoPlayer.setThrowsWhenUsingWrongThread(false)`, in which case the 124*30877f79SAndroid Build Coastguard Workerissue will be logged as a warning instead. Using this opt out is not safe and 125*30877f79SAndroid Build Coastguard Workermay result in unexpected or obscure errors. It will be removed in ExoPlayer 126*30877f79SAndroid Build Coastguard Worker2.16. 127*30877f79SAndroid Build Coastguard Worker{:.info} 128*30877f79SAndroid Build Coastguard Worker 129*30877f79SAndroid Build Coastguard WorkerFor more information about ExoPlayer's threading model, see the 130*30877f79SAndroid Build Coastguard Worker["Threading model" section of the ExoPlayer Javadoc][]. 131*30877f79SAndroid Build Coastguard Worker 132*30877f79SAndroid Build Coastguard Worker## Attaching the player to a view ## 133*30877f79SAndroid Build Coastguard Worker 134*30877f79SAndroid Build Coastguard WorkerThe ExoPlayer library provides a range of pre-built UI components for media 135*30877f79SAndroid Build Coastguard Workerplayback. These include `StyledPlayerView`, which encapsulates a 136*30877f79SAndroid Build Coastguard Worker`StyledPlayerControlView`, a `SubtitleView`, and a `Surface` onto which video is 137*30877f79SAndroid Build Coastguard Workerrendered. A `StyledPlayerView` can be included in your application's layout xml. 138*30877f79SAndroid Build Coastguard WorkerBinding the player to the view is as simple as: 139*30877f79SAndroid Build Coastguard Worker 140*30877f79SAndroid Build Coastguard Worker~~~ 141*30877f79SAndroid Build Coastguard Worker// Bind the player to the view. 142*30877f79SAndroid Build Coastguard WorkerplayerView.setPlayer(player); 143*30877f79SAndroid Build Coastguard Worker~~~ 144*30877f79SAndroid Build Coastguard Worker{: .language-java} 145*30877f79SAndroid Build Coastguard Worker 146*30877f79SAndroid Build Coastguard WorkerYou can also use `StyledPlayerControlView` as a standalone component, which is 147*30877f79SAndroid Build Coastguard Workeruseful for audio only use cases. 148*30877f79SAndroid Build Coastguard Worker 149*30877f79SAndroid Build Coastguard WorkerUse of ExoPlayer's pre-built UI components is optional. For video applications 150*30877f79SAndroid Build Coastguard Workerthat implement their own UI, the target `SurfaceView`, `TextureView`, 151*30877f79SAndroid Build Coastguard Worker`SurfaceHolder` or `Surface` can be set using `ExoPlayer`'s 152*30877f79SAndroid Build Coastguard Worker`setVideoSurfaceView`, `setVideoTextureView`, `setVideoSurfaceHolder` and 153*30877f79SAndroid Build Coastguard Worker`setVideoSurface` methods respectively. `ExoPlayer`'s `addTextOutput` method can 154*30877f79SAndroid Build Coastguard Workerbe used to receive captions that should be rendered during playback. 155*30877f79SAndroid Build Coastguard Worker 156*30877f79SAndroid Build Coastguard Worker## Populating the playlist and preparing the player ## 157*30877f79SAndroid Build Coastguard Worker 158*30877f79SAndroid Build Coastguard WorkerIn ExoPlayer every piece of media is represented by a `MediaItem`. To play a 159*30877f79SAndroid Build Coastguard Workerpiece of media you need to build a corresponding `MediaItem`, add it to the 160*30877f79SAndroid Build Coastguard Workerplayer, prepare the player, and call `play` to start the playback: 161*30877f79SAndroid Build Coastguard Worker 162*30877f79SAndroid Build Coastguard Worker~~~ 163*30877f79SAndroid Build Coastguard Worker// Build the media item. 164*30877f79SAndroid Build Coastguard WorkerMediaItem mediaItem = MediaItem.fromUri(videoUri); 165*30877f79SAndroid Build Coastguard Worker// Set the media item to be played. 166*30877f79SAndroid Build Coastguard Workerplayer.setMediaItem(mediaItem); 167*30877f79SAndroid Build Coastguard Worker// Prepare the player. 168*30877f79SAndroid Build Coastguard Workerplayer.prepare(); 169*30877f79SAndroid Build Coastguard Worker// Start the playback. 170*30877f79SAndroid Build Coastguard Workerplayer.play(); 171*30877f79SAndroid Build Coastguard Worker~~~ 172*30877f79SAndroid Build Coastguard Worker{: .language-java} 173*30877f79SAndroid Build Coastguard Worker 174*30877f79SAndroid Build Coastguard WorkerExoPlayer supports playlists directly, so it's possible to prepare the player 175*30877f79SAndroid Build Coastguard Workerwith multiple media items to be played one after the other: 176*30877f79SAndroid Build Coastguard Worker 177*30877f79SAndroid Build Coastguard Worker~~~ 178*30877f79SAndroid Build Coastguard Worker// Build the media items. 179*30877f79SAndroid Build Coastguard WorkerMediaItem firstItem = MediaItem.fromUri(firstVideoUri); 180*30877f79SAndroid Build Coastguard WorkerMediaItem secondItem = MediaItem.fromUri(secondVideoUri); 181*30877f79SAndroid Build Coastguard Worker// Add the media items to be played. 182*30877f79SAndroid Build Coastguard Workerplayer.addMediaItem(firstItem); 183*30877f79SAndroid Build Coastguard Workerplayer.addMediaItem(secondItem); 184*30877f79SAndroid Build Coastguard Worker// Prepare the player. 185*30877f79SAndroid Build Coastguard Workerplayer.prepare(); 186*30877f79SAndroid Build Coastguard Worker// Start the playback. 187*30877f79SAndroid Build Coastguard Workerplayer.play(); 188*30877f79SAndroid Build Coastguard Worker~~~ 189*30877f79SAndroid Build Coastguard Worker{: .language-java} 190*30877f79SAndroid Build Coastguard Worker 191*30877f79SAndroid Build Coastguard WorkerThe playlist can be updated during playback without the need to prepare the 192*30877f79SAndroid Build Coastguard Workerplayer again. Read more about populating and manipulating the playlist on the 193*30877f79SAndroid Build Coastguard Worker[Playlists page][]. Read more about the different options available when 194*30877f79SAndroid Build Coastguard Workerbuilding media items, such as clipping and attaching subtitle files, on the 195*30877f79SAndroid Build Coastguard Worker[Media items page][]. 196*30877f79SAndroid Build Coastguard Worker 197*30877f79SAndroid Build Coastguard WorkerPrior to ExoPlayer 2.12, the player needed to be given a `MediaSource` rather 198*30877f79SAndroid Build Coastguard Workerthan media items. From 2.12 onwards, the player converts media items to the 199*30877f79SAndroid Build Coastguard Worker`MediaSource` instances that it needs internally. Read more about this process 200*30877f79SAndroid Build Coastguard Workerand how it can be customized on the [Media sources page][]. It's still possible 201*30877f79SAndroid Build Coastguard Workerto provide `MediaSource` instances directly to the player using 202*30877f79SAndroid Build Coastguard Worker`ExoPlayer.setMediaSource(s)` and `ExoPlayer.addMediaSource(s)`. 203*30877f79SAndroid Build Coastguard Worker{:.info} 204*30877f79SAndroid Build Coastguard Worker 205*30877f79SAndroid Build Coastguard Worker## Controlling the player ## 206*30877f79SAndroid Build Coastguard Worker 207*30877f79SAndroid Build Coastguard WorkerOnce the player has been prepared, playback can be controlled by calling methods 208*30877f79SAndroid Build Coastguard Workeron the player. Some of the most commonly used methods are listed below. 209*30877f79SAndroid Build Coastguard Worker 210*30877f79SAndroid Build Coastguard Worker* `play` and `pause` start and pause playback. 211*30877f79SAndroid Build Coastguard Worker* `seekTo` allows seeking within the media. 212*30877f79SAndroid Build Coastguard Worker* `hasPrevious`, `hasNext`, `previous` and `next` allow navigating through the 213*30877f79SAndroid Build Coastguard Worker playlist. 214*30877f79SAndroid Build Coastguard Worker* `setRepeatMode` controls if and how media is looped. 215*30877f79SAndroid Build Coastguard Worker* `setShuffleModeEnabled` controls playlist shuffling. 216*30877f79SAndroid Build Coastguard Worker* `setPlaybackParameters` adjusts playback speed and audio pitch. 217*30877f79SAndroid Build Coastguard Worker 218*30877f79SAndroid Build Coastguard WorkerIf the player is bound to a `StyledPlayerView` or `StyledPlayerControlView`, 219*30877f79SAndroid Build Coastguard Workerthen user interaction with these components will cause corresponding methods on 220*30877f79SAndroid Build Coastguard Workerthe player to be invoked. 221*30877f79SAndroid Build Coastguard Worker 222*30877f79SAndroid Build Coastguard Worker## Releasing the player ## 223*30877f79SAndroid Build Coastguard Worker 224*30877f79SAndroid Build Coastguard WorkerIt's important to release the player when it's no longer needed, so as to free 225*30877f79SAndroid Build Coastguard Workerup limited resources such as video decoders for use by other applications. This 226*30877f79SAndroid Build Coastguard Workercan be done by calling `ExoPlayer.release`. 227*30877f79SAndroid Build Coastguard Worker 228*30877f79SAndroid Build Coastguard Worker[main demo app]: {{ site.release_v2 }}/demos/main/ 229*30877f79SAndroid Build Coastguard Worker[extensions directory]: {{ site.release_v2 }}/extensions/ 230*30877f79SAndroid Build Coastguard Worker[release notes]: {{ site.release_v2 }}/RELEASENOTES.md 231*30877f79SAndroid Build Coastguard Worker["Threading model" section of the ExoPlayer Javadoc]: {{ site.exo_sdk }}/ExoPlayer.html 232*30877f79SAndroid Build Coastguard Worker[Playlists page]: {{ site.baseurl }}/playlists.html 233*30877f79SAndroid Build Coastguard Worker[Media items page]: {{ site.baseurl }}/media-items.html 234*30877f79SAndroid Build Coastguard Worker[Media sources page]: {{ site.baseurl }}/media-sources.html 235*30877f79SAndroid Build Coastguard Worker[Google Maven ExoPlayer page]: https://maven.google.com/web/index.html#com.google.android.exoplayer 236