AVPlayer 向后搜索时不更新 currentPlaybackTime
AVPlayer does not update currentPlaybackTime when seeking backwards
我有一个 NSTimer
设置,每 0.1
秒触发一次,在回调中我获取 currentTime()
并使用它来更新带有视频持续时间的标签。
当我向前搜索时,通过将rate
设置为3,此计时器保持同步,但是当我将速率设置为-3时,视频保持同步,但currentTime()
仍然returns 与我开始搜索时相同的值。这种情况会发生,直到我停止寻找然后 currentTime()
returns 正确的时间
如何获取视频的当前时间,这在向后搜索时有效?
编辑: 这是我使用的代码(从 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 调用),也许你只是阻塞了它的线程。
我有一个 NSTimer
设置,每 0.1
秒触发一次,在回调中我获取 currentTime()
并使用它来更新带有视频持续时间的标签。
当我向前搜索时,通过将rate
设置为3,此计时器保持同步,但是当我将速率设置为-3时,视频保持同步,但currentTime()
仍然returns 与我开始搜索时相同的值。这种情况会发生,直到我停止寻找然后 currentTime()
returns 正确的时间
如何获取视频的当前时间,这在向后搜索时有效?
编辑: 这是我使用的代码(从 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 调用),也许你只是阻塞了它的线程。