Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AVPlayer does not update currentPlaybackTime when seeking backwards

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

When I am seeking forwards, by setting the rate to 3, this timer keeps up, but when I set the rate to -3, the video keeps up, but the currentTime() still returns the same value from when I started seeking. This occurs until I stop seeking and then currentTime() returns the correct time

How can I fetch the current time the video is at, which will work when seeking backwards?

Edit: Here is the code I use (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;
    }
}
like image 670
vrwim Avatar asked Aug 03 '16 10:08

vrwim


1 Answers

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

Also check that you timer is actually running (add NSLog calls to it for example), maybe you just blocking its thread.

like image 187
Anton Malyshev Avatar answered Oct 10 '22 18:10

Anton Malyshev