ios - AVPlayer 向后搜索时不更新 currentPlaybackTime

标签 ios video-streaming nstimer mpmovieplayercontroller avplayer

我有一个 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/

相关文章:

ios - 为什么在交互和滚动时 UITableView 会发生内存泄漏?

ios - 在 Xcode 8 中工作时 Storyboard变得困惑

html - 移动 HTML5 视频格式、视频大小和 FPS

iOS:如何使 swift 3 中事件中的每个计时器失效或终止?

iphone - XCode - 一个文件中索引失败

ios - UICollectionView 滚动单列

video - 将rtsp视频流转为http流

Android 视频不适用于流式传输

objective-c - NSTimer 在触发时导致 "unrecognized selector"崩溃

ios - 显示在 View Controller 之间持续存在的 iOS 应用程序的计时器