[AVPlayerLayer superview]:无法识别的选择器发送到Swift中的实例

时间:2016-09-22 14:24:55

标签: ios swift storyboard avfoundation avplayerlayer

在我的故事板中,我在UIViewController中放置了一个视图。 我在那里添加了必要的参数:

enter image description here

并将其与我的代码相关联:

enter image description here

在我的代码中:

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()
}

,这会导致错误:

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

你能告诉我导致这个问题的原因以及如何解决这个问题?

1 个答案:

答案 0 :(得分:4)

UIView不是CALayer,您无法在故事板中添加AVPlayerLayer

相反,在您的视图加载后,以编程方式将您的AVPlayer playerLayer添加到UIView的图层。

将您的插座更改为UIView

@IBOutlet weak var yawpVideo: UIView!

然后,添加图层:

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