Swift Avkit纵横比无法正常工作
我想用边界包围以下游戏仪表。我希望视频扩展到纵横比(确实如此),但是当我围绕它绘制边界时,游戏仪式似乎是格式化为正方形的...
PlayerView(forResource: "x", withExtension: "mp4")
.scaledToFit()
.frame(height: 200)
.cornerRadius(20)
.overlay(
RoundedRectangle(cornerRadius: 20)
.stroke(Color.red, lineWidth: 5)
)
struct PlayerView: UIViewRepresentable {
let forResource: String
let withExtension: String
func updateUIView(_ uiView: UIView, context: UIViewRepresentableContext<PlayerView>) {
}
func makeUIView(context: Context) -> UIView {
return LoopingPlayerUIView(frame: .zero, forResource: forResource, withExtension: withExtension)
}
}
class LoopingPlayerUIView: UIView {
private let playerLayer = AVPlayerLayer()
private var playerLooper: AVPlayerLooper?
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
init(frame: CGRect, forResource: String, withExtension: String) {
super.init(frame: frame)
// Load the resource -> h
let fileUrl = Bundle.main.url(forResource: forResource, withExtension: withExtension)!
let asset = AVAsset(url: fileUrl)
let item = AVPlayerItem(asset: asset)
// Setup the player
let player = AVQueuePlayer()
playerLayer.player = player
playerLayer.videoGravity = .resizeAspect
layer.addSublayer(playerLayer)
// Create a new player looper with the queue player and template item
playerLooper = AVPlayerLooper(player: player, templateItem: item)
// Start the movie
player.play()
}
override func layoutSubviews() {
super.layoutSubviews()
playerLayer.frame = bounds
}
}
I want to surround the below PlayerView with a border. I want the video to scale to aspect ratio (it does) but when I draw the border around it, it seems like the PlayerView is formatted as a square...
PlayerView(forResource: "x", withExtension: "mp4")
.scaledToFit()
.frame(height: 200)
.cornerRadius(20)
.overlay(
RoundedRectangle(cornerRadius: 20)
.stroke(Color.red, lineWidth: 5)
)
struct PlayerView: UIViewRepresentable {
let forResource: String
let withExtension: String
func updateUIView(_ uiView: UIView, context: UIViewRepresentableContext<PlayerView>) {
}
func makeUIView(context: Context) -> UIView {
return LoopingPlayerUIView(frame: .zero, forResource: forResource, withExtension: withExtension)
}
}
class LoopingPlayerUIView: UIView {
private let playerLayer = AVPlayerLayer()
private var playerLooper: AVPlayerLooper?
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
init(frame: CGRect, forResource: String, withExtension: String) {
super.init(frame: frame)
// Load the resource -> h
let fileUrl = Bundle.main.url(forResource: forResource, withExtension: withExtension)!
let asset = AVAsset(url: fileUrl)
let item = AVPlayerItem(asset: asset)
// Setup the player
let player = AVQueuePlayer()
playerLayer.player = player
playerLayer.videoGravity = .resizeAspect
layer.addSublayer(playerLayer)
// Create a new player looper with the queue player and template item
playerLooper = AVPlayerLooper(player: player, templateItem: item)
// Start the movie
player.play()
}
override func layoutSubviews() {
super.layoutSubviews()
playerLayer.frame = bounds
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论