未使用iOS 8调用AVPlayerItemDidPlayToEndTimeNotification

时间:2014-10-22 16:16:10

标签: ios8 nsnotification avplayeritem

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

我有上述通知设置。当我使用iOS 7运行测试时,它被称为奇妙的,但是,我从未使用iOS 8运行我的应用程序来调用它。

2 个答案:

答案 0 :(得分:4)

通过将一个观察者注册到速率密钥路径来解决它。

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

答案 1 :(得分:3)

赞成的回答是不精确的。这就是我必须要做的事情:

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

作为参考,我正在加载一个远程URL音频文件,不确定这是否会对公差产生任何影响。