0

添加观察者后,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:控件时都会回来,但是我不会得到观察者。有任何想法吗?先感谢您。

4

2 回答 2

2

可能为时已晚,但希望可以在其他人遇到此问题时对其有所帮助。我发现 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)
    }
}
于 2016-04-16T06:09:48.807 回答
0

在您的代码中,您没有将 self.player 添加到任何视图中。这就是为什么它没有在视图上显示玩家。试试这个示例:

-(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
        }
    }
}
于 2016-01-11T14:59:42.013 回答