Mohammad Alikhani
Mohammad Alikhani

Reputation: 191

swift: unrecognized selector sent to instance signal sigabrt

i want play a video in my app, i use avplayer without problem but after ending video, app is crashing and i get this error:

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'

what is my mistake?

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.
}

Upvotes: 0

Views: 921

Answers (2)

pkc
pkc

Reputation: 8516

Upper answer by @Bensarz is correct. You are missing the receiver method.

Add the below method

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

Upvotes: 0

bsarrazin
bsarrazin

Reputation: 4040

You're registering an observer with NSNotificationCenter and telling the observer to call playerItemDidReachEnd: when it gets notified but your code doesn't implement playerItemDidReachEnd: therefore the code can't find the method and crashes.

Upvotes: 3

Related Questions