gpt4 book ai didi

ios - 视频停止并从头开始播放而不是暂停,swift AVPlayer

转载 作者:行者123 更新时间:2023-11-30 12:32:09 43 4
gpt4 key购买 nike

我有一个 uiView,我正在其中使用自定义控件播放视频。

当我暂停视频时,它会转到视频的开头并暂停,而不是在该特定帧处暂停

以下是我的代码

func playButtonTapped(cell: ViewTableCell) {
guard let indexPath = self.tableView.indexPath(for: cell) else {
return
}
let url = Bundle.main.path(forResource:ArtistFeeds.sharedInstance.videoFeeds[indexPath.row].videoUrl, ofType:"mp4")

let path = NSURL.fileURL(withPath: url!)
currentCell = tableView.cellForRow(at: indexPath) as! ViewTableCell
currentCell.videoPlayerItem = AVPlayerItem.init(url: path)

let playImage = UIImage(named: "image_video_play") as UIImage?
let pauseImage = UIImage(named: "image_video_pause") as UIImage?
if currentCell.avPlayer?.rate == 1.0 {
currentCell.stopPlayback()
currentCell.playButton.isHidden = false
currentCell.playButton.setImage(playImage, for: .normal)

} else {
currentCell.startPlayback()
currentCell.playButton.isHidden = true
currentCell.playButton.setImage(pauseImage, for: .normal)
}

}

我正在 ViewTableCell 类中设置 videoPlayer,如下所示

var avPlayer: AVPlayer?
var avPlayerLayer: AVPlayerLayer?
var videoPlayerItem: AVPlayerItem? = nil {
didSet {

avPlayer?.replaceCurrentItem(with: self.videoPlayerItem)
}
}
@IBAction func playButtonAction(_ sender: UIButton) {
self.delegate?.playButtonTapped(cell: self)
}


func setupMoviePlayer(){
self.avPlayer = AVPlayer.init(playerItem: self.videoPlayerItem)
avPlayerLayer = AVPlayerLayer(player: avPlayer)
avPlayerLayer?.videoGravity = AVLayerVideoGravityResizeAspect
avPlayer?.volume = 3
avPlayer?.actionAtItemEnd = .none
avPlayerLayer?.frame = videoView.bounds
self.backgroundColor = .clear
videoView.layer.insertSublayer(avPlayerLayer!, at: 0)

let interval = CMTime(value: 1, timescale: 2)
avPlayer?.addPeriodicTimeObserver(forInterval: interval, queue: DispatchQueue.main, using :{ (progressTime) in
let seconds = CMTimeGetSeconds(progressTime)

if let duration = self.avPlayer?.currentItem?.duration{
let durationSeconds = CMTimeGetSeconds(duration)
self.videoSlider.value = Float(seconds/durationSeconds)
}
})

}

func stopPlayback(){
self.avPlayer?.pause()
}
func startPlayback(){
self.avPlayer?.play()
}

问题在于每次单击播放按钮“currentCell.videoPlayerItem = AVPlayerItem.init(url: path)”时都会调用它来替换 videoPlayerItem。我想在该特定帧暂停视频,而不是返回到视频的开头。

我该如何克服这个问题。任何帮助将不胜感激。

最佳答案

每次按下按钮时都会替换 videoPlayerItem。这就是它重置的原因:

avPlayer?.replaceCurrentItem(with: self.videoPlayerItem)

关于ios - 视频停止并从头开始播放而不是暂停,swift AVPlayer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43385951/

43 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com