AVPlayerLooper 不会循环播放多个视频
AVPlayerLooper doesn't loop through multiple videos
我创建了一个 AVPlayerView
用于使用 AVPlayerLooper
不间断播放 4 个视频。问题是:AVPlayerView
只显示一个循环视频。但我需要播放一个序列(4 个视频)。
如何AVPlayerLooper
播放所有 4 个视频并在之后循环播放?
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()
}
}
找到之前已回答的解决方案并在评论中 post 后,@andy 让我 post 将其作为解决方案。 HERE 是 Harish 之前解决他问题的答案。
我创建了一个 AVPlayerView
用于使用 AVPlayerLooper
不间断播放 4 个视频。问题是:AVPlayerView
只显示一个循环视频。但我需要播放一个序列(4 个视频)。
如何AVPlayerLooper
播放所有 4 个视频并在之后循环播放?
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()
}
}
找到之前已回答的解决方案并在评论中 post 后,@andy 让我 post 将其作为解决方案。 HERE 是 Harish 之前解决他问题的答案。