After adding observer to AVPlayer
I suddenly lost the AVPlayerViewController
controls.
-(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
}
}
}
Every time when I remove - (void)observeValueForKeyPath:
the controls are back, but then I won't get observer. Any ideas? Thank you in advance.
It might be too late but hopefully can help others when they came to this issue. I found that AVPlayerViewController has their own observer(s) that will call -(void)observeValueForKeyPath:
The solution is call the super.observeValueForKeyPath:
I will show the code in Swift:
override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
if keyPath == "rate" {
//your code here
} else {
super.observeValueForKeyPath(keyPath, ofObject: object, change: change, context: context)
}
}
In your code you did not add the self.player to any view. That why it is not showing the player on the view. Try this sample:
-(void)viewDidAppear:(BOOL)animated {
AVAsset *asset = [AVAsset assetWithURL: [NSURL URLWithString: "Your video URL here"]];
AVPlayerItem *item = [[AVPlayerItem alloc] initWithAsset:asset];
AVPlayer *player = [[AVPlayer alloc] initWithPlayerItem:item];
player.actionAtItemEnd = AVPlayerActionAtItemEndNone;
AVPlayerLayer *layer = [AVPlayerLayer playerLayerWithPlayer:player];
UIView *containerView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0, 320.0f, 200.0f)];
layer.frame = self.view.frame;
[containerView.layer addSublayer:layer];
[self.view addSubview:containerView];
layer.backgroundColor = [UIColor greenColor].CGColor;
[layer setVideoGravity:AVLayerVideoGravityResizeAspectFill];
[player play];
}
- (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
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.