问题
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