PiP improvements

This commit is contained in:
Arkadiusz Fal 2022-05-21 22:58:11 +02:00
parent a67a96cfa0
commit eb7cca1c56
9 changed files with 30 additions and 15 deletions

View File

@ -37,7 +37,7 @@ final class AVPlayerBackend: PlayerBackend {
private(set) var avPlayer = AVPlayer()
var controller: AppleAVPlayerViewController?
var enterPiPOnPlay = false
var startPictureInPictureOnPlay = false
var switchToMPVOnPipClose = false
private var asset: AVURLAsset?
@ -323,6 +323,8 @@ final class AVPlayerBackend: PlayerBackend {
}
}
}
self.setRate(self.model.currentRate)
}
let replaceItemAndSeek = {
@ -442,7 +444,7 @@ final class AVPlayerBackend: PlayerBackend {
self,
selector: #selector(itemDidPlayToEndTime),
name: NSNotification.Name.AVPlayerItemDidPlayToEndTime,
object: playerItem
object: model.playerItem
)
}
@ -450,7 +452,7 @@ final class AVPlayerBackend: PlayerBackend {
NotificationCenter.default.removeObserver(
self,
name: NSNotification.Name.AVPlayerItemDidPlayToEndTime,
object: playerItem
object: model.playerItem
)
}
@ -470,6 +472,9 @@ final class AVPlayerBackend: PlayerBackend {
model.hide()
#endif
} else {
if model.playingInPictureInPicture {
startPictureInPictureOnPlay = true
}
model.advanceToNextItem()
}
}
@ -538,10 +543,10 @@ final class AVPlayerBackend: PlayerBackend {
if player.timeControlStatus != .waitingToPlayAtSpecifiedRate {
if let controller = self.model.pipController {
if controller.isPictureInPicturePossible {
if self.enterPiPOnPlay {
self.enterPiPOnPlay = false
DispatchQueue.main.async { [weak self] in
self?.model.pipController?.startPictureInPicture()
if self.startPictureInPictureOnPlay {
self.startPictureInPictureOnPlay = false
DispatchQueue.main.async {
self.model.pipController?.startPictureInPicture()
}
}
}

View File

@ -201,6 +201,8 @@ final class MPVBackend: PlayerBackend {
startControlsUpdates()
}
setRate(model.currentRate)
client?.play()
}

View File

@ -13,7 +13,10 @@ final class PiPDelegate: NSObject, AVPictureInPictureControllerDelegate {
func pictureInPictureControllerWillStartPictureInPicture(_: AVPictureInPictureController) {}
func pictureInPictureControllerDidStartPictureInPicture(_: AVPictureInPictureController) {}
func pictureInPictureControllerDidStartPictureInPicture(_: AVPictureInPictureController) {
player?.playingInPictureInPicture = true
player?.avPlayerBackend.startPictureInPictureOnPlay = false
}
func pictureInPictureControllerDidStopPictureInPicture(_: AVPictureInPictureController) {
if player?.avPlayerBackend.switchToMPVOnPipClose ?? false {
@ -24,12 +27,16 @@ final class PiPDelegate: NSObject, AVPictureInPictureControllerDelegate {
}
}
}
player?.playingInPictureInPicture = false
}
func pictureInPictureControllerWillStopPictureInPicture(_: AVPictureInPictureController) {}
func pictureInPictureController(
_: AVPictureInPictureController,
restoreUserInterfaceForPictureInPictureStopWithCompletionHandler _: @escaping (Bool) -> Void
) {}
restoreUserInterfaceForPictureInPictureStopWithCompletionHandler completionHandler: @escaping (Bool) -> Void
) {
completionHandler(true)
}
}

View File

@ -3,7 +3,7 @@ import Foundation
import SwiftUI
final class PlayerControlsModel: ObservableObject {
@Published var isLoadingVideo = true
@Published var isLoadingVideo = false
@Published var isPlaying = true
@Published var currentTime = CMTime.zero
@Published var duration = CMTime.zero

View File

@ -343,7 +343,6 @@ final class PlayerModel: ObservableObject {
}
inactiveBackends().forEach { $0.pause() }
backend.setRate(currentRate)
let fromBackend: PlayerBackend = from == .appleAVPlayer ? avPlayerBackend : mpvBackend
let toBackend: PlayerBackend = to == .appleAVPlayer ? avPlayerBackend : mpvBackend

View File

@ -111,7 +111,6 @@ extension PlayerModel {
remove(newItem)
currentItem = newItem
pause()
accounts.api.loadDetails(newItem) { newItem in
self.playItem(newItem, video: newItem.video, at: time)
@ -152,7 +151,6 @@ extension PlayerModel {
if play {
currentItem = item
// pause playing current video as it's going to be replaced with next one
pause()
}
queue.insert(item, at: prepending ? 0 : queue.endIndex)

View File

@ -252,7 +252,7 @@ struct PlayerControls: View {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.2) {
print(player.pipController?.isPictureInPicturePossible ?? false ? "possible" : "NOT possible")
player.avPlayerBackend.enterPiPOnPlay = true
player.avPlayerBackend.startPictureInPictureOnPlay = true
player.pipController?.startPictureInPicture()
}
}

View File

@ -30,6 +30,8 @@ struct PlayerQueueRow: View {
Button {
player.prepareCurrentItemForHistory()
player.avPlayerBackend.startPictureInPictureOnPlay = player.playingInPictureInPicture
if history {
player.playHistory(item, at: watchStoppedAt)
} else {

View File

@ -91,6 +91,8 @@ struct VideoCell: View {
playAt = .secondsInDefaultTimescale(watch!.stoppedAt)
}
player.avPlayerBackend.startPictureInPictureOnPlay = player.playingInPictureInPicture
player.play(video, at: playAt, inNavigationView: inNavigationView)
}
}