4
非常簡單,我使用一個按鈕生成一個隨機數,然後使用開關/外殼爲它動畫並播放聲音。我的問題是,當您再次按下按鈕時,之前播放的聲音不會停止,並且聲音重疊。使用播放它的同一個按鈕來停止聲音
下面的代碼:
- (IBAction) pushme: (id) sender{
int random = (arc4random()%10);
switch (random) {
case 0: {
[ANIMATION NUMBER 0]
NSString *path = [NSString stringWithFormat:@"%@%@",[[NSBundle mainBundle] resourcePath],@"/sound0.wav"];
SystemSoundID soundID;
NSURL *filePath = [NSURL fileURLWithPath:path isDirectory:NO];
AudioServicesCreateSystemSoundID((CFURLRef)filePath, &soundID);
AudioServicesPlaySystemSound(soundID);
}
break;
case 1: {
[ANIMATION NUMBER 1]
NSString *path = [NSString stringWithFormat:@"%@%@",[[NSBundle mainBundle] resourcePath],@"/sound1.wav"];
SystemSoundID soundID;
NSURL *filePath = [NSURL fileURLWithPath:path isDirectory:NO];
AudioServicesCreateSystemSoundID((CFURLRef)filePath, &soundID);
AudioServicesPlaySystemSound(soundID);
} break; //Lots more cases after this one
我知道,這是不這樣做,一遍又一遍聲明同一變量的最佳方式。 但有沒有辦法阻止它再播放另一個?
大聲笑我剛剛發現了我自己的問題的答案。 – Nicolas 2011-04-23 17:17:53
太棒了!請在此發佈答案,以便其他人可以從您的研究中受益。 – 2011-04-23 17:20:26
補充:)真的很簡單 – Nicolas 2011-04-23 17:22:03