我正在使用AVPlayer
创建视频播放器,但seekToTime
方法相当慢。苹果应用程序"Photos“的搜索性能给我留下了深刻的印象。有人知道苹果是怎么做到这么快的搜索的吗?
这跟线程有什么关系吗?我试着把seekToTime
在调度队列中调用,也无济于事。
发布于 2015-06-02 11:08:58
我已经找到了解决方案。
如果我使用seekToTime
做擦洗,它是相当慢的。我应该使用的是一个名为stepByCount
来自AVPlayerItem
..。
发布于 2021-02-23 22:30:21
此代码取自:
https://developer.apple.com/library/archive/qa/qa1820/_index.html
它有一点帮助,向前看起来很顺利。但是向后寻找仍然花费了太多的时间(这里SeekToTime向前平滑地工作,而freezy向后工作是解释为什么)。
import AVFoundation
class MyClass {
var isSeekInProgress = false
let player = <#A valid player object #>
var chaseTime = kCMTimeZero
// your player.currentItem.status
var playerCurrentItemStatus:AVPlayerItemStatus = .Unknown
...
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.seekToTime(seekTimeInProgress, toleranceBefore: kCMTimeZero,
toleranceAfter: kCMTimeZero, completionHandler:
{ (isFinished:Bool) -> Void in
if CMTimeCompare(seekTimeInProgress, chaseTime) == 0
{
isSeekInProgress = false
}
else
{
trySeekToChaseTime()
}
})
}
}
https://stackoverflow.com/questions/30437407
复制相似问题