rcat24
rcat24

Reputation: 538

AVPlayerItemDidPlayToEndTimeNotification not being called with iOS 8

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.

Upvotes: 4

Views: 2406

Answers (2)

bizz84
bizz84

Reputation: 2272

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.

Upvotes: 3

rcat24
rcat24

Reputation: 538

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

Upvotes: 3

Related Questions