2021 Audio & Video
WWDC21 · 15 min · Audio & Video
Explore dynamic pre-rolls and mid-rolls in HLS
Learn how you can create seamless transitions between advertisements and your HLS streams. We’ll show you how to incorporate HLS tags and AVFoundation APIs to create media experiences that move easily between your primary content and mid-rolls, and provide best practices for playing these streams in your app.
Watch at developer.apple.com ↗Code shown on screen · 4 snippets
AVPlayerInterstitialEvent
class AVPlayerInterstitialEvent {
var primaryItem: AVPlayerItem? { get }
var identifier: String { get }
var time: CMTime { get }
var date: Date? { get }
var templateItems: [AVPlayerItem] { get }
var restrictions: AVPlayerInterstitialEvent.Restrictions { get }
var resumptionOffset: CMTime { get }
var playoutLimit: CMTime { get }
var userDefinedAttributes: [AnyHashable : Any] { get }
} Observing server inserted events
// Client observes server-side interstitial playback
let player = AVPlayer(url: movieURL) // movieURL has EXT-X-DATERANGE ad tags
let observer = AVPlayerInterstitialEventMonitor(primaryPlayer: player)
NotificationCenter.default.addObserver(
forName: AVPlayerInterstitialEventMonitor.currentEventDidChangeNotification,
object: observer,
queue: OperationQueue.main) {
notification_ in
self.updateUI(observer.currentEvent, observer.interstitialPlayer)
} AVPlayerInterstitialEventController
class AVPlayerInterstitialEventController : AVPlayerInterstitialEventMonitor {
var events: [AVPlayerInterstitialEvent]!
func cancelCurrentEvent(withResumptionOffset resumptionOffset: CMTime)
} Client schedules ad pod
// Client inserted events
// Client schedules an ad pod at 10s into primary asset
let player = AVPlayer(url: movieURL) // no ads in primary asset
let controller = AVPlayerInterstitialEventController(primaryPlayer: player)
let adPodTemplates = [AVPlayerItem(url: ad1URL), AVPlayerItem(url: ad2URL)]
let event = AVPlayerInterstitialEvent(
primaryItem: player.currentItem,
time: CMTime(seconds: 10, preferredTimescale: 1),
templateItems: adPodTemplates,
restrictions: [],
resumptionOffset: .zero,
playoutLimit: .invalid)
controller.events = [event]
player.currentItem.translatesPlayerInterstitialEvents = true
let vc = AVPlayerViewController()
vc.player = player
player.play() Resources
Related sessions
-
9 min -
17 min -
38 min