Playing ads with ExoPlayer and IMA

The ExoPlayer 2.5 release includes a new extension for playing ads, wrapping the Interactive Media Ads SDK (IMA). This post describes how to get started with the extension and explains some of its benefits compared to a using IMA directly.


Integrating the IMA extension with your existing player is easy. The first step is to add a dependency as described in the README. Then create an ImaAdsLoader, passing in an ad tag URI identifying the ads to play. We support ad tags from DoubleClick for Publishers, Google AdSense or any other VAST-compliant ad server, and the IMA site provides some sample ad tags for testing.

The next step is to construct an ImaAdsMediaSource, which wraps your content MediaSource and takes a reference to the ImaAdsLoader and a ViewGroup on top of the player in which IMA will show the ad overlay UI. This is similar to MediaSource composition, which you may have used before: the content MediaSource is a child of the ImaAdsMediaSource.

The last step is to handle pausing and resuming ad playback if the app enters the background. When the app enters the background, store the content position (obtained from player.getContentPosition()). Then release the player, but retain the ImaAdsLoader as it keeps track of the ad playback state. When the app enters the foreground again, seek to the stored content position and prepare the player with a new ImaAdsMediaSource, created using the same ImaAdsloader. Ad playback will resume where it left off. When the Activity is finally destroyed, release the loader to clean up resources used by IMA.

Benefits of the ExoPlayer IMA extension

In a conventional integration with IMA it’s generally necessary to swap out the player’s source whenever playback transitions between ads and content, which can be inefficient and leads to rebuffering.

In contrast, with the IMA extension ExoPlayer knows about the structure of the ad timeline and seamlessly joins ads and content, so users normally won’t see rebuffering when transitioning between the two. The player also knows about the positions of midroll ads, which means that it can avoid buffering then throwing away extra media past an ad break cue point, saving battery and data.

Ad playback in the demo app with skippable ad UI rendered by IMA

ExoPlayer’s UI components are now also ad-aware: SimpleExoPlayerView’s DefaultTimeBar displays positions of ad breaks and playback controls are automatically hidden when ads start playing.

DefaultTimeBar with ad markers, from the ExoPlayer UI library

We hope that the IMA extension makes it easier to monetize your content while providing a great user experience. Please file any issues on our issue tracker and feel free to provide other feedback below.