【问题标题】:'NSInvalidArgumentException', reason: '*** -[NSURL initFileURLWithPath:]: nil string parameter'NSInvalidArgumentException',原因:'*** -[NSURL initFileURLWithPath:]:nil 字符串参数
【发布时间】: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


【解决方案1】:

使用断点检查路径,如果它为 nil,则可能资源不在包中或使用不同的名称。
请注意,虽然模拟器不区分大小写,但设备区分大小写。

【讨论】:

  • 这是另一个问题,你应该发布另一个问题。您确定设备没有处于静音模式吗?
猜你喜欢
  • 2014-04-28
  • 2011-03-14
  • 1970-01-01
  • 1970-01-01
  • 2011-07-01
  • 1970-01-01
  • 2022-07-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多