我試圖播放和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