andrewlewis e009322edd Fix seeking to a non-zero position in a preloaded ad item
`ImaAdsLoader` will preload the first ad of a subsequent media item, but the
preloaded ad might not actually play because the user could seek to a non-zero
position in that media item (which could trigger playback of a midroll, not the
preroll). In this case, playback would get stuck because the midroll ad
expected to play after the seek would never load, because the IMA SDK expected
the preroll to play first.

Fix this behavior by discarding the preloaded ad break. If there isn't a seek,
the transition to the next media item is still seamless.

#minor-release

PiperOrigin-RevId: 357682510
2021-02-19 10:57:00 +00:00
..

ExoPlayer IMA extension

The IMA extension is an AdsLoader implementation wrapping the Interactive Media Ads SDK for Android. You can use it to insert ads alongside content.

Getting the extension

The easiest way to use the extension is to add it as a gradle dependency:

implementation 'com.google.android.exoplayer:extension-ima:2.X.X'

where 2.X.X is the version, which must match the version of the ExoPlayer library being used.

Alternatively, you can clone the ExoPlayer repository and depend on the module locally. Instructions for doing this can be found in ExoPlayer's top level README.

Using the extension

To use the extension, follow the instructions on the Ad insertion page of the developer guide. The AdsLoaderProvider passed to the player's DefaultMediaSourceFactory should return an ImaAdsLoader. Note that the IMA extension only supports players which are accessed on the application's main thread.

Resuming the player after entering the background requires some special handling when playing ads. The player and its media source are released on entering the background, and are recreated when returning to the foreground. When playing ads it is necessary to persist ad playback state while in the background by keeping a reference to the ImaAdsLoader. When re-entering the foreground, pass the same instance back when AdsLoaderProvider.getAdsLoader(MediaItem.AdsConfiguration adsConfiguration) is called to restore the state. It is also important to persist the player position when entering the background by storing the value of player.getContentPosition(). On returning to the foreground, seek to that position before preparing the new player instance. Finally, it is important to call ImaAdsLoader.release() when playback has finished and will not be resumed.

You can try the IMA extension in the ExoPlayer demo app, which has test content in the "IMA sample ad tags" section of the sample chooser. The demo app's PlayerActivity also shows how to persist the ImaAdsLoader instance and the player position when backgrounded during ad playback.