缓冲区外时触发 AVPlayerItemDidPlayToEndTimeNotification

AVPlayerItemDidPlayToEndTimeNotification firing when out of buffer

我注意到AVPlayerItemDidPlayToEndTimeNotification 偶尔会在网络停用且视频无法继续播放时发送。尽管视频尚未完成。

可以使用以下代码重现:

#import "ViewController.h"  
#import   

@interface ViewController ()  
@property AVPlayerItem *item;  
@property AVPlayer *player;  

@end  

@implementation ViewController  

- (void)viewDidLoad {  
    [super viewDidLoad];  

    NSURL *url = [NSURL URLWithString:@"https://video-dev.github.io/streams/x36xhzz/x36xhzz.m3u8"];  
    self.item = [[AVPlayerItem alloc] initWithURL: url];  
    self.player = [[AVPlayer alloc] initWithPlayerItem:self.item];  


    AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:self.player];  
    playerLayer.frame = self.view.bounds;  

    [NSNotificationCenter.defaultCenter addObserver:self selector:@selector(handleNotification:) name:AVPlayerItemDidPlayToEndTimeNotification object:self.item];  
    [self.view.layer addSublayer:playerLayer];  

    [self.player addObserver:self forKeyPath:@"status" options:0 context:nil];  

    [[AVAudioSession sharedInstance]  
     setCategory: AVAudioSessionCategoryPlayback  
     error: nil];  

    [self.player play];  

    NSLog(@"Playing");  
}  

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object  
                        change:(NSDictionary *)change context:(void *)context {  
    if (object == self.player && [keyPath isEqualToString:@"status"]) {  

    }  
}  

-(void)handleNotification:(NSNotification*)notification {  
    NSLog(@"%@", notification.name.debugDescription);  

    NSLog(@"HandleNotification called at:%lld seconds. Duration on player is:%lld seconds", self.player.currentTime.value/self.player.currentTime.timescale, self.item.duration.value/self.item.duration.timescale);  
}  


@end  

复制步骤为: 1. 运行 应用程序,让视频播放大约 40 秒。 2.终止设备上的网络。 AVPlayerItemDidPlayToEndTimeNotification 被触发并写入日志。

有什么理由让我认为这会发生吗?如果是这样,我如何区分播放结束事件和由于缺少缓冲内容而无法继续播放?

我从 AVPlayer 通知中发现了同样的问题。 当AVPlayerItemFailedToPlayToEndTime被触发时,我从最后检查AVPlayerItem的位置。

Code sample 来自 ModernAVPlayer 图书馆: