mohammadreza
mohammadreza

Reputation: 235

detect when video item change in AVQueuePlayer

in my project I use AVQueuePlayer for play multiple video, in some case I want to know when current item finish and next item begin. I use Noftification like this:

NotificationCenter.default.addObserver(self, selector: #selector(self.goToStepTwo), name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: self.player?.currentItem)

but this method only work for first item.

thanks

Upvotes: 1

Views: 542

Answers (1)

Ashley Mills
Ashley Mills

Reputation: 53121

Using

.addObserver(self, selector: #selector(self.goToStepTwo), name: .AVPlayerItemDidPlayToEndTime, object: self.player?.currentItem)

You're explicitly asking do be notified when self.player?.currentItem plays to the end.

To be notified for any item, just use nil as the object

.addObserver(self, selector: #selector(self.goToStepTwo), name: .AVPlayerItemDidPlayToEndTime, object: nil)

From https://developer.apple.com/documentation/foundation/notificationcenter/1411723-addobserver

obj

The object whose notifications the observer wants to receive; that is, only notifications sent by this sender are delivered to the observer.

If you pass nil, the notification center doesn’t use a notification’s sender to decide whether to deliver it to the observer.

Upvotes: 2

Related Questions