Check play state of AVPlayer

112,974

Solution 1

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];
}];

Solution 2

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
    }
}

Solution 3

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.

Solution 4

rate is NOT the way to check whether a video is playing (it could stalled). From documentation of rate:

Indicates the desired rate of playback; 0.0 means "paused", 1.0 indicates a desire to play at the natural rate of the current item.

Key words "desire to play" - a rate of 1.0 does not mean the video is playing.

The solution since iOS 10.0 is to use AVPlayerTimeControlStatus which can be observed on AVPlayer timeControlStatus property.

The solution prior to iOS 10.0 (9.0, 8.0 etc.) is to roll your own solution. A rate of 0.0 means that the video is paused. When rate != 0.0 it means that the video is either playing or is stalled.

You can find out the difference by observing player time via: func addPeriodicTimeObserver(forInterval interval: CMTime, queue: DispatchQueue?, using block: @escaping (CMTime) -> Void) -> Any

The block returns the current player time in CMTime, so a comparison of lastTime (the time that was last received from the block) and currentTime (the time that the block just reported) will tell whether the player is playing or is stalled. For example, if lastTime == currentTime and rate != 0.0, then the player has stalled.

As noted by others, figuring out whether playback has finished is indicated by AVPlayerItemDidPlayToEndTimeNotification.

Solution 5

A more reliable alternative to NSNotification is to add yourself as observer to player's rate property.

[self.player addObserver:self
              forKeyPath:@"rate"
                 options:NSKeyValueObservingOptionNew
                 context:NULL];

Then check if the new value for observed rate is zero, which means that playback has stopped for some reason, like reaching the end or stalling because of empty buffer.

- (void)observeValueForKeyPath:(NSString *)keyPath
                      ofObject:(id)object
                        change:(NSDictionary<NSString *,id> *)change
                       context:(void *)context {
    if ([keyPath isEqualToString:@"rate"]) {
        float rate = [change[NSKeyValueChangeNewKey] floatValue];
        if (rate == 0.0) {
            // Playback stopped
        } else if (rate == 1.0) {
            // Normal playback
        } else if (rate == -1.0) {
            // Reverse playback
        }
    }
}

For rate == 0.0 case, to know what exactly caused the playback to stop, you can do the following checks:

if (self.player.error != nil) {
    // Playback failed
}
if (CMTimeGetSeconds(self.player.currentTime) >=
    CMTimeGetSeconds(self.player.currentItem.duration)) {
    // Playback reached end
} else if (!self.player.currentItem.playbackLikelyToKeepUp) {
    // Not ready to play, wait until enough data is loaded
}

And don't forget to make your player stop when it reaches the end:

self.player.actionAtItemEnd = AVPlayerActionAtItemEndPause;

Share:
112,974

Related videos on Youtube

Egil
Author by

Egil

Updated on July 08, 2022

