【发布时间】:2016-08-03 10:17:21
【问题描述】:
我有一个NSTimer 设置,每0.1 秒触发一次,在回调中我获取currentTime() 并使用它来更新带有视频持续时间的标签。
当我寻求转发时,通过将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;
}
}
【问题讨论】:
-
你能添加代码吗?刚刚在 demo project 上尝试过这种行为,按预期工作。
-
@RomanErmolov 我添加了我的代码,我会查看你的代码,看看我做错了什么
标签: ios video-streaming nstimer mpmovieplayercontroller avplayer