Frage

Hi I'm playing the audio file in one view controller when i going the another view controller its still playing the audio how to pause the audio when we going back to another view controller.

   NSString *audioPath = [[NSBundle mainBundle] pathForResource:@"audio" ofType:@"mp3" ];
   NSURL *audioURL = [NSURL fileURLWithPath:audioPath];
   NSError *error;
   self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:audioURL error:&error];
  [self.audioPlayer play];

Please tell where i have to put the pause code for the audio when I'm going for another view controller.

Thanks.

War es hilfreich?

Lösung

in your viewController dealloc method add code to stop your audioPlayer

 - (void)dealloc {
    if ([self.audioPlayer isPlaying])
       [self.audioPlayer stop];

     [super dealloc]; // call if you are not using ARC
 }

You can also put the stop audio code in ViewWillDisappear method if you are going deeper into heirarchy

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top