1*30877f79SAndroid Build Coastguard Worker# ExoPlayer IMA module
2*30877f79SAndroid Build Coastguard Worker
3*30877f79SAndroid Build Coastguard WorkerThe ExoPlayer IMA module provides an [AdsLoader][] implementation wrapping the
4*30877f79SAndroid Build Coastguard Worker[Interactive Media Ads SDK for Android][IMA]. You can use it to insert ads into
5*30877f79SAndroid Build Coastguard Workercontent played using ExoPlayer.
6*30877f79SAndroid Build Coastguard Worker
7*30877f79SAndroid Build Coastguard Worker[IMA]: https://developers.google.com/interactive-media-ads/docs/sdks/android/
8*30877f79SAndroid Build Coastguard Worker[AdsLoader]: https://exoplayer.dev/doc/reference/com/google/android/exoplayer2/source/ads/AdsLoader.html
9*30877f79SAndroid Build Coastguard Worker
10*30877f79SAndroid Build Coastguard Worker## Getting the module
11*30877f79SAndroid Build Coastguard Worker
12*30877f79SAndroid Build Coastguard WorkerThe easiest way to get the module is to add it as a gradle dependency:
13*30877f79SAndroid Build Coastguard Worker
14*30877f79SAndroid Build Coastguard Worker```gradle
15*30877f79SAndroid Build Coastguard Workerimplementation 'com.google.android.exoplayer:extension-ima:2.X.X'
16*30877f79SAndroid Build Coastguard Worker```
17*30877f79SAndroid Build Coastguard Worker
18*30877f79SAndroid Build Coastguard Workerwhere `2.X.X` is the version, which must match the version of the other media
19*30877f79SAndroid Build Coastguard Workermodules being used.
20*30877f79SAndroid Build Coastguard Worker
21*30877f79SAndroid Build Coastguard WorkerAlternatively, you can clone this GitHub project and depend on the module
22*30877f79SAndroid Build Coastguard Workerlocally. Instructions for doing this can be found in the [top level README][].
23*30877f79SAndroid Build Coastguard Worker
24*30877f79SAndroid Build Coastguard Worker[top level README]: https://github.com/google/ExoPlayer/blob/release-v2/README.md
25*30877f79SAndroid Build Coastguard Worker
26*30877f79SAndroid Build Coastguard Worker## Using the module
27*30877f79SAndroid Build Coastguard Worker
28*30877f79SAndroid Build Coastguard WorkerTo use the module, follow the instructions on the
29*30877f79SAndroid Build Coastguard Worker[Ad insertion page](https://exoplayer.dev/ad-insertion.html#declarative-ad-support)
30*30877f79SAndroid Build Coastguard Workerof the developer guide. The `AdsLoaderProvider` passed to the player's
31*30877f79SAndroid Build Coastguard Worker`DefaultMediaSourceFactory` should return an `ImaAdsLoader`. Note that the IMA
32*30877f79SAndroid Build Coastguard Workermodule only supports players that are accessed on the application's main thread.
33*30877f79SAndroid Build Coastguard Worker
34*30877f79SAndroid Build Coastguard WorkerResuming the player after entering the background requires some special
35*30877f79SAndroid Build Coastguard Workerhandling when playing ads. The player and its media source are released on
36*30877f79SAndroid Build Coastguard Workerentering the background, and are recreated when returning to the foreground.
37*30877f79SAndroid Build Coastguard WorkerWhen playing ads it is necessary to persist ad playback state while in the
38*30877f79SAndroid Build Coastguard Workerbackground by keeping a reference to the `ImaAdsLoader`. When re-entering the
39*30877f79SAndroid Build Coastguard Workerforeground, pass the same instance back when
40*30877f79SAndroid Build Coastguard Worker`AdsLoaderProvider.getAdsLoader(MediaItem.AdsConfiguration adsConfiguration)`
41*30877f79SAndroid Build Coastguard Workeris called to restore the state. It is also important to persist the player
42*30877f79SAndroid Build Coastguard Workerposition when entering the background by storing the value of
43*30877f79SAndroid Build Coastguard Worker`player.getContentPosition()`.  On returning to the foreground, seek to that
44*30877f79SAndroid Build Coastguard Workerposition before preparing the new player instance. Finally, it is important to
45*30877f79SAndroid Build Coastguard Workercall `ImaAdsLoader.release()` when playback has finished and will not be
46*30877f79SAndroid Build Coastguard Workerresumed.
47*30877f79SAndroid Build Coastguard Worker
48*30877f79SAndroid Build Coastguard WorkerYou can try the IMA module in the ExoPlayer demo app, which has test content in
49*30877f79SAndroid Build Coastguard Workerthe "IMA sample ad tags" section of the sample chooser. The demo app's
50*30877f79SAndroid Build Coastguard Worker`PlayerActivity` also shows how to persist the `ImaAdsLoader` instance and the
51*30877f79SAndroid Build Coastguard Workerplayer position when backgrounded during ad playback.
52*30877f79SAndroid Build Coastguard Worker
53*30877f79SAndroid Build Coastguard Worker## Links
54*30877f79SAndroid Build Coastguard Worker
55*30877f79SAndroid Build Coastguard Worker* [ExoPlayer documentation on ad insertion][]
56*30877f79SAndroid Build Coastguard Worker* [Javadoc][]
57*30877f79SAndroid Build Coastguard Worker
58*30877f79SAndroid Build Coastguard Worker[ExoPlayer documentation on ad insertion]: https://exoplayer.dev/ad-insertion.html
59*30877f79SAndroid Build Coastguard Worker[Javadoc]: https://exoplayer.dev/doc/reference/index.html
60