2010-07-06 98 views
0

即時通訊新開發者和製作我的firt iPhone應用程序,並且我想讓按鈕打開/關閉多重音樂,例如當我播放音樂後按下多個聲音按鈕時它會同時播放所有聲音如果它的關閉只能播放一種聲音,那麼什麼代碼會使ON/OFF多個聲音播放?iPhone,AVAudioPlayer和多重音樂

對不起我的英文不好坦克!

回答

1

米爾扎,

爲了一次播放多個聲音,蘋果公司建議使用.caf格式是硬件解碼。此外,基本上只是爲每個聲音文件創建一個新的AVAudioPlayer對象,而不是在一次只需要播放一個聲音的情況下重新使用同一個對象。

我不會進入太多的代碼,因爲已經有很多在那裏,如果你只是尋找它......但這裏有一些有用的鏈接,讓你開始:

http://developer.apple.com/iphone/library/documentation/AVFoundation/Reference/AVAudioPlayerClassReference/Reference/Reference.html

http://www.mobileorchard.com/easy-audio-playback-with-avaudioplayer/

// a function I use to play multiple sounds at once 
- (void)playOnce:(NSString *)aSound { 

// Gets the file system path to the sound to play. 
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:aSound ofType:@"caf"]; 

// Converts the sound's file path to an NSURL object 
NSURL *soundURL = [[NSURL alloc] initFileURLWithPath: soundFilePath]; 
self.soundFileURL = soundURL; 
[soundURL release]; 

AVAudioPlayer * newAudio=[[AVAudioPlayer alloc] initWithContentsOfURL: soundFileURL error:nil]; 
self.theAudio = newAudio; // automatically retain audio and dealloc old file if new file is loaded 

[newAudio release]; // release the audio safely 

// buffers data and primes to play 
[theAudio prepareToPlay]; 

// set it up and play 
[theAudio setNumberOfLoops:0]; 
[theAudio setVolume: volumeLevel]; 
[theAudio setDelegate: self]; 
[theAudio play]; 

}