StackGU
StackGU

Reputation: 958

NotificationCenter observer calls plays multiple videos - swift - programmatically

I have different UIViews one on top of the other; each of them plays a video using AVPlayer I need each video to be replayed at the end and in order to do so I use this code:

NotificationCenter.default.addObserver(self, selector: #selector(playerDidReachEnd), name: .AVPlayerItemDidPlayToEndTime, object: self.player.currentItem)

@objc fileprivate func playerDidReachEnd(){
 self.player.seek(to: .zero)
 self.player.play()
}

I noticed that when the selector is called, all the other players in the other UIViews start playing as well... This is strange to me because I've set the object of the observer to be only the self.player.currentItem

How can I make only this AVPlayer play?

Upvotes: 0

Views: 114

Answers (1)

matt
matt

Reputation: 534925

The problem is that that code is in every one of those views. So when the notification is posted, all of those views are observers. So they all start playing.

Upvotes: 1

Related Questions