我有一个 NSTimer
设置,每 0.1
秒触发一次,在回调中我获取 currentTime()
并使用它来更新带有视频持续时间的标签。
当我向前寻找时,通过将 rate
设置为 3,此计时器会保持同步,但是当我将 rate 设置为 -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 调用),也许你只是阻塞了它的线程。
关于ios - AVPlayer 向后搜索时不更新 currentPlaybackTime,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38740752/