The HLS Interstitials specification introduces a flexible way to schedule and
insert advertisements into a video or audio stream. With the client-side
approach, your application takes full control over when to request and play the
ad breaks by creating the
AVPlayerInterstitialEvent
class. This approach doesn't require the EXT-X-DATERANGE tags in the content
stream manifests. Client-side HLS interstitials lets you dynamically insert ads
into any content, without needing to modify the stream manifest or media files.
This guide covers integrating the Interactive Media Ads (IMA) SDK into a video player app that creates a Server guided ad insertion (SGAI) livestream session and schedules interstitials client-side. For more information, see Server guided DAI.
Prerequisites
Before you begin, you need the following:
A new Xcode project, using
Storyboardfor the user interface. For more information, see Creating an Xcode project for an app.Google IMA SDK. For more information, see Set up the IMA SDK for DAI.
The following parameters for your DAI livestream request:
NETWORK_CODE: Your Google Ad Manager network code.CUSTOM_ASSET_KEY: Your custom string identifying the DAI livestream event. The livestream event must have the Pod serving manifest DAI type.
Configure a storyboard
In your iPhone.storyboard file, do the following:
- Create a
UIViewobject as a container for the video player and the ad UI. - Create an
adUIViewproperty of theViewControllerclass to connect with theUIViewobject. - In the
adUIViewobject, create aUIButtonto function as a play button. - Create a
playButtonproperty of theViewControllerclass to connect with theUIButtonobject and anonPlayButtonTouchfunction to handle user taps.
Initialize an ads loader
In the main view controller's viewDidLoad event, do the following:
- Set up a video player using
AVPlayerandAVPlayerLayerclasses. - Create
IMAAdDisplayContainerandIMAAVPlayerVideoDisplayobjects. The ad display container specifies theadUIViewfor IMA DAI SDK to insert the ad UI subviews. The video display object acts as a bridge between the IMA DAI SDK's ad logic and the AVFoundation playback system, tracking the video ads playback. - Initialize the
IMAAdsLoaderobject with ad playback and ad UI localization settings.
The following example initializes an ads loader with an empty
IMASettings
object:
Make a stream request
To request ads for a content stream, create an
IMAPodStreamRequest
object and pass it to your
IMAAdsLoader
instance. Optionally, set the adTagParameters property to supply DAI options
and targeting parameters for your stream.
This example calls the loadAdStream method in the viewDidAppear event:
In your production app, call the loadAdStream method after the user selects a
content stream.
Handle stream load events
Implement the
IMAAdsLoaderDelegate
protocol to handle the success or failure of the stream request:
- On success, you receive an
IMAAdsLoadedDataobject containing theIMAStreamManager. Store thestreamManager.streamIdvalue for the current DAI session. - On failure, log the error.
The following example handles the stream loaded event and logs the stream failed to load event:
Schedule ad insertions
To schedule an ad break, create an
AVPlayerInterstitialEvent
object. Set the templateItems property of the event object to an array of
AVPlayerItem
objects, where each item object holds an ad pod manifest URL.
To construct an ad pod manifest URL, follow Method: HLS pod manifest documentation.
For demonstration purposes, the following example generates a pod identifier
string using the content livestream's current time. The generatePodIdentifier
function returns the pod identifier as ad_break_id/mid-roll-{minute}.
In your production app, retrieve the pod identifier from a source that provides unique values for each ad break, synchronized for all viewers of the livestream.
The following example schedules an ad break to start within the next two minutes after the user clicks the play button:
The scheduleAdInsertion method
calculates the ad break start time and constructs an ad pod manifest URL. Use
this URL to create an AVPlayerInterstitialEvent object.
Optionally, use AVPlayerInterstitialEvent.Restrictions struct to restrict user
skipping or rewinding during ad playback.
Handle ad events
To handle ad events, implement the
IMAStreamManagerDelegate
protocol. This approach lets you track when ad breaks start and end, and to get
information about individual ads.
Run your app. If successful, you can request and play interstitials using an Pod serving manifest stream.