我正在尝试从我的应用程序中从我的服务器检索播放和 mp3 文件,执行以下操作:
- (IBAction)play:(UIButton *)sender {
dispatch_queue_t downloadQueue = dispatch_queue_create("audio data downloader", NULL);
dispatch_async(downloadQueue, ^{
NSURL *audioURL = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@/%@.mp3", FONYK_FILES_URL, [self.voicenote valueForKeyPath:@"Fonyker.fonykid"], [self.voicenote valueForKeyPath:@"Voicenote.vnid"]]];
NSData *audioData = [NSData dataWithContentsOfURL:audioURL];
dispatch_async(dispatch_get_main_queue(), ^{
NSError *error = nil;
AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithData:audioData error:&error];
NSLog(@"%@", error);
audioPlayer.delegate = self;
[audioPlayer play];
});
});
}
播放失败,这意味着没有声音发出,我在逐步调试我的应用程序时得到这个输出:
Catchpoint 2 (exception thrown).Single stepping until exit from function __cxa_throw, which has no line number information.
当我在模拟器中对错误进行 NSLog 记录时,就会出现这种情况:
Error Domain=NSOSStatusErrorDomain Code=-50 "The operation couldn’t be completed. (OSStatus error -50.)"
没有其他任何迹象表明它只是失败了,有什么想法吗?
更新:根据 J_D 的回答修改了我的代码,并在模拟器中得到了这个:
Error loading /System/Library/Extensions/AudioIPCDriver.kext/Contents/Resources/AudioIPCPlugIn.bundle/Contents/MacOS/AudioIPCPlugIn: dlopen(/System/Library/Extensions/AudioIPCDriver.kext/Contents/Resources/AudioIPCPlugIn.bundle/Contents/MacOS/AudioIPCPlugIn, 262): Symbol not found: ___CFObjCIsCollectable
Referenced from: /System/Library/Frameworks/Security.framework/Versions/A/Security
Expected in: /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator5.1.sdk/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation
in /System/Library/Frameworks/Security.framework/Versions/A/Security
2012-04-17 15:03:43.054 Fonyk[8238:15307] Error loading /System/Library/Extensions/AudioIPCDriver.kext/Contents/Resources/AudioIPCPlugIn.bundle/Contents/MacOS/AudioIPCPlugIn: dlopen(/System/Library/Extensions/AudioIPCDriver.kext/Contents/Resources/AudioIPCPlugIn.bundle/Contents/MacOS/AudioIPCPlugIn, 262): Symbol not found: ___CFObjCIsCollectable
Referenced from: /System/Library/Frameworks/Security.framework/Versions/A/Security
Expected in: /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator5.1.sdk/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation
in /System/Library/Frameworks/Security.framework/Versions/A/Security
更新:我遇到的真正问题不是在我的代码中创建和 AVAudioSession 实例设置为播放,这样做与我接受的更正答案一起使它工作。