添加观察者后,AVPlayer
我突然失去了AVPlayerViewController
控制。
-(void)viewDidAppear:(BOOL)animated {
AVPlayerItem *playerItem = [[AVPlayerItem alloc]initWithURL:_playbackInfo.streamURL];
// logo to show
CGRect viewRect = CGRectMake(10, 10, 30, 30);
UIView *overlayView = [[UIView alloc] initWithFrame:viewRect];
UIImageView *image = [[UIImageView alloc]initWithFrame:viewRect];
image.image = _playbackInfo.logoType;
[overlayView addSubview:image];
self.player = [[AVPlayer alloc]initWithPlayerItem:playerItem];
[self.contentOverlayView addSubview:overlayView];
self.player.actionAtItemEnd = AVPlayerActionAtItemEndNone;
[self.player addObserver:self
forKeyPath:@"rate"
options:NSKeyValueObservingOptionNew
context:NULL];
[self.player play];
[self.navigationController setNavigationBarHidden:YES animated:NO];
}
- (void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary<NSString *,id> *)change
context:(void *)context {
if ([keyPath isEqualToString:@"rate"]) {
float rate = [change[NSKeyValueChangeNewKey] floatValue];
if (rate == 0.0) {
_sessionStarted = NO;
NSLog(@"Playback stopped");
} else if (rate == 1.0) {
if(!_sessionStarted) {
LTSessionManager *sessionManager = [LTSessionManager sharedInstance];
sessionManager.delegate = self;
[sessionManager startSessionManager:self.playbackInfo];
[sessionManager getSessionStatus];
NSLog(@"Start session");
_sessionStarted = YES;
}
} else if (rate == -1.0) {
// Reverse playback
}
}
}
每次我删除- (void)observeValueForKeyPath:
控件时都会回来,但是我不会得到观察者。有任何想法吗?先感谢您。