Check play state of AVPlayer
You can tell it's playing using:
AVPlayer *player = ...
if ((player.rate != 0) && (player.error == nil)) {
// player is playing
}
Swift 3 extension:
extension AVPlayer {
var isPlaying: Bool {
return rate != 0 && error == nil
}
}
In iOS10, there's a built in property for this now: timeControlStatus
For example, this function plays or pauses the avPlayer based on it's status and updates the play/pause button appropriately.
@IBAction func btnPlayPauseTap(_ sender: Any) {
if aPlayer.timeControlStatus == .playing {
aPlayer.pause()
btnPlay.setImage(UIImage(named: "control-play"), for: .normal)
} else if aPlayer.timeControlStatus == .paused {
aPlayer.play()
btnPlay.setImage(UIImage(named: "control-pause"), for: .normal)
}
}
As for your second question, to know if the avPlayer reached the end, the easiest thing to do would be to set up a notification.
NotificationCenter.default.addObserver(self, selector: #selector(self.didPlayToEnd), name: .AVPlayerItemDidPlayToEndTime, object: nil)
When it gets to the end, for example, you can have it rewind to the beginning of the video and reset the Pause button to Play.
@objc func didPlayToEnd() {
aPlayer.seek(to: CMTimeMakeWithSeconds(0, 1))
btnPlay.setImage(UIImage(named: "control-play"), for: .normal)
}
These examples are useful if you're creating your own controls, but if you use a AVPlayerViewController, then the controls come built in.