2

With AVPlayer, after seeking to a specific time (let's say 0.5s) and calling play(), periodic observer fires few callbacks with player's currentTime() before the seek position. Here's the code:

import AVFoundation

class Player {

    init() {
        let session = AVAudioSession.sharedInstance()
        do {
            try session.setCategory(.playAndRecord, mode: .videoRecording)
            try session.setActive(true)
            session.requestRecordPermission { allowed in
                if !allowed {
                    fatalError("you must allow mic access")
                }
            }
        } catch {
            fatalError(error.localizedDescription)
        }

        addPeriodicTimeObserver(to: player)

        if let url = Bundle.main.url(forResource: "test_loop", withExtension: "wav") {
            let item = AVPlayerItem(asset: AVAsset(url: url))
            player.replaceCurrentItem(with: item)
        }

        DispatchQueue.main.asyncAfter(deadline: .now() + 3) { [weak self] in
            self?.seek() {
                DispatchQueue.main.asyncAfter(deadline: .now() + 3) {
                    self?.play()
                }
            }
        }
    }

    func addPeriodicTimeObserver(to player: AVPlayer) {
        let time = CMTime(seconds: 0.04, preferredTimescale: timeScale)

        _ = player.addPeriodicTimeObserver(forInterval: time, queue: .main) { [unowned self] _ in
            if
                self.playCalled == true &&
                self.player.currentItem?.status == .readyToPlay &&
                self.player.status == .readyToPlay &&
                self.player.timeControlStatus == .playing {
                print("currentTime \(self.player.currentTime().seconds)")
            }
        }
    }

    func seek(completion: @escaping () -> Void) {
        let time = CMTime(seconds: 0.5, preferredTimescale: timeScale)
        player.seek(to: time, toleranceBefore: .zero, toleranceAfter: .zero) { [unowned self] in
            if $0 {
                print("seek current time: \(self.player.currentTime().seconds)")
                completion()
            }
        }
    }

    func play() {
        if let currentItem = player.currentItem, currentItem.status == .readyToPlay {
            player.play()
            playCalled = true
        }
    }

    let player = AVPlayer()
    let timeScale = CMTimeScale(NSEC_PER_SEC)

    var playCalled: Bool = false

}

It produces:

seek current time: 0.5
currentTime 0.475462793
currentTime 0.475983585
currentTime 0.48074996
currentTime 0.521489168
currentTime 0.561431835
currentTime 0.601529793
currentTime 0.641514043
[...]

IMPORTANT NOTE: This happens only when you run it on the device. Simulator runs are behaving as expected.

As you can see, I tried to guard myself as I could not to print any current time before it might be relevant, but still I get confusing results. Any ideas on what might the problem be and how to fix it? I would love to see the current time staring at the moment I seeked to :)

0 Answers0