Replay items in AVQueuePlayer after last

后端 未结 4 2039
独厮守ぢ
独厮守ぢ 2021-01-05 04:29

I need to create something like an infinite loop in my AVQueuePlayer. Especially, I want to replay the whole NSArray of AVPlayerItems

4条回答
  •  孤街浪徒
    2021-01-05 04:54

    In Swift 4, you can use something like the following:

            NotificationCenter.default.addObserver(forName: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: nil, queue: nil) {
    
                notification in
    
                print("A PlayerItem just finished playing !")
    
                currentVideo += 1
    
                if(currentVideo >= videoPaths.count) {
    
                   currentVideo = 0
                }
    
                let url = URL(fileURLWithPath:videoPaths[currentVideo])
                let playerItem = AVPlayerItem.init(url: url)
    
                player.insert(playerItem, after: nil)
            }
    

    From what I understand the AVQueuePlayer will remove the last item played, so you need to keep adding the video just played to the queue, otherwise it will actually run out of videos to play! You can't just .seek to 0 and .play() again, that doesn't work.

提交回复
热议问题