I have an app that when it launches it plays an intro clip. The following code is in the - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions within the appDelegate.m which works just splendid.
NSLog(@"PLAY SOUND CLIP WHILE LOADING APP");
NSURL *clip = [[NSBundle mainBundle] URLForResource: @"intro" withExtension:@"caf"];
self.startupPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:clip error:NULL];
[self.startupPlayer play];
If the user changes view before the intro sound finishes it still continues to play. I have placed this code outside of - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions thinking it would help but no.
-(void)viewDidDisappear:(BOOL)animated {
[super viewDidDisappear:animated];
// Stop Sound
[self.startupPlayer stop];
}
I thought maybe if I put an if statement right after the load request that might do the trick but it has not worked. See example:
NSLog(@"PLAY SOUND CLIP WHILE LOADING APP");
NSURL *clip = [[NSBundle mainBundle] URLForResource: @"intro" withExtension:@"caf"];
self.startupPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:clip error:NULL];
[self.startupPlayer play];
if ([viewDidDisappear == YES]) {
[self.startupPlayer stop];
}
Any suggestions that would stop the intro sound if the user changes view before that clip is done playing would be great. Oh and I’ve used the “viewWillDisappear” in other parts of the app with success, but in this case I choose “viewDidDisappear” because the later didn’t work either. So I’m stumped. Thanks in advance.
EDIT: So I moved the viewWillDisappear into my MainViewController and called the delegate but I’m still not having any luck. Again, help would be appreciated.
I solved this issue by taking the @property declaration for *startupPlayer and placing it in the AppDelegate.h file instead of how it was below;
In the AppDelegate.m
Then I still @synthesized it in the .m file as shown below and kept the didFinishLaunchingWithOptions the same:
Then in my MainViewController.m
Now even if the intro music is still playing through one cycle, the user can switch to another view and stop that music.