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!");
}
有帮助吗?

解决方案

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top