是否可以更改AVPlayer的背景颜色?如果有,怎么样?

时间:2016-05-13 08:15:36

标签: ios swift avplayer

我希望将默认背景颜色从黑色更改为我想要的颜色。可能是与视频形成鲜明对比的颜色(大部分时间都是黑色)。

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

let playerLayer = AVPlayerLayer(player: player)
playerLayer.backgroundColor = UIColor.blueColor().CGColor

2 个答案:

答案 0 :(得分:7)

背景的黑色是AVPlayerView - Controller视图的backgroundColor,您可以自由更改。

例如:

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

答案 1 :(得分: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;
        }
    }
}