【问题标题】:AVPlayer does not update currentPlaybackTime when seeking backwardsAVPlayer 向后搜索时不更新 currentPlaybackTime
【发布时间】: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


【解决方案1】:

尝试CMTimeGetSeconds(player.currentTime()) 而不是player.currentTime().seconds。它对我有用。

还要检查你的计时器是否真的在运行(例如添加 NSLog 调用),也许你只是阻塞了它的线程。

【讨论】:

  • 我没有阻止它的线程,但我注意到我检查了视频是否暂停(显然电影时间不应该更新),但我有这样的:time > 0而不是time != 0
  • @vrwim 在上面的代码中,也许一切都是正确的,而你的time>0 是问题所在。现在解决了吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-12-18
  • 2021-11-12
  • 1970-01-01
  • 2013-03-04
  • 1970-01-01
  • 2014-11-29
  • 2016-03-19
相关资源
最近更新 更多