Question

i checked for answers but couldn't find any here is my code..i also logged to see if I'm missing something but both loggs showed up but the sound didn't play...i also imported the framework...i cant figure out what I'm doing wrong I'm a newbie please help...Thanks

- (void)viewDidLoad
{

    NSLog(@"play!");

    NSString *path = [[NSBundle mainBundle]pathForResource:@"Submarine" ofType:@"mp3"];
    AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
    [audioPlayer prepareToPlay];
    [audioPlayer play];

    NSLog(@"not playing!");
}
Was it helpful?

Solution

ARC will decrease the retain count of audioPlayer as soon as viewDidLoad finishes executing. Declare audioPlayer as an instance variable, then assign in viewDidLoad to solve this issue.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top