使用播放它的相同button停止声音
很简单,我使用一个button,产生一个随机数字,然后与开关/情况下,它animation图像和播放声音。 我的问题是,当你再次按下button时,以前播放的声音不会停止,声音重叠。
代码如下:
- (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
我知道这不是最好的办法,一次又一次地声明同一个variables。 但有没有办法阻止它发挥另一个?
我只是回答了我自己的问题。
我做的是
- 声明
SoundSystemID soundID;
在标题中。 - 在生成随机数之前添加以下行:
AudioServicesDisposeSystemSoundID(soundID);
请记住,如果您要再次播放声音,则需要在处理完资源后再次加载资源。
谢谢你们,无论如何。