title: How to stop AVPlayer playback in cell when the cell has moved offscreen
body: <p><strong>Scenario</strong>: I have a <code>tableView</code> with many cells. Each cell will contain an AVPlayer to play a video. The AVPlayer is being created once the user presses a play button on the cell. I want the AVPlayer to stop playing it's video and be <strong>completely</strong> removed upon it's cell moving offscreen.</p>
  
  <p><strong>The Issue</strong>: When the cell moves offscreen the media will still play. So when I attempt to remove the player like I need, my app crashes with an error</p>
  
  <blockquote>
    <p>Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'An instance of AVPlayer cannot remove a time observer that was added by a different instance of AVPlayer.'</p>
  </blockquote>
  
  <hr>
  
  <p><strong>How the player is being created</strong>:</p>
  
  <p>(in the cell)</p>
  
  <pre><code>-(void)addPlayer {
  
      if (!self.player) {
  
          // This is my custom init method
          self.player = [[AVPlayer alloc] initWithFrame:self.container.bounds contentURL:mediaURL];
          [self.player setUserInteractionEnabled:YES];
          [self.container setUserInteractionEnabled:YES];
          [self.container addSubview:self.player];
      }
  
  }
  </code></pre>
  
  <hr>
  
  <p><strong>How the addTimeObserver is being added</strong>:</p>
  
  <pre><code>-(void)beginObservingTime {
  
  // This will monitor the current time of the player
  
      __weak STPlayer *weakSelf = self;
      self.observerToken = [self.player addPeriodicTimeObserverForInterval:CMTimeMakeWithSeconds(1.0, NSEC_PER_SEC) queue:nil usingBlock:^ (CMTime time) {
          if (CMTimeGetSeconds(time) &gt; self.playbackTime) {
              // done buffering
              [weakSelf updatePlaybackTime:CMTimeGetSeconds(time)];
              [weakSelf.player removeTimeObserver:weakSelf.observerToken];
              [weakSelf hideActivityIndicator];
          }
      }];
  }
  </code></pre>
  
  <hr>
  
  <p><strong>How the player is being removed</strong>:</p>
  
  <p>(in the UITableViewController)</p>
  
  <pre><code>-(void)tableView:(UITableView *)tableView didEndDisplayingCell:(FeedCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
  
      [cell breakDownPlayer];
  }
  </code></pre>
  
  <p>(in the cell)</p>
  
  <pre><code>-(void)breakDownPlayer {
  
      [self.player breakDown];
  }
  </code></pre>
  
  <p>(in player subclass)</p>
  
  <pre><code>-(void)breakDown {
  
      [self.player removeTimeObserver:self.observerToken];
  
      [[NSNotificationCenter defaultCenter] removeObserver:self name:AVPlayerItemDidPlayToEndTimeNotification object:self.playerItem];
  }
  </code></pre>
  
  <p><strong>Question</strong>: How do I remove the player from a <code>UITableViewCell</code> once <code>didEndDisplayingCell</code> has been called and the app not crash? </p>