Search code examples
iosswiftswiftuiavkit

Remove black space from top and bottom of SwiftUI VideoPlayer


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

Solution

  • try this

    VideoPlayer(player: player)
      .aspectRatio(widthOfVideo / heightOfVideo, contentMode: .fit)