Dunfey · Hotel WWDC as data, est. 1983
Front desk everything
Years
Topics

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 ↗

Transcript all transcripts

Code shown on screen · 4 snippets

AVPlayerInterstitialEvent swift · at 9:50 ↗
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 swift · at 10:58 ↗
// 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 swift · at 11:40 ↗
class AVPlayerInterstitialEventController : AVPlayerInterstitialEventMonitor {
    var events: [AVPlayerInterstitialEvent]!
    func cancelCurrentEvent(withResumptionOffset resumptionOffset: CMTime) 
}
Client schedules ad pod swift · at 12:01 ↗
// 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