Present additional content such as legal text, content warnings, or advertisements alongside your main media presentation.
Media playback apps often present additional content such as legal text, content warnings, or advertisements alongside their main media content. The best solution to present this kind of content is to use HTTP Live Streaming’s support for serving stitched playlists as discussed in Incorporating Ads into a Playlist. Stitched playlists let you combine multiple media playlists into a single, unified playlist that’s delivered to the client as a single stream. This stream provides a smooth playback experience to users, with no breaks or stutters in the action when the player presents the interstitial content.
Define Interstitial Time Ranges
AVKit in tvOS simplifies working with interstitial content delivered as part of a stitched playlist. You define the time ranges in your presentation that contain interstitial content. As the player encounters the time ranges during playback, you receive callbacks when they begin and end, giving you the opportunity to enforce business rules or capture analytics.
AVPlayer in tvOS adds an
interstitial property that you set to an array of
AVInterstitial objects. Each object defines a
CMTime marking the interstitial time range in your media’s timeline. The following code example shows how to create interstitial time ranges:
When you define interstitial time ranges,
AVPlayer updates its user interface in two important ways (see Figure 1). First, the player represents any interstitial times ranges as small dots on the player’s timeline. This helps users understand where they are between interstitial breaks and helps orient them to where they are in the overall media timeline. Second, the player collapses interstitial time ranges from its time display. The current time and duration presented represent only your main content, providing a better sense of the primary media’s timeline.
Enforce Linear Playback
When you adopt the
AVPlayer protocol, the player can notify your app as it traverses interstitial time ranges, which is useful to help you enforce business rules. For instance, a common requirement when presenting advertisements is to prevent users from skipping past them. You can use the
requires property of
AVPlayer to control whether users can navigate through the content using the Siri Remote. During playback, this property is normally set to
NO, but when presenting an advertisement, you can set it to
YES to prevent user navigation, as shown in the following example:
Prevent Skipping of Interstitial Content
If your app presents interstitial content, such as ads or legal text, you may want to prevent users from skipping past it. Implement this functionality by using the
player delegate method. The system calls this method whenever a user performs a seek operation using the Siri Remote, which happens either by swiping left or right on the remote touch pad or by navigating chapter markers in the Info panel. The following code shows a simple example of how you might implement this method to prevent users from skipping past advertisements:
For any forward seeks, the example code ensures that the user can’t skip past an ad break. It attempts to find an interstitial time range within the time range of the user’s seek request. If it finds an interstitial time range, the code returns its start time, forcing playback to begin at the start of the advertisement.