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

2

6

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
Virginiavirginie answered 13/5, 2016 at 8:15 Comment(0)
B
18

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()
Bunko answered 13/5, 2016 at 8:41 Comment(4)
I declared av in the class and put the second line of code in viewDidLoad. Is that expected to work? Because mine didnt.Virginiavirginie
Compiler error: Use of unresolved identifier AVPlayerViewControllerKimberlykimberlyn
This answer is only relevant if you are using an AVPlayerViewController, not if you are using a standalone AVPlayerLayer (which is what the original question was...)Adjourn
Better use: UIColor.secondarySystemFillVet
S
0

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;
        }
    }
}
Sidwohl answered 31/12, 2019 at 12:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.