【发布时间】:2015-03-20 16:51:52
【问题描述】:
我只是为我的游戏添加声音效果,在我输入这段代码之前效果很好:
if (hit == NO) {
NSString *path = [[NSBundle mainBundle] pathForResource:@"miss" ofType:@"m4a"];
AVAudioPlayer* theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
[theAudio play];
NSLog(@"audio played miss.m4a");
// if hit is no miss sound effect plays
}
else {
NSString *path = [[NSBundle mainBundle] pathForResource:@"hit" ofType:@"m4a"];
AVAudioPlayer* theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
[theAudio play];
NSLog(@"audio played hit.m4a");
//if hit is yes or else (not no) then it plays hit sound effect
}
它说问题出在 [NSURL fileURLWithPath:path] 部分,我已经检查过它并查看了许多问题和教程,但似乎找不到解决方案。我的所有文件都在 Xcode 项目中,并且命名正确并对应于我的 pathForResource:@"miss" 和 hit 以及 ofType 等。
【问题讨论】:
-
您的文件是否存在于
Build Phases的“复制捆绑资源”中? -
也许你还没有将文件添加到“复制捆绑资源”中:stackoverflow.com/a/9332075/2274694
-
是的,我已将其添加到复制捆绑资源上方的文件夹中,谢谢
标签: ios objective-c xcode avfoundation