Using NotificationCenter to alternate videos files in AVPlayer

391 views Asked by At

I'm working on implementing a video player in Swift that will detect if a video has stopped playing, and then play the second one. When the second one has stopped playing, the first video should play again.

Here's where I set up the player, assets, and player items:

    //Create URLs
    let movieOneURL: URL = URL(fileURLWithPath: movieOnePath)
    let movieTwoURL: URL = URL(fileURLWithPath: movieTwoPath)

    //Create Assets
    let assetOne = AVAsset(url: movieOneURL)
    let assetTwo = AVAsset(url: movieTwoURL)

    //Create Player Items
    avPlayerItemOne = AVPlayerItem(asset: assetOne)
    avPlayerItemTwo = AVPlayerItem(asset: assetTwo)

    avplayer = AVPlayer(playerItem: avPlayerItemOne)
    let avPlayerLayer = AVPlayerLayer(player: avplayer)
    avPlayerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill
    avPlayerLayer.frame = UIScreen.main.bounds
    movieView.layer.addSublayer(avPlayerLayer)

    //Config player
    avplayer .seek(to: kCMTimeZero)
    avplayer.volume = 0.0

And here's where I set up a notification to detect if the player reached the end of the video file:

    NotificationCenter.default.addObserver(self, selector: #selector(self.playerItemDidReachEnd), name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: avplayer.currentItem)

...which calls this selector:

    func playerItemDidReachEnd(_ notification: Notification) {
//        avplayer.seek(to: kCMTimeZero)
        changePlayerAsset()
//        avplayer.play()
    }

...which will then switch out the asset:

func changePlayerAsset(){
    if avplayer.currentItem == avPlayerItemOne {
        avplayer.replaceCurrentItem(with: avPlayerItemTwo)
        avplayer.play()

    } else if avplayer.currentItem == avPlayerItemTwo {
        avplayer.replaceCurrentItem(with: avPlayerItemOne)
        avplayer.play()
    }
}

This works perfectly the first time through - when the first movie has finished playing, the next one will then start playing.

The problem I'm having is that my notification observer only seems to register once; at the end of the first video...the notification isn't fired when the second video stops playing at all.

Anyone have an idea why that would be the case

1

There are 1 answers

0
Noah Witherspoon On BEST ANSWER

The reason your notification handler isn’t getting called for the second item is this bit, at the end of where you register the notification handler: object: avplayer.currentItem. The handler gets called once, when that item finishes playing, but then when the next item finishes, the notification gets posted with a different object—the other item—which doesn’t match what you registered for, and so your handler doesn’t get called. If you change object to nil when you register the handler, it’ll get called when any item finishes, which is closer to what you’re after.

That said, this isn’t a great way to do what you want—manually swapping out items is likely to incur the cost and delay of loading each item each time it’s about to play. You’d be much better off using the built-in functionality for playing videos in sequence and looping them, namely AVQueuePlayer and AVPlayerLooper. There’s an example of how to use both in the answer to this question.