diff --git a/Model/Player/PlayerModel.swift b/Model/Player/PlayerModel.swift index 18e36c45..941694bf 100644 --- a/Model/Player/PlayerModel.swift +++ b/Model/Player/PlayerModel.swift @@ -683,10 +683,11 @@ final class PlayerModel: ObservableObject { return } - guard let video = currentVideo else { return } - guard let stream = backend.bestPlayable(availableStreams, maxResolution: .hd720p30, formatOrder: qualityProfile!.formats) else { return } + // First, we need to create an array with supported formats. + let formatOrderPiP: [QualityProfile.Format] = [.hls, .stream, .mp4] - exitFullScreen() + guard let video = currentVideo else { return } + guard let stream = avPlayerBackend.bestPlayable(availableStreams, maxResolution: .hd720p30, formatOrder: formatOrderPiP) else { return } if avPlayerBackend.video == video { if activeBackend != .appleAVPlayer { @@ -698,7 +699,19 @@ final class PlayerModel: ObservableObject { playStream(stream, of: video, preservingTime: true, upgrading: true, withBackend: avPlayerBackend) } - controls.objectWillChange.send() + var retryCount = 0 + _ = Timer.scheduledTimer(withTimeInterval: 0.5, repeats: true) { [weak self] timer in + if let pipController = self?.pipController, pipController.isPictureInPictureActive, self?.avPlayerBackend.isPlaying == true { + self?.exitFullScreen() + self?.controls.objectWillChange.send() + timer.invalidate() + } else if retryCount < 3, self?.activeBackend == .appleAVPlayer, self?.avPlayerBackend.startPictureInPictureOnSwitch == false { + // If PiP didn't start, try starting it again up to 3 times, + self?.avPlayerBackend.startPictureInPictureOnSwitch = true + self?.avPlayerBackend.tryStartingPictureInPicture() + retryCount += 1 + } + } } var transitioningToPiP: Bool { @@ -726,12 +739,19 @@ final class PlayerModel: ObservableObject { show() #endif - backend.closePiP() if previousActiveBackend == .mpv { saveTime { self.changeActiveBackend(from: self.activeBackend, to: .mpv, isInClosePip: true) - self.controls.resetTimer() + _ = Timer.scheduledTimer(withTimeInterval: 0.5, repeats: true) { [weak self] timer in + if self?.activeBackend == .mpv, self?.mpvBackend.isPlaying == true { + self?.backend.closePiP() + self?.controls.resetTimer() + timer.invalidate() + } + } } + } else { + backend.closePiP() } } diff --git a/Shared/Defaults.swift b/Shared/Defaults.swift index 51dd3dcb..fef49850 100644 --- a/Shared/Defaults.swift +++ b/Shared/Defaults.swift @@ -170,7 +170,7 @@ extension Defaults.Keys { static let hd2160pMPVProfile = QualityProfile(id: "hd2160pMPVProfile", backend: .mpv, resolution: .hd2160p60, formats: QualityProfile.Format.allCases, order: Array(QualityProfile.Format.allCases.indices)) static let hd1080pMPVProfile = QualityProfile(id: "hd1080pMPVProfile", backend: .mpv, resolution: .hd1080p60, formats: QualityProfile.Format.allCases, order: Array(QualityProfile.Format.allCases.indices)) static let hd720pMPVProfile = QualityProfile(id: "hd720pMPVProfile", backend: .mpv, resolution: .hd720p60, formats: QualityProfile.Format.allCases, order: Array(QualityProfile.Format.allCases.indices)) - static let hd720pAVPlayerProfile = QualityProfile(id: "hd720pAVPlayerProfile", backend: .appleAVPlayer, resolution: .hd720p60, formats: [.hls, .stream], order: Array(QualityProfile.Format.allCases.indices)) + static let hd720pAVPlayerProfile = QualityProfile(id: "hd720pAVPlayerProfile", backend: .appleAVPlayer, resolution: .hd720p30, formats: [.hls, .stream], order: Array(QualityProfile.Format.allCases.indices)) static let sd360pAVPlayerProfile = QualityProfile(id: "sd360pAVPlayerProfile", backend: .appleAVPlayer, resolution: .sd360p30, formats: [.hls, .stream], order: Array(QualityProfile.Format.allCases.indices)) #if os(iOS) diff --git a/Shared/Settings/QualityProfileForm.swift b/Shared/Settings/QualityProfileForm.swift index 7e5150e7..545d1422 100644 --- a/Shared/Settings/QualityProfileForm.swift +++ b/Shared/Settings/QualityProfileForm.swift @@ -304,7 +304,7 @@ struct QualityProfileForm: View { func isResolutionDisabled(_ resolution: ResolutionSetting) -> Bool { guard backend == .appleAVPlayer else { return false } - return resolution.value > .hd1080p60 + return resolution.value > .hd720p30 } func initializeForm() {