2011-04-30 141 views
0

你好 我在視圖中有16個聲音。而他們循環等我想要一個矩形按鈕,你可以點擊它的所有聲音停止。 這裏是我用於聲音的一個代碼,它同樣爲休息。#立即停止所有聲音?

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

} 

我試圖

-(IBAction)goStop:(id)sender; { 

    [oneAudio, twoAudio, threeAudio, fourAudio, fiveAudio, sixAudio, sevenAudio, eightAudio, nineAudio, tenAudio, elevenAudio, twelveAudio, thirteenAudio, fourteenAudio, fifthteenAudio, sixteenAudio stop]; 
} 

但沒有工作。

謝謝

+0

讓你希望Objective-C有ActionScript的'stopAllSounds()'或其他東西:) – BoltClock 2011-04-30 23:57:26

+0

大聲笑是啊,我希望!我想我嘗試了懶惰的方式..和史蒂夫工作可能不喜歡懶惰的人:( – user731501 2011-05-01 00:01:28

回答

3

我想你必須使用NSArray而不是許多聲音對象。所以你可以很容易地用12個聲音填充陣列,然後你可以使用「for」循環來阻止它們。

+0

或一個NSMutableArray,如果你需要它 – edo42 2011-05-01 04:22:33

+0

這可能是最好的答案,或者使用'NSArray'的預定義'AVAudioPlayer'對象,或創建一個「NSMutableArray」來跟蹤這些對象的創建過程。 – Joseph 2011-06-14 20:51:05