swift:无法识别的选择器发送到实例信号sigabrt

时间:2015-10-09 11:31:05

标签: ios swift avplayer

我想在我的应用中播放视频,我使用avplayer没有问题,但在结束视频后,应用程序崩溃了,我收到此错误:

  

无法识别的选择器发送到实例0x7f950bf6d1a0 2015-10-09   14:42:42.769拼车[47925:1543415] ***由于终止应用程序   未捕获的异常'NSInvalidArgumentException',原因:   ' - [Carpooling.ViewController playerItemDidReachEnd:]:无法识别   选择器发送到实例0x7f950bf6d1a0'

我的错误是什么?

lazy var playerLayer:AVPlayerLayer = {

    let player = AVPlayer(URL:  NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("BWWalkthrough", ofType: "mp4")!))
    player.muted = true
    player.allowsExternalPlayback = false
    player.appliesMediaSelectionCriteriaAutomatically = false
    var error:NSError?

    // This is needed so it would not cut off users audio (if listening to music etc.
    do {
        try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryAmbient)
    } catch var error1 as NSError {
        error = error1
    } catch {
        fatalError()
    }
    if error != nil {
        print(error)
    }



    var playerLayer = AVPlayerLayer(player: player)
    playerLayer.frame = self.view.frame
    playerLayer.videoGravity = "AVLayerVideoGravityResizeAspectFill"

    playerLayer.backgroundColor = UIColor.blackColor().CGColor
    player.play()
    player.actionAtItemEnd = AVPlayerActionAtItemEnd.None
    NSNotificationCenter.defaultCenter().addObserver(self, selector: "playerItemDidReachEnd:",
        name: AVPlayerItemDidPlayToEndTimeNotification,
        object: player.currentItem)
    return playerLayer
    }()


override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    self.view.layer.addSublayer(self.playerLayer)

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

2 个答案:

答案 0 :(得分:3)

您正在向NSNotificationCenter注册观察者,并告知观察者在收到通知后致电playerItemDidReachEnd:但您的代码未实施playerItemDidReachEnd:因此代码可以& #39;找到方法并崩溃。

答案 1 :(得分:0)

@Bensarz的回答是正确的。你错过了接收方法。

添加以下方法

func playerItemDidReachEnd(playerItem: AVPlayerItem) {
    //Do your stuff here
}