AVAudioPlayer 在 sprite kit 场景中不工作 Xcode 7

AVAudioPlayer is not working in sprite kit scene Xcode 7

我一直在使用以下代码在 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]; 我已经查阅了文档,但无法找出我已经拥有的内容有什么问题。任何人都知道如何解决这个问题?以下是建议:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSURL initFileURLWithPath:]: nil string parameter'

而不是:

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

试试这个:

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