Can't stop AVPlayer streaming

796 Views Asked by At

I have a UITableview with an AVPlayer instance in every cell. My requirement is to stream video only in the visible cells hence I don't want any player instances in the invisible cells. Play method is called in cellForRowAtIndexPath. Here is my code to remove AVPlayer

- (void)tableView:(UITableView *)tableView didEndDisplayingCell: (UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath*)indexPath
 {
    if (![ _feedsTableView.indexPathsForVisibleRows containsObject:indexPath]) {
        NSLog(@"index of visible cell  %ld",(long)indexPath.row);
        LTHomeFeedCell * cell = (LTHomeFeedCell*)[_feedsTableView cellForRowAtIndexPath:indexPath ];
        [ cell.avMoviePlayer pause ];
        cell.avMoviePlayer = [ AVPlayer playerWithURL:[NSURL URLWithString:@""] ];
        cell.avMoviePlayer = nil;
  }

But, still I hear audio from invisible cells. I'm using this same code to pause the video when I move to a different screen but I still hear the audio. How do I fix this?

1

There are 1 best solutions below

1
On

You can handle the pause function through local notifications also like this:

  • Add an observer for a local notification in the cell when you start playing the video.
  • And in the selector of that notification, write the code to pause you player and remove the notification
  • After that in didEndDisplayingCell, post that notification to pause that video.