I'm facing a small issue with playing sounds upon loading of the view (and ultimately upon other actions that are performed if this code works)
The Problem: The sound does not play on the phone when the view is loaded. HOWEVER, I can hear the sound from my iphone if I step through the code after adding a breakpoint on:
[click play]
I have obtained the following code from an online tutorial and copied it to my viewDidLoad method:
- (void)viewDidLoad
{
[super viewDidLoad];
NSURL* musicFile = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"States" ofType:@"mp3"]];
AVAudioPlayer *click = [[AVAudioPlayer alloc] initWithContentsOfURL:musicFile error:nil];
[click play]; // Breakpoint is here
}
Things I've done:
- Imported AudioToolbox/AudioToolbox.h and AVFoundation/AVFoundation.h in my header file
- Executed the code with and without breakpoints --> With the breakpoints and the stepping through I can hear the sound; without I cant.
I would greatly appreciate it if I can get some suggestions or inputs to try it. Or even another way to implement audio in my application
Thank you.