Objective-C - 尝试播放声音

时间:2015-09-21 12:30:42

标签: ios objective-c xcode

我正在尝试使用以下代码在我的应用中播放声音:

NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:@"errorbeep.wav" ofType:@"m4a"];
        NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];

        AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL
                                                                       error:nil];
        player.numberOfLoops = -1; //Infinite

        [player play];

但声音没有播放:(

调试后,我可以看到soundFilePath是nil这是我的资源文件的截图:

enter image description here

3 个答案:

答案 0 :(得分:1)

您应该使用一个扩展名将文件导入项目。那可能是正在发生的事情。只需将文件重新导入为一个扩展名(即errorbeep.m4a)

答案 1 :(得分:0)

如果文件路径为nil,我会尝试将文件重命名为errorbeep.m4a,然后执行:

NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:@"errorbeep" ofType:@"m4a"];

答案 2 :(得分:0)

选择您的声音文件,看看您的目标会员资格是否已经过检查。

enter image description here

相关问题