Skip to content

Commit

Permalink
Go a bit further in changing over
Browse files Browse the repository at this point in the history
  • Loading branch information
allenhumphreys committed Jun 8, 2023
1 parent f2dc89d commit e334a5a
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 179 deletions.
69 changes: 0 additions & 69 deletions PlayerUI/Controllers/PUIPictureContainerViewController.swift

This file was deleted.

152 changes: 46 additions & 106 deletions PlayerUI/Views/PUIPlayerView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,20 @@ public final class PUIPlayerView: NSView {
public var mediaTitle: String?
public var mediaIsLiveStream: Bool = false

var pictureContainer: PUIPictureContainerViewController!

public init(player: AVPlayer) {
self.player = player
self.pipController = AVPictureInPictureController(contentSource: .init(playerLayer: playerLayer))

super.init(frame: .zero)

pipPossibleObservation = pipController.observe(
\AVPictureInPictureController.isPictureInPicturePossible, options: [.initial, .new]
) { [weak self] _, change in
// Update the PiP button's enabled state.
self?.pipButton.isEnabled = change.newValue ?? false
}

pipController.delegate = self
wantsLayer = true
layer = PUIBoringLayer()
layer?.backgroundColor = NSColor.black.cgColor
Expand Down Expand Up @@ -217,7 +224,7 @@ public final class PUIPlayerView: NSView {
return player?.currentItem?.asset
}

private var playerLayer = PUIBoringPlayerLayer()
private let playerLayer = PUIBoringPlayerLayer()

private func setupPlayer() {
elapsedTimeLabel.stringValue = elapsedTimeInitialValue
Expand All @@ -229,15 +236,6 @@ public final class PUIPlayerView: NSView {
playerLayer.player = player
playerLayer.videoGravity = .resizeAspect

if pictureContainer == nil {
pictureContainer = PUIPictureContainerViewController(playerLayer: playerLayer)
pictureContainer.delegate = self
pictureContainer.view.frame = bounds
pictureContainer.view.autoresizingMask = [.width, .height]

addSubview(pictureContainer.view)
}

player.addObserver(self, forKeyPath: #keyPath(AVPlayer.status), options: [.initial, .new], context: nil)
player.addObserver(self, forKeyPath: #keyPath(AVPlayer.volume), options: [.initial, .new], context: nil)
player.addObserver(self, forKeyPath: #keyPath(AVPlayer.rate), options: [.initial, .new], context: nil)
Expand Down Expand Up @@ -341,8 +339,6 @@ public final class PUIPlayerView: NSView {
}

fileprivate func updatePlayingState() {
// pipController?.setPlaying(isPlaying)

if isPlaying {
playButton.image = .PUIPause
} else {
Expand Down Expand Up @@ -651,6 +647,7 @@ public final class PUIPlayerView: NSView {
b.target = self
b.action = #selector(togglePip)
b.toolTip = "Toggle picture in picture"
b.isEnabled = false

return b
}()
Expand All @@ -662,6 +659,17 @@ public final class PUIPlayerView: NSView {
private func setupControls() {
externalStatusController.view.isHidden = true
externalStatusController.view.translatesAutoresizingMaskIntoConstraints = false
let playerView = NSView()
playerView.translatesAutoresizingMaskIntoConstraints = false
playerView.wantsLayer = true
playerView.layer = playerLayer
playerLayer.backgroundColor = .clear
addSubview(playerView)
playerView.leadingAnchor.constraint(equalTo: leadingAnchor).isActive = true
playerView.trailingAnchor.constraint(equalTo: trailingAnchor).isActive = true
playerView.topAnchor.constraint(equalTo: topAnchor).isActive = true
playerView.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true

addSubview(externalStatusController.view)
externalStatusController.view.leadingAnchor.constraint(equalTo: leadingAnchor).isActive = true
externalStatusController.view.trailingAnchor.constraint(equalTo: trailingAnchor).isActive = true
Expand Down Expand Up @@ -1020,9 +1028,9 @@ public final class PUIPlayerView: NSView {

@IBAction public func togglePip(_ sender: NSView?) {
if isInPictureInPictureMode {
pipController?.stopPictureInPicture()
pipController.stopPictureInPicture()
} else {
enterPictureInPictureMode()
pipController.startPictureInPicture()
}
}

Expand Down Expand Up @@ -1246,24 +1254,8 @@ public final class PUIPlayerView: NSView {
}
}

fileprivate var pipController: AVPictureInPictureController?

fileprivate func enterPictureInPictureMode() {
delegate?.playerViewWillEnterPictureInPictureMode(self)

snapshotPlayer { [weak self] image in
self?.externalStatusController.snapshot = image
}

pipController = AVPictureInPictureController(playerLayer: playerLayer)
pipController?.delegate = self
// pipController?.setPlaying(isPlaying)
// pipController?.aspectRatio = currentPresentationSize ?? NSSize(width: 640, height: 360)
// pipController?.view.layer?.backgroundColor = NSColor.black.cgColor

// pipController?.presentAsPicture(inPicture: pictureContainer)
pipController?.startPictureInPicture()
}
fileprivate let pipController: AVPictureInPictureController
private var pipPossibleObservation: Any?

// MARK: - Visibility management

Expand Down Expand Up @@ -1616,65 +1608,9 @@ extension PUIPlayerView: PUIExternalPlaybackConsumer {

// MARK: - PiP delegate

extension PUIPlayerView: PUIPictureContainerViewControllerDelegate, AVPictureInPictureControllerDelegate {

// public func pipActionStop(_ pip: PIPViewController) {
// pause(pip)
//
// }
//
// public func pipActionReturn(_ pip: PIPViewController) {
// delegate?.playerViewWillExitPictureInPictureModelegate?.playerViewWillExitPictureInPictureMode(self, reason: .exitButton)de(self, reason: .returnButton)
//
// if !NSApp.isActive {
// NSApp.activate(ignoringOtherApps: true)
// }
//
// if let window = lastKnownWindow {
// window.makeKeyAndOrderFront(pip)
//
// if window.isMiniaturized {
// window.deminiaturize(nil)
// }
// }
// }

// public func pipActionPause(_ pip: PIPViewController) {
// pause(pip)
// }
//
// public func pipActionPlay(_ pip: PIPViewController) {
// play(pip)
// }

public func pictureInPictureController(_ pictureInPictureController: AVPictureInPictureController, failedToStartPictureInPictureWithError error: Error) {
isInPictureInPictureMode = false
pipController = nil
}

public func pictureInPictureControllerDidStopPictureInPicture(_ pictureInPictureController: AVPictureInPictureController) {
print("Player Rate: \(player?.rate)")
isInPictureInPictureMode = false
pipController = nil
}

public func pictureInPictureControllerDidStartPictureInPicture(_ pictureInPictureController: AVPictureInPictureController) {
isInPictureInPictureMode = true
}

// public func pipDidClose(_ pip: PIPViewController) {
// pictureContainer.view.frame = bounds
//
// addSubview(pictureContainer.view, positioned: .below, relativeTo: scrimContainerView)
//
// isInPictureInPictureMode = false
// pipController = nil
// }

public func pictureInPictureControllerWillStopPictureInPicture(_ pictureInPictureController: AVPictureInPictureController) {
delegate?.playerViewWillExitPictureInPictureMode(self, reason: .returnButton)
print("Player Rate: \(player?.rate)")
extension PUIPlayerView: /*PUIPictureContainerViewControllerDelegate,*/ AVPictureInPictureControllerDelegate {

public func pictureInPictureController(_ pictureInPictureController: AVPictureInPictureController, restoreUserInterfaceForPictureInPictureStopWithCompletionHandler completionHandler: @escaping (Bool) -> Void) {
if !NSApp.isActive {
NSApp.activate(ignoringOtherApps: true)
}
Expand All @@ -1686,26 +1622,30 @@ extension PUIPlayerView: PUIPictureContainerViewControllerDelegate, AVPictureInP
window.deminiaturize(nil)
}
}
completionHandler(true)
}

// public func pipWillClose(_ pip: PIPViewController) {
// pip.replacementRect = frame
// pip.replacementView = self
// pip.replacementWindow = lastKnownWindow
// }

func pictureContainerViewSuperviewDidChange(to superview: NSView?) {
guard let superview = superview else { return }
public func pictureInPictureController(_ pictureInPictureController: AVPictureInPictureController, failedToStartPictureInPictureWithError error: Error) {
isInPictureInPictureMode = false
}

pictureContainer.view.frame = superview.bounds
public func pictureInPictureControllerDidStopPictureInPicture(_ pictureInPictureController: AVPictureInPictureController) {
isInPictureInPictureMode = false
}

if superview == self, pipController != nil {
if pictureContainer.presentingViewController == pipController {
pipController?.stopPictureInPicture()
}
public func pictureInPictureControllerWillStartPictureInPicture(_ pictureInPictureController: AVPictureInPictureController) {
delegate?.playerViewWillEnterPictureInPictureMode(self)

pipController = nil
snapshotPlayer { [weak self] image in
self?.externalStatusController.snapshot = image
}
}

public func pictureInPictureControllerDidStartPictureInPicture(_ pictureInPictureController: AVPictureInPictureController) {
isInPictureInPictureMode = true
}

public func pictureInPictureControllerWillStopPictureInPicture(_ pictureInPictureController: AVPictureInPictureController) {
delegate?.playerViewWillExitPictureInPictureMode(self, reason: .returnButton)
}
}
4 changes: 0 additions & 4 deletions WWDC.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,6 @@
DDF7219D1ECA12780054C503 /* PlayerUI.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = DDF721961ECA12780054C503 /* PlayerUI.framework */; };
DDF7219E1ECA12780054C503 /* PlayerUI.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = DDF721961ECA12780054C503 /* PlayerUI.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
DDF721C71ECA12A40054C503 /* PUIExternalPlaybackStatusViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = DDF721A51ECA12A40054C503 /* PUIExternalPlaybackStatusViewController.swift */; };
DDF721C81ECA12A40054C503 /* PUIPictureContainerViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = DDF721A61ECA12A40054C503 /* PUIPictureContainerViewController.swift */; };
DDF721C91ECA12A40054C503 /* Colors.swift in Sources */ = {isa = PBXBuildFile; fileRef = DDF721A81ECA12A40054C503 /* Colors.swift */; };
DDF721CA1ECA12A40054C503 /* Images.swift in Sources */ = {isa = PBXBuildFile; fileRef = DDF721A91ECA12A40054C503 /* Images.swift */; };
DDF721CB1ECA12A40054C503 /* Speeds.swift in Sources */ = {isa = PBXBuildFile; fileRef = DDF721AA1ECA12A40054C503 /* Speeds.swift */; };
Expand Down Expand Up @@ -414,7 +413,6 @@
DDF721981ECA12780054C503 /* PlayerUI.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = PlayerUI.h; sourceTree = "<group>"; };
DDF721991ECA12780054C503 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
DDF721A51ECA12A40054C503 /* PUIExternalPlaybackStatusViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PUIExternalPlaybackStatusViewController.swift; sourceTree = "<group>"; };
DDF721A61ECA12A40054C503 /* PUIPictureContainerViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PUIPictureContainerViewController.swift; sourceTree = "<group>"; };
DDF721A81ECA12A40054C503 /* Colors.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Colors.swift; sourceTree = "<group>"; };
DDF721A91ECA12A40054C503 /* Images.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Images.swift; sourceTree = "<group>"; };
DDF721AA1ECA12A40054C503 /* Speeds.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Speeds.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -986,7 +984,6 @@
isa = PBXGroup;
children = (
DDF721A51ECA12A40054C503 /* PUIExternalPlaybackStatusViewController.swift */,
DDF721A61ECA12A40054C503 /* PUIPictureContainerViewController.swift */,
DDC6781E1EDB8EDA00A4E19C /* PUIAnnotationWindowController.swift */,
);
path = Controllers;
Expand Down Expand Up @@ -1545,7 +1542,6 @@
buildActionMask = 2147483647;
files = (
DDF721D11ECA12A40054C503 /* PUITimelineAnnotation.swift in Sources */,
DDF721C81ECA12A40054C503 /* PUIPictureContainerViewController.swift in Sources */,
DDF721D51ECA12A40054C503 /* AVPlayer+Validation.swift in Sources */,
DDF721CA1ECA12A40054C503 /* Images.swift in Sources */,
4DA83FE222AC3F2F0062DB8B /* PUIVibrantBackgroundButton.swift in Sources */,
Expand Down

0 comments on commit e334a5a

Please sign in to comment.