6

I created a AVPlayerView for non-stop playback of 4 videos using AVPlayerLooper. The problem is: AVPlayerView shows only one looped video. But I need to play a sequence (4 videos).

How to make AVPlayerLooper play all 4 videos and loop after that?

import Cocoa
import AVFoundation

class ViewController: NSViewController {

    @IBOutlet weak var viewOne: AVPlayerView!
    var playerLooper: AVPlayerLooper? = nil

    override func viewDidLoad() {
        super.viewDidLoad()
    
        let videos = (1...4).map { _ in
            Bundle.main.urls(forResourcesWithExtension: "mov", 
                                          subdirectory: nil)![Int(arc4random_uniform(UInt32(4)))]
        }
    
        viewOne.player = AVQueuePlayer(url: videos[Int(arc4random_uniform(UInt32(4)))])
        let item = AVPlayerItem(url: videos[Int(arc4random_uniform(UInt32(4)))])
        playerLooper = AVPlayerLooper(player: viewOne.player as! AVQueuePlayer, templateItem: item)
        viewOne.player?.actionAtItemEnd = .advance
        viewOne.controlsStyle = .none

        NotificationCenter.default.addObserver(self, 
                                     selector: #selector(loopSequence), 
                                         name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, 
                                       object: viewOne.player?.currentItem)
    
        func loopSequence() {
            self.viewOne.player?.pause()
            self.viewOne.player?.currentItem?.seek(to: kCMTimeZero, completionHandler: nil)
            self.viewOne.player?.play()
        }
        loopSequence()
    }
}
Andy Jazz
  • 49,178
  • 17
  • 136
  • 220
  • `AVPlayerLooper` is for only ONE item. I'd suggest to create a queue of 4 `AVPlayerItem` and use this to detect the end and "restart/loop". https://stackoverflow.com/questions/5361145/looping-a-video-with-avfoundation-avplayer – Larme Mar 13 '18 at 15:01
  • 2
    There are answers that are in Swift in the linked question. Did you try them? – Larme Mar 13 '18 at 15:07
  • Have you tried using [`AVQueuePlayer`](https://developer.apple.com/documentation/avfoundation/avqueueplayer) – GoodSp33d Apr 03 '18 at 12:47
  • I'm using AVQueuePlayer. – Andy Jazz Apr 03 '18 at 14:28
  • 1
    I'm fairly certain that [this is an exact solution](https://stackoverflow.com/a/45598546/9333764) for your situation. – Jake Apr 07 '18 at 17:07

1 Answers1

1

After finding the solution that had been previously answered and posting it in the comments, @andy asked me to post it as a solution. HERE is the previous answer by Harish that solved his problem.

Jake
  • 2,126
  • 1
  • 10
  • 23
  • I can still help. I’ll be busy today but I’ll try to get to this tomorrow – Jake Apr 08 '18 at 14:02
  • Have you set breakpoints and stepped through to see what happens? Specifically at the line of code that is supposed to play the video – Jake Apr 10 '18 at 18:19
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/168672/discussion-between-jake-and-andy). – Jake Apr 10 '18 at 18:21