OGeek|极客世界-中国程序员成长平台

标题: ios - AVPlayer 在缓冲区已满并重新开始时停止播放 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 13:37
标题: ios - AVPlayer 在缓冲区已满并重新开始时停止播放

我有一个 Collection View ,其中有一个 AVPlayer 在单元格内,并且 AVPlayer 开始循环播放 AVPlayerItem

-(void)scrollViewDidEndDeceleratingUIScrollView *)scrollView

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

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

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

- (void)observeValueForKeyPathNSString *)keyPath ofObjectid)object
                                    changeNSDictionary *)change contextvoid *)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?
                }

            }



Best Answer-推荐答案


我的解决方案是:

首先在 viewDidLoad 中为 AVPlayerItemPlaybackStalledNotification 添加观察或 ...

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

-(void)playerItemDidReachEndNSNotification*)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 key 补丁? 如果你得到 ,你可以在那里调用 play 方法。 请通知我结果

关于ios - AVPlayer 在缓冲区已满并重新开始时停止播放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34845057/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4