Aspect ratio improvements

This commit is contained in:
Arkadiusz Fal
2022-07-10 03:15:15 +02:00
parent d93e9294db
commit cff3838a94
7 changed files with 56 additions and 72 deletions

View File

@@ -34,10 +34,13 @@ final class AVPlayerBackend: PlayerBackend {
}
var aspectRatio: Double {
#if os(tvOS)
VideoPlayerView.defaultAspectRatio
#if os(iOS)
guard let view = model?.playerLayerView else { return VideoPlayerView.defaultAspectRatio }
let videoRect = view.playerLayer.videoRect
return videoRect.width / videoRect.height
#else
controller?.aspectRatio ?? VideoPlayerView.defaultAspectRatio
VideoPlayerView.defaultAspectRatio
#endif
}
@@ -306,36 +309,38 @@ final class AVPlayerBackend: PlayerBackend {
try? AVAudioSession.sharedInstance().setActive(true)
#endif
if self.isAutoplaying(self.model.playerItem!) {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) { [weak self] in
guard let self = self else {
return
}
self.setRate(self.model.currentRate)
if !preservingTime,
let segment = self.model.sponsorBlock.segments.first,
segment.start < 3,
self.model.lastSkipped.isNil
{
self.avPlayer.seek(
to: segment.endTime,
toleranceBefore: .secondsInDefaultTimescale(1),
toleranceAfter: .zero
) { finished in
guard finished else {
return
}
guard let item = self.model.playerItem, self.isAutoplaying(item) else { return }
self.model.lastSkipped = segment
self.model.play()
DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) { [weak self] in
guard let self = self else {
return
}
self.model.aspectRatio = self.aspectRatio
if !preservingTime,
let segment = self.model.sponsorBlock.segments.first,
segment.start < 3,
self.model.lastSkipped.isNil
{
self.avPlayer.seek(
to: segment.endTime,
toleranceBefore: .secondsInDefaultTimescale(1),
toleranceAfter: .zero
) { finished in
guard finished else {
return
}
} else {
self.model.lastSkipped = segment
self.model.play()
}
} else {
self.model.play()
}
}
self.setRate(self.model.currentRate)
}
let replaceItemAndSeek = {

View File

@@ -204,9 +204,11 @@ final class MPVBackend: PlayerBackend {
self.model.lastSkipped = segment
self.play()
self.model.aspectRatio = self.aspectRatio
}
} else {
self.play()
self.model.aspectRatio = self.aspectRatio
}
}
}

View File

@@ -45,6 +45,7 @@ final class PlayerModel: ObservableObject {
@Published var playerSize: CGSize = .zero { didSet {
backend.setSize(playerSize.width, playerSize.height)
}}
@Published var aspectRatio = VideoPlayerView.defaultAspectRatio
@Published var stream: Stream?
@Published var currentRate: Float = 1.0 { didSet { backend.setRate(currentRate) } }
@@ -70,12 +71,6 @@ final class PlayerModel: ObservableObject {
backend.setNeedsNetworkStateUpdates(true)
}}
#if os(iOS)
@Published var motionManager: CMMotionManager!
@Published var lockedOrientation: UIInterfaceOrientation?
@Published var lastOrientation: UIInterfaceOrientation?
#endif
var accounts: AccountsModel
var comments: CommentsModel
var controls: PlayerControlsModel { didSet {
@@ -487,6 +482,7 @@ final class PlayerModel: ObservableObject {
currentItem = nil
backend.closeItem()
aspectRatio = VideoPlayerView.defaultAspectRatio
}
func closePiP() {