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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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