如果我启动此流然后关闭 wifi,则会触发“playbackBufferEmpty”,我可以通过尝试重新启动流来响应。但是,如果我立即关闭 wifi 并重新打开,则流将停止播放,但不会触发任何观察者。
- (void)restartStream
{
[self.activityIndicator startAnimating];
NSLog(@"Removing Observers");
[self.radioStream removeObserver:self forKeyPath:@"status"];
[self.radioStream removeObserver:self forKeyPath:@"timedMetadata"];
[self.radioStream removeObserver:self forKeyPath:@"playbackBufferEmpty"];
[self.radioStream removeObserver:self forKeyPath:@"playbackLikelyToKeepUp"];
NSLog(@"Creating New AVPlayerItem");
self.radioStream = [AVPlayerItem playerItemWithURL:[NSURL URLWithString:@"http://mystream.com"]];
[self.radioStream addObserver:self forKeyPath:@"status" options:0 context:nil];
[self.radioStream addObserver:self forKeyPath:@"timedMetadata" options:NSKeyValueObservingOptionNew context:nil];
[self.radioStream addObserver:self forKeyPath:@"playbackBufferEmpty" options:NSKeyValueObservingOptionNew context:nil];
[self.radioStream addObserver:self forKeyPath:@"playbackLikelyToKeepUp" options:NSKeyValueObservingOptionNew context:nil];
NSLog(@"Playing radioPlayer with new AVPlayerItem");
self.radioPlayer = [AVPlayer playerWithPlayerItem:self.radioStream];
}