AVPlayerItemDidPlayToEndTime Notification never fires in SwiftUI

29 viewsiosswiftui
0

I know there are many questions and answers about this but none of them helped.
Everything is working as they are suppose to be working except this notification.
I can not detect end of video like this.

So I am using playerItem.currentTime() to check the end.
What a dayum.

.onAppear {
                    
                    let playerItem = AVPlayerItem(url: videoURL!)
                    let videoOutput = AVPlayerItemVideoOutput(pixelBufferAttributes: nil)
                    playerItem.add(videoOutput)
                    
                    let player = AVPlayer(playerItem: playerItem)
                    self.player = player
                    
                    NotificationCenter.default.addObserver(self, selector: #selector(VideoPlayerCoordinator.playerDidFinishPlaying), name: .AVPlayerItemDidPlayToEndTime, object: nil)
                    
                    player.play()
                    
                    self.coordinator = VideoPlayerCoordinator(videoOutput: videoOutput,
                                                              playerItem: playerItem)
                    
                    
                }

This is the notification action func in class VideoPlayerCoordinator.

@objc func playerDidFinishPlaying() {
    
    print("Video finished playing")

  
}