是否可以更改AVPlayer的背景颜色?如果是,如何?

问题描述 投票:2回答:2

我希望将默认背景颜色从黑色更改为我想要的颜色。可能是与视频形成对比的颜色(大多数情况下为黑色)。

我已经在viewWillAppear()函数中添加了这段代码:

let playerLayer = AVPlayerLayer(player: player)
playerLayer.backgroundColor = UIColor.blueColor().CGColor
ios swift avplayer
2个回答
6
投票

背景的黑色是AVPlayerView ‐控制器视图的backgroundColor,可以自由更改。

例如:

let av = AVPlayerViewController()
av.view.backgroundColor = UIColor.whiteColor()

0
投票

您可以在AVPlayerView上设置背景颜色:

@IBOutlet weak var videoView: AVPlayerView!

override func awakeFromNib() {

    videoView.wantsLayer = true
    videoView.layer?.backgroundColor = NSColor.red.cgColor

    let url = URL(fileURLWithPath: "your file path")
    playerItem =  AVPlayerItem.init(url: url)

    player = AVPlayer.init(playerItem: playerItem)
    player?.allowsExternalPlayback = true

    videoView.player = player

    player?.currentItem?.addObserver(self, forKeyPath: "status", options: NSKeyValueObservingOptions(rawValue: 0), context: nil)
}

override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {

    if let currentPlayer = player , let playerObject = object as? AVPlayerItem, playerObject == currentPlayer.currentItem, keyPath == "status"
    {
        if ( currentPlayer.currentItem!.status == .readyToPlay)
        {
            currentPlayer.play()
            currentPlayer.rate = 1.0;
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.