Я заметил, что 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 запускается и записывается в журнал.
Есть ли какая-либо причина, почему я должен ожидать, что это произойдет?И если да, то как я могу отличить событие окончания воспроизведения от невозможности продолжить воспроизведение из-за отсутствия буферизованного контента?