Comments

  • Egil
    Egil over 1 year

    Is there a way to know whether an AVPlayer playback has stalled or reached the end?

  • Dermot
    Dermot over 10 years
    Not necessarily, it doesn't handle situations where the player stalls due an error in the file. Something I found out the hard way...
  • phi
    phi over 10 years
    As Dermot said, if you try to play something while in airplane mode, the AVPlayer rate is still set to 1.0, since it implies the intention to play.
  • openfrog
    openfrog about 10 years
    Is there a better solution?
  • James Campbell
    James Campbell almost 10 years
    The AVPlayer has an error property, just check that it isn't nil as well as checking the rate isn't 0 :)
  • quemeful
    quemeful almost 9 years
    AVPlayer != AVAudioPlayer
  • Dan Rosenstark
    Dan Rosenstark almost 9 years
    Caveats: all mentioned above in the answer that showed up two years before this one.
  • Aks
    Aks almost 9 years
    @Yar I know also upvoted above answer but this is for swift and in Swift '!player.error' was not working for me it is allowed only for bool types so I have added the answer oops upvoted your comment by mistake to give reply using this stack overflow app :)
  • Dan Rosenstark
    Dan Rosenstark almost 9 years
    My concern is that I don't know how well the player.error not being nil actually works.
  • jomafer
    jomafer over 8 years
    Note that the answer has been updated to prevent @Dermot scenario. So this one actually works.
  • Montana Burr
    Montana Burr over 8 years
    You could throw in a Boolean flag to check the play status.
  • Julian F. Weinert
    Julian F. Weinert over 8 years
    Will not be working when playing video footage reverse (- [AVPlayer setRate:-1.0]), because -1.0 is less than 0.
  • Julian F. Weinert
    Julian F. Weinert over 8 years
    Will not be working when playing video footage reverse (player.rate = -1.0), because -1.0 is less than 0.
  • maxkonovalov
    maxkonovalov almost 8 years
    Notifications might cause problems if you change player's item, for example, with -replaceCurrentItemWithPlayerItem:, and don't handle it properly. A more reliable way for me is to track AVPlayer's status using KVO. See my answer below for more details: stackoverflow.com/a/34321993/3160561
  • Hema Nandagopal
    Hema Nandagopal almost 8 years
    note that when using the above example with Swift, player.error is an optional not a bool, and so for this to compile, you'll need to change it to player.rate > 0 && (player.error == nil)
  • ToolmakerSteve
    ToolmakerSteve almost 8 years
    I think also need notification of failure to reach end - AVPlayerItemFailedToPlayToEndTimeNotification. If this error happens, will never reach the end time. Or reading maz's answer, add to your code a check for player.error != nil, in which case playback has "ended" due to error.
  • ToolmakerSteve
    ToolmakerSteve almost 8 years
    This answer does NOT distinguish between "stalled" and "ended"! I think combine this answer with maxkonovalov's answer. And note Julian's comment, the test should be player.rate != 0, so that playing in reverse isn't considered stopped.
  • ToolmakerSteve
    ToolmakerSteve almost 8 years
    BTW, what did you find "not reliable" about using NSNotification of AVPlayerItemDidPlayToEndTimeNotification?
  • ToolmakerSteve
    ToolmakerSteve almost 8 years
    Also need notification of error? AVPlayerItemFailedToPlayToEndTimeNotification
  • maxkonovalov
    maxkonovalov almost 8 years
    ToolmakerSteve, thanks for your note, added error check to my answer. For not reliable notifications - I came across the issue myself while implementing repeating and reusable player views in collection view, and KVO made things more clear, as it allowed to keep it all more local without different players' notifications interfering with each other.
  • oleynikd
    oleynikd about 7 years
    This will also not work after player.replaceCurrentItem(with: nil) when player is actually not playing, but player.rate will still be != 0
  • Alex
    Alex about 7 years
    I had the player stuck at rat 1.0 although I was not in airplane mode. So there some other condition that need to be included in the test.
  • technerd
    technerd over 6 years
    Easy to use for iOS 10+
  • Cesare
    Cesare over 6 years
    Hello Mr Stanev, thanks for your answer. This doesn't work for me (aka doesn't print anything in the console?)
  • Cesare
    Cesare over 6 years
    Nevermind, it does work – my player was nil! But I need to know when the view starts (not ends). Any way to do that?
  • Albert Renshaw
    Albert Renshaw about 6 years
    Very useful addition to my Macros.m, thank you. #define isAVPlayerPlaying(player) ((player.rate != 0) && (player.error == nil))
  • Sean Stayns
    Sean Stayns over 4 years
    @objc func didPlayToEnd() for Swift 4.2
  • MyNameIsCaleb
    MyNameIsCaleb about 4 years
    This provided answer may be correct, but it could benefit from an explanation. Code only answers are not considered "good" answers. Here are some guidelines for How do I write a good answer?. From review.
  • FontFamily
    FontFamily over 2 years
    This is not the right way to do this. The question wants to know when the player has reached the end. The user pausing playback will also activate this function, which will lead to unintended effects.