2011-04-14 121 views
0

我在一個視圖中有10個聲音。而且他們都在玩,但是一次只能播放一個聲音。我想要它,所以你可以點擊你想播放的聲音,而且他們都在同一時間播放。但是當你按下它播放的聲音時,你會按另一個聲音,並且聲音停止。在iOS設備上同時播放聲音

這是我所使用的聲音

- (IBAction)oneSound:(id)sender; { 
    NSString *path = [[NSBundle mainBundle] pathForResource:@"1" ofType:@"wav"]; 
    if (theAudio) [theAudio release]; 
    NSError *error = nil; 
    theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:&error]; 
    if (error) 
     NSLog(@"%@",[error localizedDescription]); 
    theAudio.delegate = self; 
    [theAudio play]; 


} 



- (IBAction)twoSound:(id)sender; { 
    NSString *path = [[NSBundle mainBundle] pathForResource:@"2" ofType:@"wav"]; 
    if (theAudio) [theAudio release]; 
    NSError *error = nil; 
    theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:&error]; 
    if (error) 
     NSLog(@"%@",[error localizedDescription]); 
    theAudio.delegate = self; 
    [theAudio play]; 

} 



- (IBAction)threeSound:(id)sender; { 
    NSString *path = [[NSBundle mainBundle] pathForResource:@"3" ofType:@"wav"]; 
    if (theAudio) [theAudio release]; 
    NSError *error = nil; 
    theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:&error]; 
    if (error) 
     NSLog(@"%@",[error localizedDescription]); 
    theAudio.delegate = self; 
    [theAudio play]; 

} 

感謝代碼!

回答

0

看起來像你的音頻是全球性的,你在每種方法中再次實例化它。將其轉換爲局部變量或定義3(或需要多少個)。

+0

我該怎麼做 – LAA 2011-04-14 22:21:47

2

這可能導致問題:

if (theAudio) [theAudio release]; 

的AVAudioPlayer不能繼續玩它發佈後。

什麼基本情況:
- 按鈕點擊
- theAudio初始化
- theAudio開始播放
- 另一個按鈕,點擊
- theAudio被釋放,併爲此停止播放
- theAudio與另一個聲音初始化
- 音頻開始播放

+0

所以我刪除那一點? – LAA 2011-04-14 23:31:42

+0

不,只需在頭文件中創建'oneAudio','twoAudio'和'threeAudio',而不是隻創建'theAudio'。現在你可以用' - (IBAction)oneSound:(id)sender'來播放'oneAudio'等等。 – Anne 2011-04-15 01:28:55