2

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

            }
aviv_elk
  • 416
  • 1
  • 9
  • 21
  • i had this problem also! i can't find the better way for it , but in dispatch_after (w8 for buffer ready again ) --> i call avplayer play method again and it cause resume :) – Mo Farhand Jan 18 '16 at 06:51
  • hi, can you please send me the full code of the dispatch_after? when is the buffer ready again? – aviv_elk Jan 18 '16 at 07:03
  • @aviv_elk did you managed to play the video as you described? I am trying to play the video with AVPlayer and AVPlayerLayer in TableViewCell. I am also facing the issue of no playing a video after a while. – Bhavin Kansagara Jul 06 '17 at 12:17

1 Answers1

1

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

Mo Farhand
  • 1,144
  • 8
  • 21
  • this is the available way to detect and replay the AVPlayer, but the problem arises while playing it in cell in continuous loop to produce the GIF like feel with MP4 Video. have you tried something similar? – Bhavin Kansagara Jul 06 '17 at 12:21