6

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;
    }
}
vrwim
  • 13,020
  • 13
  • 63
  • 118
  • Can you please add code? Just had tried this behavior on [demo project](https://developer.apple.com/library/ios/samplecode/AVPlayerDemo/Introduction/Intro.html), works as expected. – Roman Ermolov Aug 16 '16 at 17:57
  • @RomanErmolov I added my code, I will look into your code to try and see what I am doing wrong – vrwim Aug 16 '16 at 19:13

1 Answers1

2

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.

Anton Malyshev
  • 8,686
  • 2
  • 27
  • 45
  • I wasn't blocking it's thread, but I noticed I had a check if the video was paused (obviously the movie time shouldn't update then), but I had it like this: `time > 0` instead of `time != 0`. – vrwim Aug 17 '16 at 07:31
  • @vrwim in the above code, maybe everything is correct, and your `time>0` was the problem. now is it solved? – T.Todua Aug 18 '16 at 09:29