gpt4 book ai didi

iOS 9 : AVPlayerViewController lost controls after adding Observers

转载 作者:行者123 更新时间:2023-11-28 21:33:47 24 4
gpt4 key购买 nike

将观察者添加到 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: 时,控件又回来了,但我不会得到观察者。有任何想法吗?提前谢谢你。

最佳答案

可能为时已晚,但希望在遇到此问题时可以帮助其他人。我发现 AVPlayerViewController 有自己的观察者,会调用 -(void)observeValueForKeyPath:

解决方案是调用 super.observeValueForKeyPath: 我将在 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)
}
}

关于iOS 9 : AVPlayerViewController lost controls after adding Observers,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34723641/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com