Is it possible to change the background color of AVPlayer? If yes, how?

狂风中的少年 提交于 2020-01-15 05:13:25

问题


I wish to change the default background color from black to a color that i desire. Possibly to a color which contrasts the video (which is black most of the time).

I have added this piece of code in my viewWillAppear() function:

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

回答1:


The black color of the background is the backgroundColor of the AVPlayerView ‐ Controller’s view, which you are free to change.

For example:

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



回答2:


You can set the background color at 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;
        }
    }
}


来源:https://stackoverflow.com/questions/37204758/is-it-possible-to-change-the-background-color-of-avplayer-if-yes-how

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!