【问题标题】:AVAudioPlayer is not working in sprite kit scene Xcode 7AVAudioPlayer 在精灵套件场景 Xcode 7 中不起作用
【发布时间】:2015-09-12 19:36:45
【问题描述】:

我一直在使用以下代码在 sprite kit 游戏中播放背景音乐,它在 Xcode 6.4 中运行良好,但现在抛出异常并导致游戏崩溃:

- (void)startBackgroundMusic
{
    NSError *err;
    NSURL *file = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"lostinspace.caf" ofType:nil]];
    _backgroundAudioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:file error:&err];
    if (err) {
        NSLog(@"error in audio play %@",[err userInfo]);
        return;
    }
    [_backgroundAudioPlayer prepareToPlay];
    _backgroundAudioPlayer.numberOfLoops = -1;
    [_backgroundAudioPlayer setVolume:0.5];
    [_backgroundAudioPlayer play];
}

在 didMoveToView 中将上述代码声明为 :[self startBackgroundMusic]; 我查看了文档,但无法找出我已经拥有的内容有什么问题。有谁知道如何解决这个问题?以下是建议:

由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“*** -[NSURL initFileURLWithPath:]: nil string parameter”

【问题讨论】:

    标签: objective-c sprite-kit avplayer ios9 xcode7


    【解决方案1】:

    代替:

    NSURL *file = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"lostinspace.caf" ofType:nil]];
    

    试试这个:

    NSURL *file = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"lostinspace" ofType:@"caf"]];
    

    【讨论】:

    • 你的答案是正确的。我不得不清理项目并关闭并重新启动 Xcode,但它成功了。谢谢。
    猜你喜欢
    • 2014-03-02
    • 2014-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多