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

标签 ios swift avplayer

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

unrecognized selector sent to instance 0x7f950bf6d1a0 2015-10-09 14:42:42.769 Carpooling[47925:1543415] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Carpooling.ViewController playerItemDidReachEnd:]: unrecognized selector sent to instance 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.
}

最佳答案

你正在使用 NSNotificationCenter 注册一个观察者,并告诉观察者在收到通知时调用 playerItemDidReachEnd: 但你的代码没有实现 playerItemDidReachEnd: 因此代码找不到方法并崩溃。

关于ios - swift:发送到实例信号 sigabrt 的无法识别的选择器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33037209/

相关文章:

iphone - 使用 CLLocationManager 的自定义位置管理器类

ios - 如何快速解析XMPPMessage中的xml?

iphone - ObjectiveC/CMTime - 将 AVPlayer.duration 转换为毫秒

iphone - AVPlayer 在 iPhone 上结束中断时不会恢复播放,但在 iPad 上会这样做

iphone - AFNetworking 检查进度

ios - 有没有办法从 Interface Builder 中控制 leftViewMode (UITextFieldViewMode) 和 leftView?

IOS - 删除或修改在后台访问位置的权限

ios - UIMenuController 未在 UICollectionViewController 子类上显示自定义操作

ios - 如何从minimumLineSpacingForSectionAt获取IndexPath以设置collectionview中项目之间的不同间距?

ios - AVPlayer 工作但在 subview 中不可见