ios 知道 hls 直播何时播放完最后一个块
ios Knowing when a hls livestream has played the last chunk
我对流没有问题,但我不知道它何时缓冲或流何时结束。无论如何在 Objective-C 中确定这一点。我找到了音频解决方案,甚至尝试了 AVPlayerItemDidPlayToEndTimeNotification,但它不起作用。有什么建议么?
NSString *url = liveStream.stream[@"cdn"];
dispatch_async(dispatch_get_main_queue(), ^{
AVPlayerItem *playerItem = [[AVPlayerItem alloc]
initWithURL:[NSURL URLWithString:url]];
[_player replaceCurrentItemWithPlayerItem:playerItem];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(itemDidFinishPlaying:) name:AVPlayerItemDidPlayToEndTimeNotification object:playerItem];
[_player play];
});
}
-(void)itemDidFinishPlaying:(NSNotification *) notification {
}
除了你正在使用的通知之外,你应该使用KVO来观察avPlayer的速率,以及avplayer当前AVPlayer项目的状态。通过观察这些属性,您可以制作一个状态机,您的播放器视图控制器可以在其中知道发生了什么,并且可以从状态的各种变化中恢复。根据您观察到的属性,您必须准备各种玩家状态并从中恢复。
Here's an example of a complete AVPLayer
And of course, here's apple's documentation on AVPlayer
Here's the documentation on AVPlayerItem
Lastly, here's the link on KVOController. You'll thank me for this later.
我对流没有问题,但我不知道它何时缓冲或流何时结束。无论如何在 Objective-C 中确定这一点。我找到了音频解决方案,甚至尝试了 AVPlayerItemDidPlayToEndTimeNotification,但它不起作用。有什么建议么?
NSString *url = liveStream.stream[@"cdn"];
dispatch_async(dispatch_get_main_queue(), ^{
AVPlayerItem *playerItem = [[AVPlayerItem alloc]
initWithURL:[NSURL URLWithString:url]];
[_player replaceCurrentItemWithPlayerItem:playerItem];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(itemDidFinishPlaying:) name:AVPlayerItemDidPlayToEndTimeNotification object:playerItem];
[_player play];
});
}
-(void)itemDidFinishPlaying:(NSNotification *) notification {
}
除了你正在使用的通知之外,你应该使用KVO来观察avPlayer的速率,以及avplayer当前AVPlayer项目的状态。通过观察这些属性,您可以制作一个状态机,您的播放器视图控制器可以在其中知道发生了什么,并且可以从状态的各种变化中恢复。根据您观察到的属性,您必须准备各种玩家状态并从中恢复。
Here's an example of a complete AVPLayer
And of course, here's apple's documentation on AVPlayer
Here's the documentation on AVPlayerItem
Lastly, here's the link on KVOController. You'll thank me for this later.