我没有流的问题,但我不知道它何时缓冲或流何时结束。无论如何,在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 {
}
答案 0 :(得分:1)
除了您正在使用的通知之外,您还应该使用KVO来观察avPlayer的速率,以及avplayer当前AVPlayer项目的状态。通过观察这些属性,您可以创建一个各种状态机,您的播放器视图控制器可以知道发生了什么,并且可以从各种更改中恢复。根据您观察到的属性,您必须准备和恢复各种播放器状态。
Here's an answer on how to check for buffering
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.