所以我试图在 iPhone 上测试音频播放器,然后我离开了 Troy Brant 的 iOS 书。我的项目中添加了 Core Audio、Core Foundation、AudioToolbox 和 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];
}`
关于iphone - iOS 5.0 AVAudioPlayer 加载音频剪辑时出错 : The operation couldn’t be completed.(OSStatus 错误 -50。),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9021032/