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.
To get notification for reaching the end of an item (via Apple):
[[NSNotificationCenter defaultCenter] addObserver:<self> selector:@selector(<#The selector name#>) name:AVPlayerItemDidPlayToEndTimeNotification object:<#A player item#>];
And to track playing you can:
"track changes in the position of the playhead in an AVPlayer object" by using addPeriodicTimeObserverForInterval:queue:usingBlock: or addBoundaryTimeObserverForTimes:queue:usingBlock:.
Example is from Apple:
// Assume a property: @property (retain) id playerObserver;Float64 durationSeconds = CMTimeGetSeconds([<#An asset#> duration]);CMTime firstThird = CMTimeMakeWithSeconds(durationSeconds/3.0, 1);CMTime secondThird = CMTimeMakeWithSeconds(durationSeconds*2.0/3.0, 1);NSArray *times = [NSArray arrayWithObjects:[NSValue valueWithCMTime:firstThird], [NSValue valueWithCMTime:secondThird], nil];self.playerObserver = [<#A player#> addBoundaryTimeObserverForTimes:times queue:NULL usingBlock:^{ // Passing NULL for the queue specifies the main queue. NSString *timeDescription = (NSString *)CMTimeCopyDescription(NULL, [self.player currentTime]); NSLog(@"Passed a boundary at %@", timeDescription); [timeDescription release];}];