media3/libraries/exoplayer_ima
bachinger d1534680e9 Support splitting the ad playback state for live streams
This change does basically three things in preparation of a follow
up CL that will insert ad breaks into multi-period live windows.

- Make sure that only `AdPlaybackState`s of multi-period streams
  are passed to `ImaUtil.splitAdPlaybackStateForPeriods()` from
  the media source.
- Enhance the splitting algorithm in ImaUtil to support live windows.
  Roughly, this includes calculating the window start position in the
  UNIX epoch context, handling the end period with an unknown duration
  and taking the `adResumePosition` of an ad group into account that
  affects the ad group start positions (`adGroup.timeUs`).
- Append a placeholder postroll ad group to the ad playback state of
  each period in a live stream to allow immediate transition to an
  ad that's inserted by an ad event from the SDK.

PiperOrigin-RevId: 516167958
2023-03-14 07:52:58 +00:00
..
2023-01-04 21:29:59 +00:00
2021-10-27 09:12:46 +01:00
2022-12-12 11:19:23 +00:00

ExoPlayer IMA module

The ExoPlayer IMA module provides an AdsLoader implementation wrapping the Interactive Media Ads SDK for Android. You can use it to insert ads into content played using ExoPlayer.

Getting the module

The easiest way to get the module is to add it as a gradle dependency:

implementation 'androidx.media3:media3-exoplayer-ima:1.X.X'

where 1.X.X is the version, which must match the version of the other media modules being used.

Alternatively, you can clone this GitHub project and depend on the module locally. Instructions for doing this can be found in the top level README.

Using the module

To use the module, 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 module only supports players that 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 module 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.