0

I tried this code but didn't work,

-(void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];

NSString *soundPath = [[NSBundle mainBundle] pathForResource:@"paying with coins and bills" ofType:@"wav"];
AudioServicesCreateSystemSoundID(CFBridgingRetain([NSURL fileURLWithPath:soundPath]), &(soundID));
AudioServicesPlaySystemSound (soundID);

}

It will play only when i runs the xcode project.

My requirement is, in my ipad whenever open my app i need to play a sound. Please help me

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Shankar
  • 51
  • 1
  • 8

1 Answers1

0

You may try to move your code to AppDelegate.m

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

this method will be called when your app become active, which may fit in your case.

Jing
  • 536
  • 3
  • 12
  • several ways. You may create a singleton class to manage your sound play. whenever you want to play or stop the sound, just let your singleton to do it. Or you can move you sound code to you ViewController, and create a Bool and use KVO to observe this property. and in your applicationDidBecomeActive change this property. And when you move to another viewController from the current viewController, just also change the property – Jing Oct 24 '13 at 03:52
  • can you explain with code for this. Because i'm new to objective c – Shankar Nov 19 '13 at 04:24
  • a singleton may be easier and clearer in this case. First, create a singleton (http://stackoverflow.com/questions/5720029/create-singleton-using-gcds-dispatch-once-in-objective-c). Second, move your play and stop code to this singleton. Third, whenever you want to control the sound play, just call the singleton instance to do the operation. – Jing Nov 22 '13 at 03:52