简体   繁体   中英

Swift: Detect video end

I have to play some videos in my swift application. The video is working perfectly but I want to detect when the video is ended. I searched about that then I found that the NotificationCenter is the solution for that. I used this code but my application crash at the end of the video.

This is my code:

func playVideo(url: NSURL){
let player = AVPlayer(url: url as URL)

        NotificationCenter.default.addObserver(self, selector: Selector(("playerDidFinishPlaying")), name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: player.currentItem)
player.play()
}

func playerDidFinishPlaying(note: NSNotification) {
        print("Video Finished")
    }

The error is:

[myApp.myViewController playerDidFinishPlaying]: unrecognized selector sent to instance 0x79669740

Any help please?

Because your selector is wrong, obviously. You are saying:

Selector(("playerDidFinishPlaying"))

But that is not the Objective-C name of your method.

Clearly, you don't know how to make the Objective-C name of your method. And you don't have to! This is exactly what #selector syntax solves. Just use it:

#selector(playerDidFinishPlaying)

And now it will work, because Swift will solve the problem you don't know how to solve.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM