4
AVPlayerItem *currentItem = self.player.currentItem;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playerItemDidReachEnd:) name:AVPlayerItemDidPlayToEndTimeNotification object:currentItem];

I have the above notification setup. It is being called fantastically when I run tests with iOS 7, however, it's never being called with I run my application with iOS 8.

rcat24
  • 548
  • 2
  • 23

2 Answers2

3

It is resolved by registering an observer to the rate keypath.

[self.player addObserver:self forKeyPath:@"rate" options:0 context:nil];

- (void)observeValueForKeyPath:(NSString*)keyPath ofObject:(id)object change:(NSDictionary*)change context:(void*)context {
if (self.player.rate == 0.0) {
    CMTime time = self.player.currentTime;
    if (time >= duration) {
        //song reached end
    }
}
rcat24
  • 548
  • 2
  • 23
3

The upvoted answer is imprecise. This is what I had to do:

if ([keyPath isEqualToString:@"rate"]) {

    if (_player.rate == 0.0) {
        CMTime time = _player.currentTime;
        NSTimeInterval timeSeconds = CMTimeGetSeconds(time);
        CMTime duration = _player.currentItem.asset.duration;
        NSTimeInterval durationSeconds = CMTimeGetSeconds(duration);
        if (timeSeconds >= durationSeconds - 1.0) { // 1 sec epsilon for comparison
            [_delegate playerDidReachEnd:_player];
        }
    }
}

For reference, I'm loading a remote URL audio file, not sure if this makes any differences with regards to tolerances.

bizz84
  • 1,964
  • 21
  • 34