wades
wades

Reputation: 292

Set opacity of AVPlayerLayer

I have seen quite a few posts of people wanting to play videos with transparent backgrounds. That is not what I am trying to accomplish here.

I am trying to set the opacity of the whole layer. Below is how I am setting the opacity of an image layer. I have tried the same approach to set opacity for the video layer but nothing happened when I attempted to fade

    @IBAction func imageFadeAction(_ sender: Any) {
    let rounded = Int(round(imageFader.value))
    imageLabel.text = "\(rounded.description)%"
    let fade = imageFader.value / 100
    SecondScreen.main?.backgroundImage.alpha = CGFloat(fade)
    SecondScreen.main?.viewDidLoad()
}

This is how I am playing the video and generating the layer

    func playVideo() {
    let videoURL = SecondScreen.videoPath
    self.player = AVPlayer(url: videoURL!)
    self.avpController = AVPlayerLayer(player: player)
    avpController.frame = self.view.bounds
    self.view.layer.addSublayer(avpController)
    player.play()
}

Can anyone tell me if its possible or point me in the right direction

Upvotes: 2

Views: 1207

Answers (2)

Septronic
Septronic

Reputation: 1176

You can set its opacity to 0. something like this:

    guard let path = Bundle.main.path(forResource: "SomeVideo", ofType: ".Someformat") else {
        return nil
    }
    let player = AVPlayer(url: URL(fileURLWithPath: path))
    let playerLayer = AVPlayerLayer(player: player)

    playerLayer.opacity = 0
    .
    .
    .

Upvotes: 0

wades
wades

Reputation: 292

To answer my own question, Use a container view. play your video in a separate ViewController and embed it in the container view. THEN you can set the opacity of the container view.

I don't know if this is the correct way to do it, but it works for me and what I needed. If someone has a better answer please feel free to share

Upvotes: 3

Related Questions