Change player size based on aspect ratio

This commit is contained in:
Arkadiusz Fal 2021-11-07 18:53:00 +01:00
parent f33319fd64
commit 76f0a1c66c
4 changed files with 29 additions and 14 deletions

View File

@ -8,6 +8,16 @@ final class PlayerViewController: UIViewController {
var playerModel: PlayerModel!
var playerViewController = AVPlayerViewController()
var aspectRatio: Double? {
let ratio = Double(playerViewController.videoBounds.width) / Double(playerViewController.videoBounds.height)
if !ratio.isFinite {
return VideoPlayerView.defaultAspectRatio
}
return [ratio, 1.0].max()!
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)

View File

@ -2,7 +2,7 @@ import Foundation
import SwiftUI
struct VideoPlayerSizeModifier: ViewModifier {
let geometry: GeometryProxy!
let geometry: GeometryProxy
let aspectRatio: Double?
let minimumHeightLeft: Double
@ -11,7 +11,7 @@ struct VideoPlayerSizeModifier: ViewModifier {
#endif
init(
geometry: GeometryProxy? = nil,
geometry: GeometryProxy,
aspectRatio: Double? = nil,
minimumHeightLeft: Double? = nil
) {
@ -21,15 +21,10 @@ struct VideoPlayerSizeModifier: ViewModifier {
}
func body(content: Content) -> some View {
// TODO: verify if optional GeometryProxy is still used
if geometry != nil {
content
.frame(maxHeight: maxHeight)
.aspectRatio(usedAspectRatio, contentMode: usedAspectRatioContentMode)
.edgesIgnoringSafeArea(edgesIgnoringSafeArea)
} else {
content.edgesIgnoringSafeArea(edgesIgnoringSafeArea)
}
content
.frame(maxHeight: maxHeight)
.aspectRatio(usedAspectRatio, contentMode: usedAspectRatioContentMode)
.edgesIgnoringSafeArea(edgesIgnoringSafeArea)
}
var usedAspectRatio: Double {

View File

@ -67,11 +67,11 @@ struct VideoPlayerView: View {
} else {
#if os(macOS)
Player()
.modifier(VideoPlayerSizeModifier(geometry: geometry))
.modifier(VideoPlayerSizeModifier(geometry: geometry, aspectRatio: player.controller?.aspectRatio))
#else
player.playerView
.modifier(VideoPlayerSizeModifier(geometry: geometry))
.modifier(VideoPlayerSizeModifier(geometry: geometry, aspectRatio: player.controller?.aspectRatio))
#endif
}
}
@ -99,7 +99,7 @@ struct VideoPlayerView: View {
#endif
}
.background()
.modifier(VideoDetailsPaddingModifier(geometry: geometry, fullScreen: fullScreen))
.modifier(VideoDetailsPaddingModifier(geometry: geometry, aspectRatio: player.controller?.aspectRatio, fullScreen: fullScreen))
}
#endif
}

View File

@ -6,6 +6,16 @@ final class PlayerViewController: NSViewController {
var playerView = AVPlayerView()
var pictureInPictureDelegate = PictureInPictureDelegate()
var aspectRatio: Double? {
let ratio = Double(playerView.videoBounds.width) / Double(playerView.videoBounds.height)
if !ratio.isFinite {
return VideoPlayerView.defaultAspectRatio
}
return [ratio, 1.0].max()!
}
override func viewDidDisappear() {
super.viewDidDisappear()
}