我希望将默认背景颜色从黑色更改为我想要的颜色。可能是与视频形成对比的颜色(大多数情况下为黑色)。
我已经在viewWillAppear()
函数中添加了这段代码:
let playerLayer = AVPlayerLayer(player: player)
playerLayer.backgroundColor = UIColor.blueColor().CGColor
背景的黑色是AVPlayerView ‐控制器视图的backgroundColor,可以自由更改。
例如:
let av = AVPlayerViewController()
av.view.backgroundColor = UIColor.whiteColor()
您可以在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;
}
}
}