2012-01-26 78 views
4

所以我試圖測試iPhone上的音頻播放器,然後我離開了Troy Brant的iOS書。我將Core Audio,Core Foundation,AudioToolbox和AVFoundation框架添加到我的項目中。我得到的錯誤消息是在主題字段中。在訴諸詢問之前,我讀了20頁Google搜索結果! /嘆。謝謝,如果你能幫助。這是我的代碼,幾乎逐字地出自他的書:iOS 5.0 AVAudioPlayer加載音頻片段時出錯:操作無法完成。 (OSStatus錯誤-50。)

NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:@"Yonah" ofType:@"caf"]; 
NSLog(@"%@", soundFilePath); 
NSURL *fileURL = [NSURL URLWithString:soundFilePath]; 
NSError *error; 
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:&error]; 

if (!error) 
{ 
    audioPlayer.delegate = self; 
    //audioPlayer.numberOfLoops = -1; 
    [audioPlayer play]; 
} 
else 
{ 
    NSLog(@"Error loading audio clip: %@", [error localizedDescription]); 
}  

編輯:聖神道。我想清楚它是什麼。我改變

NSURL *fileURL = [NSURL URLWithString:soundFilePath]; 

NSURL *fileURL = [NSURL fileURLWithPath:soundFilePath]; 

後者和我得到一個奇怪的錯誤,比在主題怪異但我GOOGLE了這一點,我改變了我的OS輸入裝置從我的網絡攝像頭我的內置麥克風,並猜測它是什麼,它在fileURLWithPath方法下工作。我將會。詛咒。

+0

應的.caf在iOS5中得到支持,但也許你應該嘗試MP3或什麼的。你確實記得在項目中導入你的Jonah.caf嗎?另外:你爲什麼設置代表 - 你需要它嗎?如果你只是調用[audioPlayer prepareToPlay],你會得到同樣的錯誤;而不是[audioPlayer播放];? –

回答

0

嘗試使用這樣的讀取音頻文件:

audioPlayer_ = [[AVAudioPlayer alloc] initWithContentsOfURL: 
[NSURL fileURLWithPath:[NSString stringWithString:soundFilePath_]] 
error:&error]; 
2

試試這個單你的URL轉換爲NSData的

NSString* resourcePath = self.audioStr; //your url 
NSData *_objectData = [NSData dataWithContentsOfURL:[NSURL URLWithString:resourcePath]]; 
NSError *error = [[NSError alloc] init]; 

NSLog(@"url is %@",resourcePath); 

audioPlayer = [[AVAudioPlayer alloc] initWithData:_objectData error:&error]; 
audioPlayer.numberOfLoops = 0; 

if (error) 
{ 
    NSLog(@"Error in audioPlayer: %@", 
      [error localizedDescription]); 
} else { 
    audioPlayer.delegate = self; 
    [audioPlayer prepareToPlay]; 
}` 
相關問題