title: iOS Stop Audio Playback At Certain Time (AVAudioPlayer End Time)
body: <p>I'm wanting to play just part of an audio file. This audio file contains 232 spoken words, I have a dictionary that contains the start time of each word. So I have the start and stop times I just can't find a way to stop at a given time or play the file for a certain duration. Any advice and/or sample code to help me would be much appreciated, thanks Jason. </p>
  
  <p>So I've found a solution, there's a problem with how I get endTime but I sure I can fix it.</p>
  
  <pre><code>//Prepare to play
  [audioPlayer prepareToPlay];
  
  //Get current time from the array using selected word
  audioPlayer.currentTime = [[wordsDict objectForKey:selectedWord] floatValue]; 
  
  //Find end time  - this is a little messy for now
  int currentIndex = 0;
  int count = 0;
  
  for (NSString* item in wordsKeys) {
      if ([item isEqualToString: selectedWord]) {
          currentIndex = count+1;
      }
      count++;
  }
  
  //Store found end time
  endTime  = [[wordsDict objectForKey:[wordsKeys objectAtIndex:currentIndex]] floatValue];
  
  //Start Timer
  NSTimer * myAudioTimer = [NSTimer scheduledTimerWithTimeInterval:0.1
                                                               target:self
                                                             selector:@selector(checkCurrentTime)
                                                             userInfo:nil
                                                              repeats:YES]
  //Now play audio
  [audioPlayer play];
  
  
  
  //Stop at endTime
  - (void) checkCurrentTime {
  if(audioPlayer.playing &amp;&amp; audioPlayer.currentTime &gt;= endTime)
      [audioPlayer stop];
  }
  </code></pre>