aviv_elk
aviv_elk

Reputation: 436

AVPlayer stops playing when buffer is full and dosent resume

I have a collection view with an AVPlayer inside the cell and the AVPlayer starts playing the AVPlayerItem in a loop when

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView

gets called. this works well but the problem is that after the AVPlayer is playing the item a few times the video is no longer shown but i can hear its sound. I also add an observer for the value @"playbackBufferFull" for each item that is played like that:

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

i noticed that when the video stops the observer method of the value @"playbackBufferFull" gets called, first of all i would like to know what causes the buffer the get full, the second and most important is how can i resume the AVPlayer when the video stops; i tried calling [cell.videoPlayer play]; and to replace the item with a new one and it didnt work, the observer method:

- (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?
                }

            }

Upvotes: 2

Views: 1838

Answers (1)

MoLowKey
MoLowKey

Reputation: 1144

my solution is :

first add observe for AVPlayerItemPlaybackStalledNotification in viewDidLoad or ...

[[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];
 });
}

BUT maybe you can find the best way to call play method again for resume! please check do you get AVPlayerStatusReadyToPlay keypatch ? if you get , you can call play method there. please notify me about the result

Upvotes: 1

Related Questions