繁体   English   中英

发行音频-Cocoa Xcode(内存管理)

[英]Releasing Audio - Cocoa Xcode (Memory Management)

因此,在我之前的问题FOUND HERE中 ,我试图找到每次让精灵在iOS游戏中跳跃时播放“跳”声的最佳方法...

今天由于某种原因,我开始担心要加载音频文件数百万次并浪费内存:(

有人向我提出了以下建议(因此,每次我通过点击屏幕进行跳转时,都会加载并播放声音)...

    -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *) event
    {
            //NSString *
            jumpSound = [[NSBundle mainBundle] pathForResource:@"boing" ofType:@"mp3"];

            //AVAudioPlayer *
            jumpAffect = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:jumpSound] error:NULL];

            [jumpAffect prepareToPlay];
            [jumpAffect play];

            //then, make sprite jump
            jumpUp = 16;
    }

上面的方法允许我快速按一下跳转并重新启动音频循环,即使上一个循环(来自上一个跳转)尚未完成也是如此。

值得一提的是,这可能每分钟发生数百次(或更实际的是每秒发生两次)...

这是一个问题吗? 完成方法/游戏循环后,我是否需要释放曲目?

我认为您的最后一个问题是完全错误的。 AVAudioPlayer对游戏的等待时间过多。 当您跳跃时,声音可能会在第二秒发生,而不是在您想要时立即发生。 如果您不想每分钟播放一百万次声音,则应按以下方式使用AudioServices:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
     NSURL *pathURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"boing" ofType:@"wav"]];

     SystemSoundID audioEffect;
     AudioServicesCreateSystemSoundID((__bridge CFURLRef) pathURL, &audioEffect);
     AudioServicesPlaySystemSound(audioEffect);
     //then, make sprite jump
     jumpUp = 16;
}

您将必须将mp3转换为wav或caf。 但是我认为这是值得的...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM