2011-07-11 54 views
0

我想循環我的聲音文件,我無法找到循環聲音文件的參數!我即將把最後站立的頭髮拉出我的頭。它加載罰款....它播放....我只是無法弄清楚如何使其循環的代碼。如何循環播放iPhone應用程序的聲音?

這裏是我的代碼:

NSString *eyeBGPath = [[NSBundle mainBundle] pathForResource:@"theeye" ofType:@"caf"]; 
CFURLRef eyeBGURL = (CFURLRef) [NSURL fileURLWithPath:eyeBGPath]; 
AudioServicesCreateSystemSoundID(eyeBGURL, &eyeBackGroundID); 
AudioServicesPlaySystemSound(eyeBackGroundID); 

什麼我錯在這裏做什麼?

我發現這一點:

循環 指示當它到達其內容的末尾接收器是否重新播放。默認:NO。

- (BOOL)loops 
Return Value 

YES當接收器在完成時重新開始播放,否則爲否。

可用性 適用於Mac OS X v10.5及更高版本。 另請參見 - setLoops: 宣佈 NSSound.h

http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/ApplicationKit/Classes/NSSound_Class/Reference/Reference.html%23//apple_ref/occ/instm/NSSound/loops

我對如何實現這一困惑。

+0

它真的是一個很大的聲音嗎? (請不要在您的問題標題中使用全部上限)。 – ceejayoz

+1

對不起,全部。我通常不會把時間花在網絡論壇上,所以我不熟悉THE CAPS的事情,有人認爲這是大喊大叫。道歉。 – Malin

+0

不用擔心,現在你知道了。 :-) – ceejayoz

回答

2

這是爲我工作的代碼。如果有人需要幫助,請告訴我。蘋果開發者論壇上的某個人向我指出了AVFoundation文檔。

NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/theeye3.caf", [[NSBundle mainBundle] resourcePath]]]; 

audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:NULL]; 
audioPlayer.numberOfLoops = -1; 

[audioPlayer play]; 

這裏是根據圖像數組中的隨機數挑出一個聲音文件的代碼。

NSString *audioWiz = [[NSString alloc]initWithFormat:@"%d.caf", randNum]; 

NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:[@"%@/" stringByAppendingString:audioWiz], [[NSBundle mainBundle] resourcePath]]]; 

audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:NULL]; 
audioPlayer.numberOfLoops = 0; 
audioPlayer.volume = 0.1; 

[audioPlayer play]; 
0

我認爲您正在尋找的答案是here

簡短回答,請使用MPMusicPlayerController

+0

NSURL * url = [NSURL fileURLWithPath:[NSString stringWithFormat:@「%@/theeye3.caf」,[[NSBundle mainBundle] resourcePath]]]; \t // NSError * error; \t audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:NULL]; \t audioPlayer.numberOfLoops = -1; [audioPlayer play];此代碼有效。 – Malin

相關問題