I am trying to play music in my application. The music works fine but after switching viewControllers and returning to the main menu, my music plays again! It means several
First, viewDidLoad probably isn't a good place to be initializing your AVAudioPlayer, as the system may need to unload some of your components to reclaim memory (in viewDidUnload). You should probably be creating the AVAudioPlayer in your init method or similar.
So what's happening is you end up creating a second AVAudioPlayer when the focus comes back to your application, and you're actually losing the reference to your first one. (Thus, also leaking memory.)
If you want to start playing music when the view loads, you should additionally check its status before doing so:
- (void)viewDidLoad {
...
if (!myMusic.playing) {
[myMusic play];
}
...
}