我試圖寫一些非常簡單的代碼來播放音頻文件。該代碼在iPad2模擬器上正常工作。然而,在我的iPad2設備上,[audooPlayer play]返回false,不會聽到任何音頻。AVAudioPlayer播放在硬件上返回false
我試過了.caf和.wav文件。我重新啓動了iPad2並確保靜音開關關閉。如何調試的任何提示大大讚賞。
示例代碼:
- (void)playAudio {
NSString *file = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"wav"];
url = [NSURL fileURLWithPath:file];
NSError *error = nil;
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
if (error != nil) {
NSLog(@"Error creating audio player: %@", [error localizedDescription]);
return;
}
[audioPlayer setDelegate:self];
if (![audioPlayer prepareToPlay]) {
NSLog(@"Error preparing audio");
return;
}
// Everything works fine up to this point
if (![audioPlayer play]) {
// I hit this on hardware. Works perfect on the simulator.
NSLog(@"Error playing audio");
return;
}
我已經在iPhone模擬器和iPhone設備上測試了你的代碼(都是iOS 6.0),它工作正常!我沒有iPad設備。你的錯誤是什麼?你的'audioPlayerDecodeErrorDidOccur'被調用了嗎? – sunkehappy
謝謝你的幫助。您的結果幫助我意識到問題在其他地方 - 請參閱下面的答案。 – Brad
我已經在iPhone模擬器上測試您的代碼,它工作正常! 但在真實設備中無法使用。 :(( –