我正在使用AVPlayer播放视频,我只想在用户不搜索视频时播放视频。如果用户想在视频中走得更远(即,他正在搜索),则视频将暂停,并且当用户完成搜索后,播放器应从搜索结束处继续播放。
有办法吗?
这就是我现在在播放器中拥有的。
final class PlayerViewController {
private var playPosition: Int = 0
private var didInitialSetup = false
var mainPlayerItem: AVPlayerItem?
var player: AVPlayer?
var playerController: AVPlayerViewController!
private var avPlayerItems: [AVPlayerItem] = []
weak var delegate: PlayerViewControllerDelegate?
override var mainPlayerItem: AVPlayerItem? {
willSet {
observers.forEach { observer in
observer.invalidate()
}
observers.removeAll()
let statusObserver = newValue?.observe(\.status, options: [.old, .new], changeHandler: { [weak self] item, _ in
guard let strongSelf = self else { return }
switch item.status {
case .readyToPlay:
if strongSelf.didInitialSetup == false {
strongSelf.initialSetup()
}
case .failed:
if let playError = item.error {
strongSelf.displayError(error: playError)
}
case .unknown:
break
}
})
if let observer = statusObserver {
self.observers.append(observer)
}
}
}
override func viewDidLoad() {
super.viewDidLoad()
self.loadAVPlayerItems()
}
// MARK: - Private methods
private func loadAVPlayerItems() {
self.player = AVQueuePlayer(items: self.avPlayerItems)
self.playerController.player = self.player
self.player?.play()
let rateObserver = self.player?.observe(\.rate, options: [.new], changeHandler: { [weak self] player, _ in
guard let strongSelf = self else { return }
if player.rate == 0 {
strongSelf.cancelNext()
}
})
if let observer = rateObserver {
self.observers.append(observer)
}
if let lastItem = avPlayerItems.last {
NotificationCenter.default.addObserver(self, selector: #selector(self.playerItemDidReachEnd), name: .AVPlayerItemDidPlayToEndTime, object: lastItem)
}
}
@objc func playerItemDidReachEnd() {
if let time = self.player?.currentItem?.currentTime() {
self.eventsLogger?.playerDidComplete(playHeadCMTime: time)
}
self.playheadTimer?.invalidate()
self.playheadTimer = nil
self.mainPlayerItem = nil
self.player?.pause()
self.playPosition = 0
self.dismiss(animated: true, completion: nil)
}
private func initialSetup() {
guard self.isViewLoaded && (self.view.window != nil) else {
self.player?.pause()
return
}
self.didInitialSetup = true
let timePosition = CMTime(seconds: Double(self.playPosition), preferredTimescale: CMTimeScale(NSEC_PER_SEC))
if self.playPosition > 1 {
self.player?.seek(to: timePosition)
}
self.eventsLogger?.playerDidStart(playHeadCMTime: timePosition)
self.playheadTimer?.invalidate()
self.playheadTimer = nil
self.startPlayheadTimer()
}
}
class CustomAVPlayer: AVPlayer {
override func seek(to time: CMTime, toleranceBefore: CMTime, toleranceAfter: CMTime, completionHandler: @escaping (Bool) -> Void) {
super.seek(to: time, toleranceBefore: toleranceBefore, toleranceAfter: toleranceAfter, completionHandler: completionHandler)
print(">> user scrubbing")
}
}
答案 0 :(得分:0)
我将根据我的理解并参考一般情况来回答。
此外,我假设您正在构建自己的视频播放器。
看一下可以用来观察的玩家变量。 AVFoundation具有许多可观察的属性,您应该利用它来发挥自己的优势。以下是一些要研究的属性:
AVPlayerItem的playbackBufferEmpty
:,当您向前搜索时会收到通知。除非您的视频播放完毕。通常,您应该在缓冲区为空时暂停,在缓冲区为空时继续。
AVPlayer的rate
:一般来说,如果不是1.0
并且播放器没有暂停,则应该暂停。那就是您期望正常的播放速度。但是,如果您支持不同的播放速度(例如,使用类似canPlaySlowForward
的播放速度),则可能需要稍作调整。
值0.0会暂停视频,而值1.0会以自然速率播放当前项目。
AVPlayer的status
AVPlayerItem的status
当我说观察时,我的意思是在容纳球员的班级使用observeValueForKeyPath:ofObject:change:context:
之类的东西。
您可以对照您要观察的值检查keyPath
。