Search code examples
iosxcodeviewdidload

Playing Sound at view change


Im a newb. I have a audio sound playing as soon as the new view controller loads under viewdidload but now how would I drop that from memory to make it so if you go back to that view controller it would still play the audio again?

@implementation FFViewController

-(void)viewDidAppear:(BOOL)animated

{
    CFBundleRef mainBundle = CFBundleGetMainBundle();
    CFURLRef soundFileURLRef;
    soundFileURLRef = CFBundleCopyResourceURL(mainBundle, (CFStringRef) @"crash",
                                              CFSTR ("wav"), NULL);
    UInt32 soundID;
    AudioServicesCreateSystemSoundID (soundFileURLRef, &soundID);
    AudioServicesPlaySystemSound (soundID);
}


- (void)viewDidLoad 

Solution

  • Use viewWillAppear: as it is called every time your is appeared. Also check documentation for more methods that are called.
    Responding to View Events