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

我有上述通知设置。当我用 iOS 7 运行测试时,它被调用得非常好,但是,我用 iOS 8 运行我的应用程序时,它从来没有被调用过。

4

2 回答 2

3

它是通过将观察者注册到速率键路径来解决的。

[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
    }
}
于 2014-12-30T14:15:01.357 回答
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 音频文件,不确定这是否对容差有任何影响。

于 2015-03-26T12:04:25.550 回答