简体   繁体   中英

Detect when AVPlayer reaches certain percentage played of the total duration?

When a user is playing a video, I am trying to figure out when the user reaches a certain percentage played.

For example, the user playing a video and I want to show some view when the user reaches 50% played.

more clarification, is there a way to setup a "listener/observer" for when 50% occurs?

Best way to observe would probably be to use to periodTimeObserver from docs :

func addPeriodicTimeObserver() {
    // Invoke callback every half second
    let interval = CMTime(seconds: 0.5,
                          preferredTimescale: CMTimeScale(NSEC_PER_SEC))
    // Queue on which to invoke the callback
    let mainQueue = DispatchQueue.main
    // Add time observer
    timeObserverToken =
        player.addPeriodicTimeObserver(forInterval: interval, queue: mainQueue) {
            [weak self] time in
            // Check whether the time has reached half of duration
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM