How change videoGravity of VideoPlayer in SwiftUI

I have the new iOS 14 VideoPlayer:

Code Block
private let player = AVPlayer(url: Bundle.main.url(forResource: "TL20_06_Shoba3_4k", withExtension: "mp4")!)
var body: some View {
VideoPlayer(player: player)
.aspectRatio(contentMode: .fill)
...


This player setup can not display 4:3 video on 16:9 screen of tv without black stripes. Modifier aspectRatio does not work on VideoPlayer.

How can I set videoGravity of existed AVPlayerLayer to resizeAspectFill via SwiftUI API?

Replies

You can use the .scaledToFill() view modifier on VideoPlayer:
VideoPlayer(player: player).scaledToFill()
VideoPlayer(player: player).scaledToFill()
doesn't work either
Post not yet marked as solved Up vote reply of ngb Down vote reply of ngb
Bump. Any solution to this?

Bump this would be nice to do

This should be solved with videoGravity = .resizeAspectFill

You should be able to use

.aspectRatio(CGSize(width: 9, height: 16), contentMode: .fill)

If VideoPlayer is within a ZStack it will try to take as much room as possible. In some instance, when I am layering content on top of a video, you need to make the VideoPlayer the background of the view.

var body: some View {
        ZStack {
            VStack {
                Text("Hello")
                    .frame(maxWidth: .infinity, alignment: .leading)
                    .background(Color.red)
                Spacer()
            }
        }
        .background(
            VideoPlayer(player: AVPlayer(url: URL(string: "url")!))
                .aspectRatio(CGSize(width: 9, height: 16), contentMode: .fill)
                .ignoresSafeArea()
        )
    }