Present additional content such as legal text, content warnings, or advertisements alongside your main media content.
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 support for serving stitched playlists separated with
EXT-X-DISCONTINUITY tags (see Example Playlist Files for use with HTTP Live Streaming). 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 interstitial content is presented.
Define the 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 time ranges are traversed 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 is set with 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 interstitial time ranges are defined,
AVPlayer updates its user interface in two important ways (see Figure 1). First, any interstitial times ranges are represented as small dots on the player’s timeline. This makes it easy for users to understand where they are between interstitial breaks and helps orient them to where they are in your app. Second, the interstitial time ranges are collapsed from the player’s interface. The current time and duration presented represent only your main content, providing a better sense of the timing of the main app.
Enforce Linear Playback
By adopting the
AVPlayer protocol, you can be notified as interstitial time ranges are traversed, 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
false, but when presenting an advertisement, you can set it to
true to prevent user navigation, as shown in the following example:
Prevent Skipping of Interstitial Content
If your app presents interstitial content, you may also want to prevent users from skipping past ads or legal text. Implement this functionality by using the
player delegate method. This method is called 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 simple code example shows 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 an interstitial time range is found, the code returns its start time, forcing playback to begin at the start of the advertisement.