AVPlayer 消除顶部和底部的黑条/背景(Swift)

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

快速提问,有人知道如何消除视频顶部和底部的黑条吗?我刚刚开始使用 AVPlayer,我只是在这里和那里删除代码,试图删除黑色层。感谢那些可以帮助我的人,谢谢!

UIViewController

import UIKit
import AVKit
import AVFoundation
private var looper: AVPlayerLooper?

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        let path = Bundle.main.path(forResource: "Innie-Kiss", ofType:"mp4")
        let player = AVQueuePlayer(url: URL(fileURLWithPath: path!))
        looper = AVPlayerLooper(player: player, templateItem: AVPlayerItem(asset: AVAsset(url: URL(fileURLWithPath: path!))))
        let controller = AVPlayerViewController()
        controller.player = player
        let screenSize = UIScreen.main.bounds.size
        let videoFrame = CGRect(x: 0, y: 200, width: screenSize.width, height: (screenSize.height - 130) / 2)
        controller.view.frame = videoFrame
        self.view.addSubview(controller.view)
        player.play()
    }

}

ios swift avplayer
3个回答
5
投票

需要设置AVLayer的videoGravity。默认值为

.resizeAspect
,它将保留纵横比,并在顶部/底部或左侧/右侧显示黑条。你想要的是
.resize

你需要设置videoGravity的AVLayer是你的AVPlayer的layer属性。

有关更多信息,请参阅:https://developer.apple.com/documentation/avfoundation/avplayerlayer/1388915-videogravity


1
投票
#import AVKit

var playerController = AVPlayerViewController()
playerController.videoGravity = .resizeAspect
 /*playerController.videoGravity = .resizeAspectFill*/
playerController.view.backgroundColor = UIColor.white //set color as per super or custom view.

Note: If you set the videoGravity of the AVPlayerViewController. The default value is .resizeAspect then it is possible to visibility of black color(default) in background if you wants this color would similar to your super view's color then you must set superview's or custom view's color to your playercontroller's view background color.

*示例:假设超级视图或自定义视图的背景色为白色,则playerController视图的背景色必须设置为playerController.view.backgroundColor = UIColor.white *

注意:playerController 的背景颜色不应始终设置为 UIColor.white 。它必须我设置为超级视图的背景颜色。

If you set playerController.videoGravity = .resizeAspectFill then it will fill the player content to super view or custom view, here also no black color would be shown. So choose either .resizeAspect  or resizeAspectFill as per your requirements.

0
投票

我在播放器上添加了一些垂直填充,从而消除了顶部和底部的黑色边框。

    VideoPlayer(player: player)
        .onAppear() {
            player.play()
        }
        .padding(.vertical, 25)
© www.soinside.com 2019 - 2024. All rights reserved.