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
}
}