2

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]
   }
}
tshepang
  • 12,111
  • 21
  • 91
  • 136
Link
  • 396
  • 1
  • 4
  • 15
  • Welcome to StackOverflow! Remember to stay on top of your questions and select the correct answer. – Moshe Mar 23 '11 at 14:23
  • Would you please post some code? I think it might where are you telling the audio player to stop? Is it in the same class that created it? – Moshe Mar 23 '11 at 14:24
  • Can I see your header file too, please? – Moshe Mar 23 '11 at 15:20

1 Answers1

1

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!

Moshe
  • 57,511
  • 78
  • 272
  • 425
  • Yikes, I'm really sorry about the formatting. I'm not quite sure how to fix that. – Link Mar 23 '11 at 14:33
  • @Brett - No worries. Why don't you edit your question to add the code? Most folks do when they need to add code or clarify. Comments aren't ideal for code. To get that formatting though, use ` instead of '. – Moshe Mar 23 '11 at 14:37
  • I've added that code for you @Brett. Actuially, I'm curious to see what your view lifecycle methods look like. – Moshe Mar 23 '11 at 14:39
  • Thanks, I'm not really sure how to answer that question. I have a view controller that presents modal views on top of the main menu when buttons are pressed. The videos are presented as a modal view as well. It's like a chain of modal views. Does that answer the question? – Link Mar 23 '11 at 14:43
  • View lifecycle methods are viewDidLoad, viewWillAppear, viewDidAppear, viewWillDisappear, viewWillUnload etc. – Moshe Mar 23 '11 at 15:19
  • I haven't implemented those to do anything. I tried putting [audioPlayer stop] in viewDidDisappear, but it just restarted the song once the next menu was shown. – Link Mar 23 '11 at 15:22
  • Ah, ok. Have you tried putting `@property (nonatomic, retain) AVAudioPlayer audioPlayer;` in your header file? I think that it's a referencing issue, but I can only guess unless I see more code. – Moshe Mar 23 '11 at 15:24
  • I did try that earlier when I was trying to figure it out. I just put it back in and I'm getting the same behavior. I like how it plays through the modal views, I just want it to stop when I push the buttons to play the videos. I have [audioPlayer stop] in the IBAction methods for the buttons that play the movies. It seems like it's just being ignored. – Link Mar 23 '11 at 15:33
  • Use NSLog to make sure those methods are being called. If they are, I have one other possible idea. – Moshe Mar 23 '11 at 15:40
  • The methods that play the movie or the lifecycle methods? – Link Mar 23 '11 at 15:44
  • The methods that play the movie. – Moshe Mar 23 '11 at 15:45
  • Well they are being called since the movies play. I put a breakpoint on the [audioPlayer stop] line and it is executed it just doesn't do anything. – Link Mar 23 '11 at 15:54
  • Ok. Makes sense. Your modal view doesn't contain an audioPlayer. You should send a notification to the menu view using NSNotificationCenter. – Moshe Mar 23 '11 at 16:30
  • Even though I am calling [self presentModalViewController:moviePlayer animated:YES]? I think I am confused on where to send the notifications since all of the IBAction methods are in the same class. – Link Mar 23 '11 at 16:45
  • @Brett - Please post your custom methods and what classes they're in. I would like to see the main menu view controller and the modal view. (By the way, I'm on my computer now - I was on my iPhone before. Wanna hop over to the MSO Tavern? http://chat.meta.stackoverflow.com/rooms/89/the-tavern-general ) – Moshe Mar 23 '11 at 16:47