iOS 5.0 AVAudioPlayer加载音频片段出错:无法完成操作。(OSStatus错误-50。)

4

我正在尝试在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方法就可以正常工作了。真是太不可思议了。


.caf 应该在 iOS5 中得到支持,但也许你应该尝试使用 mp3 或其他格式。你记得将 Jonah.caf 导入项目中吗?另外:为什么要设置代理 - 你需要它吗?如果只调用 [audioPlayer prepareToPlay]; 而不是 [audioPlayer play];,你是否会得到相同的错误? - Rok Jarc
2个回答

2

试试这个方法 - 将您的网址转换为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];
}`

0
尝试使用类似以下代码读取音频文件:
audioPlayer_ = [[AVAudioPlayer alloc] initWithContentsOfURL:
[NSURL fileURLWithPath:[NSString stringWithString:soundFilePath_]] 
error:&error];

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接