我试图播放和MP3文件,我从我的服务器检索我的应用程序执行以下操作:iOS的 - 在尝试播放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.)"
没有别的表明它只是失败,任何想法?
UPDATE:修改我的代码按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
UPDATE:真正的问题我没有创建和AVAudioSession例如设置在我的代码回放,这样做与我接受的更正答案一起工作。
谢谢,我会试试看。关于你最后的观察,任何想法如何我可以流文件? – 8vius 2012-04-17 18:04:28
这样做,它停止在AVAudioPlayer实例化并打印出:Catchpoint 10(抛出异常)。单步执行,直到退出函数__cxa_throw, (没有行号信息)。如果我继续,它什么都不做。 – 8vius 2012-04-17 18:09:58
我不知道为什么它能正常工作,然后我使用了与我的答案完全相同的代码,只是我使用了自己的URL。也许这与这个URL的内容有关。我也在模拟器5.0上进行测试。要回答关于流式传输的问题,请使用MPMoviePlayerController。我将添加另一个答案来获得一些可读代码。 – 2012-04-17 18:29:20