99import Cocoa
1010import AVFoundation
1111import os. log
12+ import AVKit
1213
1314public final class PUIPlayerView : NSView {
1415
@@ -340,7 +341,7 @@ public final class PUIPlayerView: NSView {
340341 }
341342
342343 fileprivate func updatePlayingState( ) {
343- pipController? . setPlaying ( isPlaying)
344+ // pipController?.setPlaying(isPlaying)
344345
345346 if isPlaying {
346347 playButton. image = . PUIPause
@@ -645,7 +646,8 @@ public final class PUIPlayerView: NSView {
645646 let b = PUIButton ( frame: . zero)
646647
647648 b. isToggle = true
648- b. image = . PUIPictureInPicture
649+ b. image = AVPictureInPictureController . pictureInPictureButtonStartImage
650+ b. alternateImage = AVPictureInPictureController . pictureInPictureButtonStopImage
649651 b. target = self
650652 b. action = #selector( togglePip)
651653 b. toolTip = " Toggle picture in picture "
@@ -1018,7 +1020,7 @@ public final class PUIPlayerView: NSView {
10181020
10191021 @IBAction public func togglePip( _ sender: NSView ? ) {
10201022 if isInPictureInPictureMode {
1021- exitPictureInPictureMode ( )
1023+ pipController ? . stopPictureInPicture ( )
10221024 } else {
10231025 enterPictureInPictureMode ( )
10241026 }
@@ -1266,7 +1268,7 @@ public final class PUIPlayerView: NSView {
12661268 }
12671269 }
12681270
1269- fileprivate var pipController : PIPViewController ?
1271+ fileprivate var pipController : AVPictureInPictureController ?
12701272
12711273 fileprivate func enterPictureInPictureMode( ) {
12721274 delegate? . playerViewWillEnterPictureInPictureMode ( self )
@@ -1275,21 +1277,14 @@ public final class PUIPlayerView: NSView {
12751277 self ? . externalStatusController. snapshot = image
12761278 }
12771279
1278- pipController = PIPViewController ( )
1280+ pipController = AVPictureInPictureController ( playerLayer : playerLayer )
12791281 pipController? . delegate = self
1280- pipController? . setPlaying ( isPlaying)
1281- pipController? . aspectRatio = currentPresentationSize ?? NSSize ( width: 640 , height: 360 )
1282- pipController? . view. layer? . backgroundColor = NSColor . black. cgColor
1282+ // pipController?.setPlaying(isPlaying)
1283+ // pipController?.aspectRatio = currentPresentationSize ?? NSSize(width: 640, height: 360)
1284+ // pipController?.view.layer?.backgroundColor = NSColor.black.cgColor
12831285
1284- pipController? . presentAsPicture ( inPicture: pictureContainer)
1285-
1286- isInPictureInPictureMode = true
1287- }
1288-
1289- fileprivate func exitPictureInPictureMode( ) {
1290- if pictureContainer. presentingViewController == pipController {
1291- pipController? . dismiss ( pictureContainer)
1292- }
1286+ // pipController?.presentAsPicture(inPicture: pictureContainer)
1287+ pipController? . startPictureInPicture ( )
12931288 }
12941289
12951290 // MARK: - Visibility management
@@ -1643,51 +1638,83 @@ extension PUIPlayerView: PUIExternalPlaybackConsumer {
16431638
16441639// MARK: - PiP delegate
16451640
1646- extension PUIPlayerView : PIPViewControllerDelegate , PUIPictureContainerViewControllerDelegate {
1641+ extension PUIPlayerView : PUIPictureContainerViewControllerDelegate , AVPictureInPictureControllerDelegate {
1642+
1643+ // public func pipActionStop(_ pip: PIPViewController) {
1644+ // pause(pip)
1645+ //
1646+ // }
1647+ //
1648+ // public func pipActionReturn(_ pip: PIPViewController) {
1649+ // delegate?.playerViewWillExitPictureInPictureModelegate?.playerViewWillExitPictureInPictureMode(self, reason: .exitButton)de(self, reason: .returnButton)
1650+ //
1651+ // if !NSApp.isActive {
1652+ // NSApp.activate(ignoringOtherApps: true)
1653+ // }
1654+ //
1655+ // if let window = lastKnownWindow {
1656+ // window.makeKeyAndOrderFront(pip)
1657+ //
1658+ // if window.isMiniaturized {
1659+ // window.deminiaturize(nil)
1660+ // }
1661+ // }
1662+ // }
1663+
1664+ // public func pipActionPause(_ pip: PIPViewController) {
1665+ // pause(pip)
1666+ // }
1667+ //
1668+ // public func pipActionPlay(_ pip: PIPViewController) {
1669+ // play(pip)
1670+ // }
1671+
1672+ public func pictureInPictureController( _ pictureInPictureController: AVPictureInPictureController , failedToStartPictureInPictureWithError error: Error ) {
1673+ isInPictureInPictureMode = false
1674+ pipController = nil
1675+ }
16471676
1648- public func pipActionStop( _ pip: PIPViewController ) {
1649- pause ( pip)
1650- delegate? . playerViewWillExitPictureInPictureMode ( self , reason: . exitButton)
1677+ public func pictureInPictureControllerDidStopPictureInPicture( _ pictureInPictureController: AVPictureInPictureController ) {
1678+ print ( " Player Rate: \( player? . rate) " )
1679+ isInPictureInPictureMode = false
1680+ pipController = nil
16511681 }
16521682
1653- public func pipActionReturn( _ pip: PIPViewController ) {
1683+ public func pictureInPictureControllerDidStartPictureInPicture( _ pictureInPictureController: AVPictureInPictureController ) {
1684+ isInPictureInPictureMode = true
1685+ }
1686+
1687+ // public func pipDidClose(_ pip: PIPViewController) {
1688+ // pictureContainer.view.frame = bounds
1689+ //
1690+ // addSubview(pictureContainer.view, positioned: .below, relativeTo: scrimContainerView)
1691+ //
1692+ // isInPictureInPictureMode = false
1693+ // pipController = nil
1694+ // }
1695+
1696+ public func pictureInPictureControllerWillStopPictureInPicture( _ pictureInPictureController: AVPictureInPictureController ) {
16541697 delegate? . playerViewWillExitPictureInPictureMode ( self , reason: . returnButton)
1698+ print ( " Player Rate: \( player? . rate) " )
16551699
16561700 if !NSApp. isActive {
16571701 NSApp . activate ( ignoringOtherApps: true )
16581702 }
16591703
16601704 if let window = lastKnownWindow {
1661- window. makeKeyAndOrderFront ( pip )
1705+ window. makeKeyAndOrderFront ( pictureInPictureController )
16621706
16631707 if window. isMiniaturized {
16641708 window. deminiaturize ( nil )
16651709 }
16661710 }
16671711 }
16681712
1669- public func pipActionPause( _ pip: PIPViewController ) {
1670- pause ( pip)
1671- }
1672-
1673- public func pipActionPlay( _ pip: PIPViewController ) {
1674- play ( pip)
1675- }
1676-
1677- public func pipDidClose( _ pip: PIPViewController ) {
1678- pictureContainer. view. frame = bounds
1679-
1680- addSubview ( pictureContainer. view, positioned: . below, relativeTo: scrimContainerView)
1681-
1682- isInPictureInPictureMode = false
1683- pipController = nil
1684- }
1685-
1686- public func pipWillClose( _ pip: PIPViewController ) {
1687- pip. replacementRect = frame
1688- pip. replacementView = self
1689- pip. replacementWindow = lastKnownWindow
1690- }
1713+ // public func pipWillClose(_ pip: PIPViewController) {
1714+ // pip.replacementRect = frame
1715+ // pip.replacementView = self
1716+ // pip.replacementWindow = lastKnownWindow
1717+ // }
16911718
16921719 func pictureContainerViewSuperviewDidChange( to superview: NSView ? ) {
16931720 guard let superview = superview else { return }
@@ -1696,7 +1723,7 @@ extension PUIPlayerView: PIPViewControllerDelegate, PUIPictureContainerViewContr
16961723
16971724 if superview == self , pipController != nil {
16981725 if pictureContainer. presentingViewController == pipController {
1699- pipController? . dismiss ( pictureContainer )
1726+ pipController? . stopPictureInPicture ( )
17001727 }
17011728
17021729 pipController = nil
0 commit comments