Search code examples
iosavaudioplayerios6.1

NSBundle finds mp3 file but not m4a when trying to instantiate AVAudioPlayer


I have a simple app that is supposed to play a audio file. Whenever I load a mp3 file to my NSURL is works fine and the AVAudioPlayer gets to play the song. However, when I change to a m4a file, the NSString path returns nil, like it is not able to find the path to my file, and thus the NSURL is not formed. The m4a file is of course there, in the project. It follows below the code, but I don't think is a code issue. Any ideias? Thanks.

NSString *path = [[NSBundle mainBundle] pathForResource:@"end" ofType:@"m4a"];
NSURL *urlFile = [NSURL fileURLWithPath:path];
NSError *error;
soundPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:urlFile error:&error];
if (error)
{
    NSLog(@"Error in audioPlayer: %@",[error localizedDescription]);
} else {
    soundPlayer.delegate = self;
    [soundPlayer prepareToPlay];
    [soundPlayer play];
}

Solution

  • Check your Target's Build Phases, if your m4a file is not listed under Copy Bundle Resources phase group, add it, then Clean, and Run again.