Skip to content
This repository has been archived by the owner on Jan 16, 2023. It is now read-only.

Picture in Picture #414

Open
wants to merge 3 commits into
base: dev
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions Sources/Clappr/Classes/Plugin/Playback/AVFoundationPlayback.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import AVFoundation
import AVKit

open class AVFoundationPlayback: Playback, AVPlayerItemInfoDelegate {
open class override var name: String { "AVPlayback" }
Expand Down Expand Up @@ -204,6 +205,8 @@ open class AVFoundationPlayback: Playback, AVPlayerItemInfoDelegate {
return playbackType == .live ? isDvrAvailable : !duration.isZero
}

private var pictureInPictureController: AVPictureInPictureController!

@available(*, unavailable)
public required init?(coder _: NSCoder) {
fatalError("init(coder:) has not been implemented")
Expand All @@ -225,6 +228,7 @@ open class AVFoundationPlayback: Playback, AVPlayerItemInfoDelegate {
player.appliesMediaSelectionCriteriaAutomatically = false

playerLayer = AVPlayerLayer(player: player)
setupPictureInPicture(layer: playerLayer)

setAudioSessionCategory(to: .playback)
}
Expand Down Expand Up @@ -795,3 +799,25 @@ open class AVFoundationPlayback: Playback, AVPlayerItemInfoDelegate {
}
}
}

extension AVFoundationPlayback: AVPictureInPictureControllerDelegate {
func setupPictureInPicture(layer: AVPlayerLayer) {
if AVPictureInPictureController.isPictureInPictureSupported() {
pictureInPictureController = AVPictureInPictureController(playerLayer: layer)
pictureInPictureController?.delegate = self
}
}

public func pictureInPictureController(_ pictureInPictureController: AVPictureInPictureController,
restoreUserInterfaceForPictureInPictureStopWithCompletionHandler completionHandler: @escaping (Bool) -> Void) {
completionHandler(true)
}

public func pictureInPictureControllerWillStartPictureInPicture(_ pictureInPictureController: AVPictureInPictureController) {
trigger(Event.disableMediaControl)
}

public func pictureInPictureControllerDidStopPictureInPicture(_ pictureInPictureController: AVPictureInPictureController) {
trigger(Event.enableMediaControl)
}
}