Nic*_*las 4 cocoa-touch objective-c ios
非常简单,我使用一个按钮生成随机数,然后使用开关/案例,它会动画图像并播放声音.我的问题是当你再次按下按钮时,之前播放的声音不会停止并且声音重叠.
这是代码:
- (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
我知道这不是最好的方法,一遍又一遍地声明相同的变量.但有没有办法在播放另一个之前阻止它?
我刚刚回答了我自己的问题.
我做的是
SoundSystemID soundID;在标题中声明.AudioServicesDisposeSystemSoundID(soundID); 请记住,如果要再次播放声音,则需要在处理完资源后再次加载资源.
无论如何,谢谢你们.
| 归档时间: | 
 | 
| 查看次数: | 2570 次 | 
| 最近记录: |