4

When the SwiftUI VideoPlayer is used, black space appears above and below the video. How can this black space be removed so that only the video with no extra space is shown. I haven't found any way to do this within SwiftUI.

 import SwiftUI
 import AVKit

 struct VideoView: View {
     let player = AVPlayer(url: URL(string: "https://vod-progressive.akamaized.net/exp=1606269156~acl=%2A%2F1123020046.mp4%2A~hmac=8d8e8fd9cd3c9de1b25de66bfd291f420d634f43137346daaa163bc0fbaebb22/vimeo-prod-skyfire-std-us/01/4047/11/295238750/1123020046.mp4?filename=What+Star+Wars+Can+Teach+Us+About+Swift.mp4")!)

     var body: some View {
         VStack {
             Spacer()
        
             VideoPlayer(player: player)
        
             Spacer()
         }
     }
 }
Ryan
  • 630
  • 8
  • 20
  • 1
    Did you try `.edgesIgnoringSafeArea(.all)`? – pawello2222 Nov 24 '20 at 21:58
  • Just tried it, no luck. I think the issue may be with the AVPlayer object. I haven't found any native swiftui way to fix this. – Ryan Nov 24 '20 at 22:02
  • 2
    It looks like you need to access `videoGravity` from `AVPlayerLayer`. I don't think it's exposed by SwiftUI's VideoPlayer. You may need to use UIKit for this. – pawello2222 Nov 24 '20 at 22:17
  • 2
    Yeah, I think you're right. I'm not very familiar with UIKit though, it would be super helpful if it could be demonstrated how to get this functionality within a SwiftUI view hierarchy. I'm sure other people learning SwiftUI will hit this roadblock at some point as well. – Ryan Nov 24 '20 at 22:21

1 Answers1

0

try this

VideoPlayer(player: player)
  .aspectRatio(widthOfVideo / heightOfVideo, contentMode: .fit)
yoodu
  • 47
  • 2