user3766930 user3766930 - 1 year ago 149
Swift Question

[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

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?

Answer Source

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)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download