Question

I have an application that displays movies. The first screen shows buttons that present different modal views. Inside those modal views are buttons that play videos.

I want the main menu to play music (easy enough), but I want it to persist through the second modal view but stop when the video starts playing.

I've trying [audioPlayer stop] when the buttons for the movies are pressed, but the music keeps playing.

I have tried stopping and playing the audio in viewDidAppear and viewDidDisappear. That has made the audio restart upon presenting the second view and not playing during the video.

I hope this makes sense, I really just need an idea of how to get the audio to play all of the time (throughout two or three modal views) except for during movie playback.

Here's my vewDidAppear method:

-(void)viewDidAppear:(BOOL)animated {
   NSString *musicPath = [[NSBundle mainBundle] pathForResource: @"Intro Music" ofType:@"mp3"]; 
   if(musicPath) {
     NSURL *musicURL = [NSURL fileURLWithPath:musicPath];
     audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:musicURL error:nil];
     [musicURL release]; 
   }
   if(!audioPlayer.playing) {
     [audioPlayer play]
   }
}
Was it helpful?

Solution

Your modal view controller is trying to stop an audioPlayer which does not exists inside of it. The mainMenueViewController has the audioPlayer, so it has to be the one to tell the audio player to stop.

You can use NSNotifications for this. Each NSNotification is sent through the singleton NSNotificationCenter. Here are the basic steps for your situation:

  1. Your main menu registers for notifications when it is ready.
  2. Your main menu has methods to call when the notifications fire.
  3. Your submenu fires a method when it needs to turn off the audio.
  4. Your main menu unregisters when it's ready to close.

Hope this helps!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top