user3766930
user3766930

Reputation: 5829

[AVPlayerLayer superview]: unrecognized selector sent to instance in Swift

In my storyboard I put a view inside of my UIViewController. I added necessary parameters there:

enter image description here

and connected it with my code:

enter image description here

In the code I have:

import AVKit
import AVFoundation

@IBOutlet weak var yawpVideo: AVPlayerLayer!

override func viewDidLoad() {

    let url:NSURL = NSURL(string: videoURL)!

    player = AVPlayer(URL: url)
    yawpVideo.player = player
    player.play()
}

and that causes error:

*** Terminating app due to uncaught exception
 'NSInvalidArgumentException', reason: '-[AVPlayerLayer superview]:
 unrecognized selector sent to instance 0x7fe320cb2030'

Can you tell me what causes this problem and how can it be fixed?

Upvotes: 0

Views: 1732

Answers (1)

JAL
JAL

Reputation: 42459

A UIView is not a CALayer, you cannot add an AVPlayerLayer to the storyboard.

Instead, after your view has loaded, programmatically add your AVPlayer's playerLayer to the UIView's layer.

Change your outlet to be a UIView:

@IBOutlet weak var yawpVideo: UIView!

Then, add the layer:

player = AVPlayer(URL: url)
let playerLayer = AVPlayerLayer(player: player)
yawpVideo.layer.addSublayer(playerLayer)

Upvotes: 4

Related Questions