AVPlayerLooper不会循环播放多个视频

时间:2018-03-07 09:33:04

标签: swift macos loops avfoundation avkit

我使用AVPlayerLooper制作了AVPlayerView,可以不间断播放4个视频。

问题是:AVPlayerLooper只显示一个循环视频。但我需要播放一个序列(4个视频)。

如何让import Cocoa import AVKit 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() } } 播放所有4个视频并在此之后循环播放?

{{1}}

1 个答案:

答案 0 :(得分:1)

找到之前已回答的解决方案并将其发布在评论中后,@ andy要求我将其作为解决方案发布。 HERE是Harish先前的回答,解决了他的问题。