2

我有一个AVPlayer在单元格内部的集合视图,并且在循环播放时AVPlayer开始播放AVPlayerItem

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView

被调用。这很好用,但问题是在AVPlayer播放项目几次后,视频不再显示,但我能听到它的声音。@"playbackBufferFull"我还为每个播放的项目的值添加了一个观察者:

[item addObserver:self forKeyPath:@"playbackBufferFull" options:NSKeyValueObservingOptionNew context:nil];

我注意到,当视频停止时,值的观察者方法@"playbackBufferFull"被调用,首先我想知道是什么导致缓冲区变满,第二个也是最重要的是如何AVPlayer在视频停止时恢复;我尝试调用[cell.videoPlayer play];并用新的项目替换该项目,但它不起作用,观察者方法:

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object
                                    change:(NSDictionary *)change context:(void *)context {


                if ([object isKindOfClass:[AVPlayerItem class]] && [keyPath isEqualToString:@"playbackBufferFull"])
                {
                  //this method is get called when the video stop showing but i can still hear it
                 //how can i resume the video?
                }

            }
4

1 回答 1

1

我的解决方案是:

首先添加观察 for AVPlayerItemPlaybackStalledNotificationin viewDidLoador ...

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(playerItemDidReachEnd:)
                                             name:AVPlayerItemPlaybackStalledNotification
                                           object:self.avPlayer.currentItem];

-(void)playerItemDidReachEnd:(NSNotification*)noti

{
//thisisn't good way but i can't find the best way for detect best place for resume again.
NSLog(@"\n\n give Error while Streaminggggg");
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
    [self.avPlayer play];
 });
}

但是 也许您可以找到再次调用 play 方法进行恢复的最佳方法!请检查你有没有AVPlayerStatusReadyToPlay密钥补丁?如果你得到,你可以play在那里调用方法。请通知我结果

于 2016-01-18T07:09:19.537 回答