AVPlayer播放,暂停和缓冲问题

我的应用程序播放streamvideo,但是当它缓冲时,播放器进入暂停模式,我必须手动将其设置为播放模式,为了处理这种情况,我在我的AVPlayer类中有以下代码,但它确实不行。

在ViewDidLoad方法中

[playerItem addObserver:self forKeyPath:@"playbackBufferEmpty" options:NSKeyValueObservingOptionNew context:nil]; [playerItem addObserver:self forKeyPath:@"playbackLikelyToKeepUp" options:NSKeyValueObservingOptionNew context:nil]; 

然后,使用以下方法处理观察者

 - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { if (!player) { return; } else if (object == playerItem && [keyPath isEqualToString:@"playbackBufferEmpty"]) { if (playerItem.playbackBufferEmpty) { //Your code here } } else if (object == playerItem && [keyPath isEqualToString:@"playbackLikelyToKeepUp"]) { if (playerItem.playbackLikelyToKeepUp) { //Your code here } } 

}

有没有另一个解决这个问题的方法来让玩家继续玩游戏模式?

这可能有帮助,

假设这是你的AVPlayer对象
player1 = [AVPlayer playerWithURL:streamURL];

当你的video进入缓冲模式,那么你可以暂停它,当它完成后再次播放,如:在观察者的方法,

 if ([object isKindOfClass:[AVPlayerItem class]]) { AVPlayerItem *item = (AVPlayerItem *)object; //playerItem status value changed? if ([keyPath isEqualToString:@"status"]) { //yes->check it... NSLog(@"STATUS = %d",item.status); switch(item.status) { case AVPlayerItemStatusFailed: NSLog(@"player item status failed"); break; case AVPlayerItemStatusReadyToPlay: [playButton setTitle:@"Pause" forState:UIControlStateNormal]; [player1 play]; NSLog(@"player item status is ready to play"); break; case AVPlayerItemStatusUnknown: NSLog(@"player item status is unknown"); break; } } else if ([keyPath isEqualToString:@"playbackBufferEmpty"]) { if (item.playbackBufferEmpty) { [playButton setTitle:@"Play" forState:UIControlStateNormal]; [player1 pause]; NSLog(@"player item playback buffer is empty"); } } } 

或者你可以保持button点击事件。 在屏幕上放置一个button以保持播放和暂停,并使用OnClick事件向其添加目标。