Wednesday, October 28, 2026
HomeiOS Developmentios - Swift -AVPlayer's PeriodicTimeObserver is Briefly Incorrect

ios – Swift -AVPlayer’s PeriodicTimeObserver is Briefly Incorrect

[ad_1]

I have been testing this for a few days now. The video hundreds, begins to play, (1) I pause it, (2) I play it, (3) I pause it. After the 2nd play, the periodicTimeObserver briefly has the inaccurate worth. The first and third time it is right. How is that attainable? It is lower than the participant.currentTime().seconds time that it was after I pressed the button and lower than the .timeControlStatus time earlier than it started to fireplace.

Listed here are the print statements:

(1) pause

buttonPressed-1 -playerTime: 2.024885584
timeControlStatus -player is paused -playerTime: 2.025396285
buttonPressed-2 -playerTime: 2.025653073
timeObserver -currentTime: 2.061 | playerTime: 2.061 <----- time is forward

(2) play

buttonPressed-1 -playerTime: 2.061
timeControlStatus -player is enjoying -playerTime: 2.061
buttonPressed-2 -playerTime: 2.061
timeObserver -currentTime: 2.040027508 | playerTime: 2.040121049 <----- time is INCORRECTLY BEHIND. How can it briefly be behind the playerTime after I pressed the button and the timeControlStatus time

// subsequent cycle it is right
timeObserver -currentTime: 2.062347054 | playerTime: 2.062397636 <----- time is now CORRECTLY forward

(3) pause

buttonPressed-1 -playerTime: 4.186262941
timeControlStatus -player is paused -playerTime:-4.186786764
buttonPressed-2 -playerTime: 4.187047009
timeObserver -currentTime: 4.240444444444444 | playerTime: 4.240444444444444 <----- time is forward

button:

@IBAction func pausePlayButtonPressed(_ sender: UIButton) {
    
    print("buttonPressed-1 -playerTime: (participant.currentTime().seconds)")

    if participant.timeControlStatus == .enjoying {
        
        participant.pause()
        // present playImage

    } else {
        
        participant.play()
        // present pauseImage
    }

    print("buttonPressed-2 -playerTime: (participant.currentTime().seconds)")
}

.timeControlStatus:

func setKVO() {

    playerTimeControlStatusObserver = participant?.observe(.timeControlStatus, choices: [.new, .old]) { // I additionally tried .preliminary and .prior individually and tried all 4 in numerous combos
            [weak self](participant, change) in

        case .enjoying:
                print("timeControlStatus -player is enjoying -playerTime: (participant.currentTime().seconds)")

        case .paused:
                print("timeControlStatus -player is paused -playerTime: (participant.currentTime().seconds)")
    })
}

time observer:

func timeObserver() {
    guard let participant = participant else { return }

    let interval = CMTime(worth: 1, timescale: 30) // I've tried this in a number of totally different worth/timescale combos and the outcomes are nonetheless the identical. Plus 1/30 updates the UISlider easily
        
    token = participant.addPeriodicTimeObserver(forInterval: interval, queue: DispatchQueue.most important, utilizing:  playerTime: (participant.currentTime().seconds)")
    )
}

[ad_2]

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments