简体   繁体   中英

AVPlayer playing even though rate property is 0

This other SO post suggests when the rate property for an AVPlayer is 0, it means the AVPlayer is no longer playing. We use the observer code below to loop a video, but sometimes invoking the pause function on the player fails to break the looping. When debugging, the rate property evaluates to 0.

1) Is the linked SO post wrong, in that a rate value of 0 does not mean the player is paused?

2) Is the continuous looping, even after the pause function is invoked, some kind of race condition where the pause function comes after the playerItemDidReachEnd notification is already issued?

NSNotificationCenter.defaultCenter().addObserver(self, selector: "playerItemDidReachEnd:", name: AVPlayerItemDidPlayToEndTimeNotification, object: playerItem)

private func playVideo() {
    player.seekToTime(kCMTimeZero)
    player.actionAtItemEnd = .Pause
    player.play()
}

when setting up the player:

player.actionAtItemEnd = AVPlayerActionAtItemEnd.None

NSNotificationCenter.defaultCenter().addObserver(self, 
                                                 selector: "playerItemDidReachEnd:", 
                                                 name: AVPlayerItemDidPlayToEndTimeNotification, 
                                                 object: player.currentItem)

this will prevent the player to pause at the end.

in the notification:

func playerItemDidReachEnd(notification: NSNotification) {
    let p: AVPlayerItem = notification.object as! AVPlayerItem
    p.seekToTime(kCMTimeZero)
}

this will rewind the movie.

Don't forget to unregister the notification when releasing the player.

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