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