AVPlayer does not update currentPlaybackTime when searching backwards

I have an NSTimer setting that fires every 0.1 seconds, in the callback I select currentTime() and use it to update the label with the video duration.

When I search ahead by setting the rate to 3, this timer will continue to work, but when I set the speed to -3, the video continues to grow, but currentTime() still returns the same value from the moment I started searching. This happens until I dwell on the search and then currentTime() will return the correct time

How can I get the current time of a video that will work when looking back?

Edit: Here is the code I'm using (translated from 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; } } 
+6
source share
1 answer

Try CMTimeGetSeconds(player.currentTime()) instead of player.currentTime().seconds . This works for me.

Also make sure that the timer is actually running (for example, adding NSLog calls to it), maybe you are just blocking its thread.

+2
source

All Articles