title: How to stop AVPlayer Ios and remove the periodicTimeObserverForInterval
body: <p>I'm having a little difficulty stopping AVPlayer.  </p>
  
  <p>I have a method that records and plays music simultaneously. I'm using AVPlayer to play the music because I want to use the addPeriodicTimeObserverForInterval Function.  I have it set up as follows:</p>
  
  <pre><code>    - (IBAction) recordVoice:(id)sender {
      if(!recorder.isRecording){
  
      //set up the file name to record to
      NSString *recordingLocation = [self createFileName];
      recordingName = recordingLocation;
      NSArray *pathComponents = [NSArray arrayWithObjects:[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject],
                                 recordingLocation, nil];
      NSURL *outputFileURL = [NSURL fileURLWithPathComponents:pathComponents];
      recordingURL = outputFileURL;
  
      // Setup audio session
      session = [AVAudioSession sharedInstance];
      [session setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:AVAudioSessionCategoryOptionDefaultToSpeaker
                     error:nil];
  
      // Define the recording settings to record as m4a
      NSMutableDictionary *recordSetting = [[NSMutableDictionary alloc] init];
      [recordSetting setValue:[NSNumber numberWithInt:kAudioFormatMPEG4AAC] forKey:AVFormatIDKey];
      [recordSetting setValue:[NSNumber numberWithFloat:44100.0] forKey:AVSampleRateKey];
      [recordSetting setValue:[NSNumber numberWithInt:2] forKey:AVNumberOfChannelsKey];
  
      // initiate and prepare the recorder
      recorder = [[AVAudioRecorder alloc] initWithURL:outputFileURL settings:recordSetting error:NULL];
      recorder.delegate = self;
      recorder.meteringEnabled = YES;
      [recorder prepareToRecord];
      [session setActive:YES error:nil];
      [recorder record];
  
  
      // find which song to play and initiate an AVPlayer to play it
      NSString *playerLocation = self.TitleLabel.text;
      NSString *path = [[NSBundle mainBundle] pathForResource:playerLocation ofType:@"m4a"];
          player = [[AVPlayer alloc] initWithURL:[NSURL fileURLWithPath:path]];
          lastTime = nil;
  
      //check where the player is at and update the song lines accordingly
      [player addPeriodicTimeObserverForInterval:CMTimeMake(3, 10) queue:NULL usingBlock:^(CMTime time){
              NSTimeInterval seconds = CMTimeGetSeconds(time);
              for (NSDictionary *item in robotR33) {
                  NSNumber *time = item[@"time"];
                  if ( seconds &gt; [time doubleValue] &amp;&amp; [time doubleValue] &gt;= [lastTime doubleValue] ) {
                      lastTime = @(seconds);
                      NSString *str = item[@"line"];
                      [self nextLine:str];
                  };
              }
          }];
          [player play];
  
      [_recordButton setImage:[UIImage imageNamed:@"micRecording.gif"] forState:UIControlStateNormal];
  
      }
      else{
          [recorder stop];
          player = nil;
          [session setActive:NO error:nil];
      }   
  }
  </code></pre>
  
  <p>If the recorder is not recording I set up both a new recorder AVAudioRecorder and an AVPlayer.  In the AVPlayer I set up an AddPeriodicTimeObserverForInterval which updates the UI based on the position of the player. </p>
  
  <p>If the recorder is recording I stop the recorder and I set the player to nil.  This stops the audio from playing but I notice that the addPeriodicTimeObserverInterval is still running because the UI continues to update.  Should I destroy the AVPlayer altogether and if so how should I do that?  Many thanks in advance.</p>
  
  <p>Also as an aside, I have a warning inside the addPeriodicTimeObserverForInterval block.  I am looping over an Array called robotR33.  Xcode tells me that 'Capturing self strongly in this block is likely to lead to a retain cycle".  Could this be part of my problem?</p>