Search code examples
ios8nsnotificationavplayeritem

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.


Solution

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