所以我试图在iPhone上测试音频播放器,我退出了Troy Brant的iOS书籍。我的项目中添加了核心音频、核心基础、音频工具箱和AVFoundation框架。我收到的错误信息在主题字段中。我读了大约20页的谷歌搜索结果,然后才问这里/叹气如果你能帮忙的话,谢谢。这是我的代码,几乎逐字逐句地出自他的书:
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方法下工作。我会的,该死。
试试这个-将您的url转换为NSdata
NSString* resourcePath = self.audioStr; //your url
NSData *_objectData = [NSData dataWithContentsOfURL:[NSURL URLWithString:resourcePath]];
NSError *error = [[NSError alloc] init];
NSLog(@"url is %@",resourcePath);
audioPlayer = [[AVAudioPlayer alloc] initWithData:_objectData error:&error];
audioPlayer.numberOfLoops = 0;
if (error)
{
NSLog(@"Error in audioPlayer: %@",
[error localizedDescription]);
} else {
audioPlayer.delegate = self;
[audioPlayer prepareToPlay];
}`
尝试使用以下内容读取音频文件:
audioPlayer_ = [[AVAudioPlayer alloc] initWithContentsOfURL:
[NSURL fileURLWithPath:[NSString stringWithString:soundFilePath_]]
error:&error];