我正在尝试在iPhone上测试音频播放器,并参考了Troy Brant的iOS书籍。我已经将Core Audio、Core Foundation、AudioToolbox和AVFoundation框架添加到我的项目中。我收到的错误消息在主题字段中。在求助于这里之前,我阅读了大约20页的Google搜索结果!/叹气。如果您能帮忙,谢谢。这是我的代码,几乎完全按照他的书:
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:@"Yonah" ofType:@"caf"];
NSLog(@"%@", soundFilePath);
NSURL *fileURL = [NSURL URLWithString:soundFilePath];
NSError *error;
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:&error];
if (!error)
{
audioPlayer.delegate = self;
//audioPlayer.numberOfLoops = -1;
[audioPlayer play];
}
else
{
NSLog(@"Error loading audio clip: %@", [error localizedDescription]);
}
编辑:天啊,我知道是什么了。我修改了
NSURL *fileURL = [NSURL URLWithString:soundFilePath];
为了
NSURL *fileURL = [NSURL fileURLWithPath:soundFilePath];
我遇到了一个奇怪的错误,比主题中的错误更奇怪。但是我在谷歌上搜索后,将我的操作系统输入设备从网络摄像头改为内置麦克风,然后使用fileURLWithPath方法就可以正常工作了。真是太不可思议了。