iphone - iOS 5.0 AVAudioPlayer 加载音频剪辑时出错 : The operation couldn’t be completed.(OSStatus 错误 -50。)

标签 iphone ios ios5 avaudioplayer

所以我试图在 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/

相关文章:

objective-c - 如何将 NSTimeInterval(秒)转换为分钟

iphone - 裁剪 UIImage 以适合框架图像

iPhone UIView touchesMoved : withEvent: does not get called on UIView with gesture recognizers

ios - 是否可以仅在App Store中更新部分应用程序?

objective-c - 两个导航 Controller (在 Storyboard 中)

ios - NSJSONSerialization 与基于流的解析器

iphone - UITableView 的屏幕截图

iphone - 图表中的文字换行

ios - 如何使用 Firebase 跟踪 iOS 应用程序卸载/删除事件?

iphone - 如何将 AppDelegate 中对象的引用传递给 ViewController