mirror of
https://github.com/yattee/yattee.git
synced 2025-08-06 10:44:06 +00:00
PiP improvements
This commit is contained in:
@@ -35,10 +35,7 @@ final class AVPlayerBackend: PlayerBackend {
|
||||
|
||||
var aspectRatio: Double {
|
||||
#if os(iOS)
|
||||
guard let view = model?.playerLayerView else { return VideoPlayerView.defaultAspectRatio }
|
||||
|
||||
let videoRect = view.playerLayer.videoRect
|
||||
return videoRect.width / videoRect.height
|
||||
playerLayer.videoRect.width / playerLayer.videoRect.height
|
||||
#else
|
||||
VideoPlayerView.defaultAspectRatio
|
||||
#endif
|
||||
@@ -54,7 +51,10 @@ final class AVPlayerBackend: PlayerBackend {
|
||||
}
|
||||
|
||||
private(set) var avPlayer = AVPlayer()
|
||||
var controller: AppleAVPlayerViewController?
|
||||
private(set) var playerLayer = AVPlayerLayer()
|
||||
#if os(tvOS)
|
||||
var controller: AppleAVPlayerViewController?
|
||||
#endif
|
||||
var startPictureInPictureOnPlay = false
|
||||
|
||||
private var asset: AVURLAsset?
|
||||
@@ -79,6 +79,8 @@ final class AVPlayerBackend: PlayerBackend {
|
||||
addFrequentTimeObserver()
|
||||
addInfrequentTimeObserver()
|
||||
addPlayerTimeControlStatusObserver()
|
||||
|
||||
playerLayer.player = avPlayer
|
||||
}
|
||||
|
||||
func bestPlayable(_ streams: [Stream], maxResolution _: ResolutionSetting) -> Stream? {
|
||||
@@ -157,39 +159,9 @@ final class AVPlayerBackend: PlayerBackend {
|
||||
avPlayer.replaceCurrentItem(with: nil)
|
||||
}
|
||||
|
||||
#if os(tvOS)
|
||||
func closePiP(wasPlaying: Bool) {
|
||||
let item = avPlayer.currentItem
|
||||
let time = avPlayer.currentTime()
|
||||
|
||||
avPlayer.replaceCurrentItem(with: nil)
|
||||
|
||||
guard !item.isNil else {
|
||||
return
|
||||
}
|
||||
|
||||
avPlayer.seek(to: time)
|
||||
avPlayer.replaceCurrentItem(with: item)
|
||||
|
||||
guard wasPlaying else {
|
||||
return
|
||||
}
|
||||
|
||||
DispatchQueue.main.asyncAfter(deadline: .now() + 1) { [weak self] in
|
||||
self?.play()
|
||||
}
|
||||
}
|
||||
#else
|
||||
func closePiP(wasPlaying: Bool) {
|
||||
model.pipController?.stopPictureInPicture()
|
||||
|
||||
guard wasPlaying else {
|
||||
return
|
||||
}
|
||||
|
||||
play()
|
||||
}
|
||||
#endif
|
||||
func closePiP() {
|
||||
model.pipController?.stopPictureInPicture()
|
||||
}
|
||||
|
||||
private func loadSingleAsset(
|
||||
_ url: URL,
|
||||
|
@@ -324,7 +324,7 @@ final class MPVBackend: PlayerBackend {
|
||||
client?.stop()
|
||||
}
|
||||
|
||||
func closePiP(wasPlaying _: Bool) {}
|
||||
func closePiP() {}
|
||||
|
||||
func updateControls() {
|
||||
self.logger.info("updating controls")
|
||||
|
@@ -45,7 +45,7 @@ protocol PlayerBackend {
|
||||
|
||||
func closeItem()
|
||||
|
||||
func closePiP(wasPlaying: Bool)
|
||||
func closePiP()
|
||||
|
||||
func updateControls()
|
||||
func startControlsUpdates()
|
||||
|
@@ -67,6 +67,8 @@ final class PlayerModel: ObservableObject {
|
||||
}
|
||||
}
|
||||
|
||||
var playerBackendView = PlayerBackendView()
|
||||
|
||||
@Published var playerSize: CGSize = .zero { didSet {
|
||||
#if !os(tvOS)
|
||||
backend.setSize(playerSize.width, playerSize.height)
|
||||
@@ -167,9 +169,6 @@ final class PlayerModel: ObservableObject {
|
||||
#endif
|
||||
|
||||
private var currentArtwork: MPMediaItemArtwork?
|
||||
#if !os(macOS)
|
||||
var playerLayerView: PlayerLayerView!
|
||||
#endif
|
||||
|
||||
var onPresentPlayer: (() -> Void)?
|
||||
private var remoteCommandCenterConfigured = false
|
||||
@@ -207,6 +206,14 @@ final class PlayerModel: ObservableObject {
|
||||
|
||||
Defaults[.activeBackend] = .mpv
|
||||
playbackMode = Defaults[.playbackMode]
|
||||
|
||||
guard pipController.isNil else { return }
|
||||
pipController = .init(playerLayer: avPlayerBackend.playerLayer)
|
||||
let pipDelegate = PiPDelegate()
|
||||
pipDelegate.player = self
|
||||
|
||||
self.pipDelegate = pipDelegate
|
||||
pipController?.delegate = pipDelegate
|
||||
}
|
||||
|
||||
func show() {
|
||||
@@ -421,7 +428,9 @@ final class PlayerModel: ObservableObject {
|
||||
return
|
||||
}
|
||||
|
||||
if let qualityProfileBackend = qualityProfile?.backend, qualityProfileBackend != activeBackend {
|
||||
if let qualityProfileBackend = qualityProfile?.backend, qualityProfileBackend != activeBackend,
|
||||
qualityProfileBackend == .appleAVPlayer || !(avPlayerBackend.startPictureInPictureOnPlay || playingInPictureInPicture)
|
||||
{
|
||||
changeActiveBackend(from: activeBackend, to: qualityProfileBackend)
|
||||
}
|
||||
|
||||
@@ -464,16 +473,10 @@ final class PlayerModel: ObservableObject {
|
||||
}
|
||||
|
||||
if !presentingPlayer, pauseOnHidingPlayer, !playingInPictureInPicture {
|
||||
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) { [weak self] in
|
||||
DispatchQueue.main.async { [weak self] in
|
||||
self?.pause()
|
||||
}
|
||||
}
|
||||
|
||||
if !presentingPlayer, !pauseOnHidingPlayer, backend.isPlaying {
|
||||
DispatchQueue.main.asyncAfter(deadline: .now() + 1) { [weak self] in
|
||||
self?.play()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func changeActiveBackend(from: PlayerBackendType, to: PlayerBackendType) {
|
||||
@@ -483,6 +486,10 @@ final class PlayerModel: ObservableObject {
|
||||
|
||||
pause()
|
||||
|
||||
if to == .mpv {
|
||||
closePiP()
|
||||
}
|
||||
|
||||
Defaults[.activeBackend] = to
|
||||
self.activeBackend = to
|
||||
|
||||
@@ -553,6 +560,7 @@ final class PlayerModel: ObservableObject {
|
||||
|
||||
func closeCurrentItem(finished: Bool = false) {
|
||||
pause()
|
||||
closePiP()
|
||||
|
||||
prepareCurrentItemForHistory(finished: finished)
|
||||
currentItem = nil
|
||||
@@ -562,7 +570,6 @@ final class PlayerModel: ObservableObject {
|
||||
aspectRatio = VideoPlayerView.defaultAspectRatio
|
||||
resetAutoplay()
|
||||
|
||||
closePiP()
|
||||
exitFullScreen()
|
||||
|
||||
#if !os(macOS)
|
||||
@@ -577,14 +584,11 @@ final class PlayerModel: ObservableObject {
|
||||
return
|
||||
}
|
||||
|
||||
let wasPlaying = isPlaying
|
||||
pause()
|
||||
|
||||
#if os(tvOS)
|
||||
show()
|
||||
#endif
|
||||
|
||||
backend.closePiP(wasPlaying: wasPlaying)
|
||||
backend.closePiP()
|
||||
}
|
||||
|
||||
func handleQueueChange() {
|
||||
|
Reference in New Issue
Block a user