AVPlayer在向后搜索时不更新currentPlaybackTime
我有一个NSTimer
设置,每调用0.1
秒就会触发一次,在回调中我获取currentTime()
并使用它来更新带有视频持续时间的标签。AVPlayer在向后搜索时不更新currentPlaybackTime
当我寻求转发时,通过将rate
设置为3,此计时器保持不变,但是当我将速率设置为-3时,视频保持不变,但currentTime()
仍会返回与我开始寻找相同的值。直到我停止查找,然后currentTime()
返回正确的时间
如何获取当前视频所在的时间,这将在向后寻找时工作?
编辑:这里是我使用的代码(从Xamarin C#翻译):
class VideoPlayer: UIView {
var player: AVPlayer!
var wasPaused: Bool!
func play(url: String) {
// set the URL to the Video Player
let streamingURL: NSURL = NSURL(string: url)!
player = AVPlayer(URL: streamingURL)
let playerLayer = AVPlayerLayer(layer: player)
layer.insertSublayer(playerLayer, atIndex: 0)
// Reset the state
player.seekToTime(CMTime(seconds: 0, preferredTimescale: 600))
// Start a timer to move the scrub label
NSTimer(timeInterval: 0.1, target: self, selector: #selector(playbackTimeUpdated), userInfo: nil, repeats: true)
}
func playbackTimeUpdated() {
// This one is not correct when seeking backwards
let time = player.currentTime().seconds;
// Use the time to adjust a UIProgressView
}
// Gets called when the reverse button is released
func reverseTouchUp() {
player.rate = 1
}
// Gets called when the reverse button is pressed
func reverseTouchDown()
{
player.rate = -3;
}
}
尝试CMTimeGetSeconds(player.currentTime())
,而不是player.currentTime().seconds
。这个对我有用。
另外检查你的计时器是否真的在运行(例如添加NSLog调用),也许你只是阻塞它的线程。
我没有阻止它的线程,但我注意到我有一个检查,如果视频暂停(显然电影时间应该然后我会这样做:'time> 0'而不是'time!= 0'。 – vrwim
@vrwim在上面的代码中,也许一切正确,你的'时间> 0'是问题。现在解决了吗? –
你可以添加代码吗?刚刚在[demo project](https://developer.apple.com/library/ios/samplecode/AVPlayerDemo/Introduction/Intro.html)上试过这种行为,按预期工作。 –
@RomanErmolov我加了我的代码,我会查看你的代码,看看我做错了什么 – vrwim