我注意到,当网络停用并且视频无法继续播放时,偶尔会发送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并将其写入日志。
我有什么理由可以期望这种情况发生吗?如果是这样,我该如何区分播放结束事件和由于缺少缓冲内容而导致继续播放失败?
答案 0 :(得分:0)
我从AVPlayer的通知中发现了相同的问题。
触发AVPlayerItemFailedToPlayToEndTime
后,我从头开始检查AVPlayerItem
的位置。
Code sample来自ModernAVPlayer
库: