Name Date Size #Lines LOC

..--

src/H25-Apr-2025-5,3184,141

README.mdH A D25-Apr-20252.6 KiB6044

build.gradleH A D25-Apr-20252 KiB5246

proguard-rules.txtH A D25-Apr-2025284 86

README.md

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