iOS – 试图播放MP3文件失败

我试图回放和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]; }); }); } 

播放失败,意味着没有声音发出,我得到这个输出时,一步一步地debugging我的应用程序:

 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 

更新:我真正的问题是没有创build和AVAudioSession实例设置为在我的代码播放,这样做与更正答案我接受使它的工作。

我看到至less有一个问题与您的代码,行

 NSURL *fileURL = [NSURL URLWithString:filePath]; 

它尝试使用本地文件path创build一个URL。 它可能会返回零。 你应该使用:

 NSURL *fileURL = [NSURL fileURLWithPath:filePath]; 

现在,即使是这样,你的代码也不能运行(我试过了,得到了同样的错误)。 我自己并没有使用AVAudioPlayer,所以我不知道为什么你的具体错误发生。

不过,我使用你的代码尝试了一个非常小的例子,我能够正确地播放MP3。 您可以使用AVAudioPlayer的initWithData构造函数,而不是将数据下载到文件并从文件中加载AVAudioPlayer:

 AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithData:[NSData dataWithContentsOfURL:audioURL] error:&error]; 

这工作正常,我认为audioURL是有效的。 我指着我的服务器上的mp4进行testing。

所以用这个initWithData重写你的代码给出:

 - (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]; }); }); } 

正如最后一个注意事项:在开始播放之前,您正在下载整个文件,这可能需要一段时间,并消耗大量的堆(或者如果要将其保存在文件中,则需要存储)

如果要stream式处理,可以使用MPMoviePlayerController,但不要将视图附加到视图层次结构中。 这样,你将只有audio,不会改变你的应用程序的任何视觉。

一个例子是:

 NSURL *audioURL = [NSURL URLWithString:@"MY_MP3_URL"]; MPMoviePlayerController* player = [[MPMoviePlayerController alloc] initWithContentURL:audioURL]; [player play]; 

再次,在我的身边,在模拟器,它工作正常。

不要忘记释放播放器。 您也可以注册代表重要的通知。

详情在这里: http : //developer.apple.com/library/ios/#documentation/mediaplayer/reference/MPMoviePlayerController_Class/Reference/Reference.html

您正在某处收到EXC_BAD_ACCESS。 我build议删除所有的asynchronous代码,看看会发生什么。 实际上,就asynchronous调用而言,我的思维过程有点困惑。

请注意,AVAudioPlayer只适用于LOCAL文件。 似乎你可能会设置它来播放从服务器的audio文件,这将无法正常工作。

另外,为什么不尝试NSLog的错误variables?