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

六眼飞鱼酱① 提交于 2019-12-23 19:39:19

问题


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];
}

回答1:


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.



来源:https://stackoverflow.com/questions/17867213/nsbundle-finds-mp3-file-but-not-m4a-when-trying-to-instantiate-avaudioplayer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!