"advanceToNextItem()" 不允许循环播放所有视频
"advanceToNextItem()" doesn't allow to loop through ALL videos
我在 KVO
的基础上获得了具有循环行为的自动播放视频片段序列。但是,除了自动播放之外,我还想使用 NSButton
推进所有剪辑。因此,如果我在序列自动播放时按下 Next Video
按钮,此操作会将我推进到队列中的下一个剪辑。但它并没有以正确的方式工作。
问题是:当队列结束时,队列中的所有剪辑都没有循环。只有第一个剪辑有一个循环(如果我按 Next Video
按钮 10 次 – 只有第一个剪辑 循环 10 次),之后所有剪辑都会正常自动播放。
问题:如何(在前进时)使用 NSButton 循环播放所有视频?
这是我的代码:
import Cocoa
import AVFoundation
import AVKit
class ViewController: NSViewController {
@IBOutlet weak var avPlayerView: AVPlayerView!
@IBOutlet weak var nextVideoButton: NSButton!
@objc private let player = AVQueuePlayer()
let clips = ["One", "Two", "Three", "Four"]
private var token: NSKeyValueObservation?
override func viewDidAppear() {
super.viewDidAppear()
addAllVideosToPlayer()
}
func addAllVideosToPlayer() {
avPlayerView.player = player
for clip in clips {
let urlPath = Bundle.main.path(forResource: clip, ofType: "mov")!
let url = URL(fileURLWithPath: urlPath)
let playerItem = AVPlayerItem(url: url)
player.insert(playerItem, after: player.items().last)
token = player.observe(\.currentItem) { [weak self] player, _ in
if self!.player.items().count == 1 { self?.addAllVideosToPlayer() }
}
player.play()
avPlayerView.controlsStyle = .none
}
}
@IBAction func nextVideo(_ sender: Any) {
var number: Int = 0
let clip = clips[number]
let urlPath = Bundle.main.path(forResource: clip, ofType: "mov")!
let url = URL(fileURLWithPath: urlPath)
let playerItem = AVPlayerItem(url: url)
player.insert(playerItem, after: player.items().last)
player.advanceToNextItem()
number += 1
if number >= clips.count {
number = 0
player.advanceToNextItem()
number += 1
}
}
}
这是我的 ViewController:
您总是在这里访问第一个视频:
var number: Int = 0
let clip = clips[number]
数字始终为零。将其保留为 class 的成员并在需要时更新它。
这是我的问题的解决方案(感谢@giorashc):
class ViewController: NSViewController {
var number: Int = 0 // class member
.........................................
.........................................
@IBAction func nextVideo(_ sender: Any) {
let clip = clips[number % 4] // modulus of 4
let urlPath = Bundle.main.path(forResource: clip, ofType: "mov")!
let url = URL(fileURLWithPath: urlPath)
let playerItem = AVPlayerItem(url: url)
player.insert(playerItem, after: player.items().last)
player.advanceToNextItem()
number += 1
}
}
我在 KVO
的基础上获得了具有循环行为的自动播放视频片段序列。但是,除了自动播放之外,我还想使用 NSButton
推进所有剪辑。因此,如果我在序列自动播放时按下 Next Video
按钮,此操作会将我推进到队列中的下一个剪辑。但它并没有以正确的方式工作。
问题是:当队列结束时,队列中的所有剪辑都没有循环。只有第一个剪辑有一个循环(如果我按 Next Video
按钮 10 次 – 只有第一个剪辑 循环 10 次),之后所有剪辑都会正常自动播放。
问题:如何(在前进时)使用 NSButton 循环播放所有视频?
这是我的代码:
import Cocoa
import AVFoundation
import AVKit
class ViewController: NSViewController {
@IBOutlet weak var avPlayerView: AVPlayerView!
@IBOutlet weak var nextVideoButton: NSButton!
@objc private let player = AVQueuePlayer()
let clips = ["One", "Two", "Three", "Four"]
private var token: NSKeyValueObservation?
override func viewDidAppear() {
super.viewDidAppear()
addAllVideosToPlayer()
}
func addAllVideosToPlayer() {
avPlayerView.player = player
for clip in clips {
let urlPath = Bundle.main.path(forResource: clip, ofType: "mov")!
let url = URL(fileURLWithPath: urlPath)
let playerItem = AVPlayerItem(url: url)
player.insert(playerItem, after: player.items().last)
token = player.observe(\.currentItem) { [weak self] player, _ in
if self!.player.items().count == 1 { self?.addAllVideosToPlayer() }
}
player.play()
avPlayerView.controlsStyle = .none
}
}
@IBAction func nextVideo(_ sender: Any) {
var number: Int = 0
let clip = clips[number]
let urlPath = Bundle.main.path(forResource: clip, ofType: "mov")!
let url = URL(fileURLWithPath: urlPath)
let playerItem = AVPlayerItem(url: url)
player.insert(playerItem, after: player.items().last)
player.advanceToNextItem()
number += 1
if number >= clips.count {
number = 0
player.advanceToNextItem()
number += 1
}
}
}
这是我的 ViewController:
您总是在这里访问第一个视频:
var number: Int = 0
let clip = clips[number]
数字始终为零。将其保留为 class 的成员并在需要时更新它。
这是我的问题的解决方案(感谢@giorashc):
class ViewController: NSViewController {
var number: Int = 0 // class member
.........................................
.........................................
@IBAction func nextVideo(_ sender: Any) {
let clip = clips[number % 4] // modulus of 4
let urlPath = Bundle.main.path(forResource: clip, ofType: "mov")!
let url = URL(fileURLWithPath: urlPath)
let playerItem = AVPlayerItem(url: url)
player.insert(playerItem, after: player.items().last)
player.advanceToNextItem()
number += 1
}
}