AVPlayerItemDidPlayToEndTimeNotification not being called since iOS 8
AVPlayerItem *currentItem = self.player.currentItem;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playerItemDidReachEnd:) name:AVPlayerItemDidPlayToEndTimeNotification object:currentItem];
I have the above notification setting. It's called fantastic when I run tests with iOS 7, however it is never called when my app is running with iOS 8.
+3
source to share
2 answers
This is resolved by registering an observer on the baud rate path.
[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
}
}
+4
source to share
The selected answer is inaccurate. 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 am downloading the remote url of the audio file, not sure if this makes any difference regarding tolerances.
+3
source to share