简体   繁体   中英

Can't stop AVPlayer streaming

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM