视频填满了所有空间(AVPlayer、UIViewControllerRepresentable、SwiftUI)

问题描述 投票:0回答:1

我正在尝试在后台显示视频。 红色部分填满整个屏幕。我怎样才能实现,这样就不会出现红色填充空间并且视图的大小将等于内容大小?

// VideoPlayerUIView is a UIViewControllerRepresentable for AVPlayer
struct VideoPlayerUIView: UIViewControllerRepresentable {
    let player: AVPlayer
    
    func makeUIViewController(context: Context) -> UIViewController {
        let controller = UIViewController()
        let playerLayer = AVPlayerLayer(player: player)
        
        // Set the video to fill the screen
        playerLayer.videoGravity = .resizeAspect
        playerLayer.frame = UIScreen.main.bounds
        controller.view.layer.addSublayer(playerLayer)
        
        return controller
    }
    
    func updateUIViewController(_ uiViewController: UIViewController, context: Context) {}
}

// This is how I display it
 VideoPlayerUIView(player: player1)
          .background(.red)
 

2

swift swiftui uikit avplayer uiviewrepresentable
1个回答
0
投票

如果你想要一些东西来填充一些东西,我想你需要在代码中提及它

playerLayer.videoGravity = .resizeAspectFill
© www.soinside.com 2019 - 2024. All rights reserved.