使用AVPlayer实现流畅的视频清理 [英] Achieve smooth video scrubbing with AVPlayer
问题描述
我正在尝试使用 AVPlayer实现流畅的视频清理
通过 UISlider
我搜索过,似乎Apple有一个 Technical Q& A 并解释了如何实现这一目标,但我的问题是如何使用此方法并使用UISlider更改玩家当前时间:
stopPlayingAndSeekSmoothlyToTime(newChaseTime:CMTime)
I am trying to achieve smooth video scrubbing with AVPlayer
through UISlider
I have searched and it seems Apple has a Technical Q&A and explained how to achieve this, but my problem is how should I use this method and change player current time with a UISlider:
stopPlayingAndSeekSmoothlyToTime(newChaseTime:CMTime)
这是我的代码:
//Play Intro Movie
let videoURL = Bundle.main.url(forResource: "intro", withExtension: "mp4")
player = AVPlayer(url:videoURL!)
let playerLayer = AVPlayerLayer(player: player)
playerLayer.frame = self.view.frame
view.layer.addSublayer(playerLayer)
//videoPlayer.play()
view.addSubview(slider)
slider.maximumValue = 0
slider.maximumValue = Float(CMTimeGetSeconds((player.currentItem?.asset.duration)!))
这是Apple示例代码:
Here is Apple sample code :
func stopPlayingAndSeekSmoothlyToTime(newChaseTime:CMTime)
{
player.pause()
if CMTimeCompare(newChaseTime, chaseTime) != 0
{
chaseTime = newChaseTime;
if !isSeekInProgress
{
trySeekToChaseTime()
}
}
}
func trySeekToChaseTime()
{
if playerCurrentItemStatus == .unknown
{
// wait until item becomes ready (KVO player.currentItem.status)
}
else if playerCurrentItemStatus == .readyToPlay
{
actuallySeekToTime()
}
}
func actuallySeekToTime()
{
isSeekInProgress = true
let seekTimeInProgress = chaseTime
player.seek(to: seekTimeInProgress, toleranceBefore: kCMTimeZero,
toleranceAfter: kCMTimeZero, completionHandler:
{ (isFinished:Bool) -> Void in
if CMTimeCompare(seekTimeInProgress, self.chaseTime) == 0
{
self.isSeekInProgress = false
}
else
{
self.trySeekToChaseTime()
}
})
}
推荐答案
虽然我没有使用与你一样的方法<$ div c $ c> stopPlayingAndSeekSmoothlyToTime ,我想我应该帮助你寻找玩家的行动。
Although I'm not using the same method as you do which is stopPlayingAndSeekSmoothlyToTime
, I thought I should help you with the seeking action of the player.
func sliderValueChanged() {
var timeToSeek = player.currentItem?.asset.duration.seconds
timeToSeek = timeToSeek * Double(slider.value)
player.seek(to: CMTimeMake(Int64(timeToSeek), 1))
}
另外你应该设置 slider.maximumValue
为1.希望这会有所帮助。
Also you should set the slider.maximumValue
to 1. Hope this helps.
注意:请不要忘记处理 currentItem
可选值。如果它是 nil
,你应该为 timeToSeek
变量设置值0。
Note: Please don't forget to handle currentItem
optional value. If it is nil
you should set the value 0 for timeToSeek
variable.
这篇关于使用AVPlayer实现流畅的视频清理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!