0

我在一个简单的游戏中编码,我移动一个方块,每次方块移动时都会产生声音效果。(像走路的声音)

问题是播放音效导致显示刷新滞后。好像对引擎来说性能太高了。我在问是否有办法正确播放重复但不连续的声音(这不是真正的循环,但如果行走是无止境的,可能是这样)

这是我的功能:...我初始化音频对象的方式

moveSound = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:nil];

这里是调用它的函数

...
 {
  moveSound.currentTime = 0;
  [moveSound play];
 } 

有什么好办法吗?谢谢

4

2 回答 2

1

为了获得更低延迟的音频,您可能应该查看CoreAudio API

您的代码将如下所示:

NSString *audioPath = [[NSBundle mainBundle] pathForResource:@"NameOfSound" ofType:@"caf"];
NSURL *audioURL = [NSURL fileURLWithPath:audioPath];
SystemSoundID soundId;
AudioServicesCreateSystemSoundID((CFURLRef)audioURL, &soundId);

该 soundId 充当您创建的声音的句柄,因此每当您需要播放它时,您都会调用

AudioServicesPlaySystemSound(soundId);
于 2010-05-25T19:51:57.077 回答
0

好的,我发现了这个:

块引用 -(void)setSound {

CFBundleRef mainBundle = CFBundleGetMainBundle ();

CFURLRef soundFileURLRef = CFBundleCopyResourceURL (
                                            mainBundle,
                                            CFSTR ("squareMove"),// à mettre dans un xml ou plist
                                            CFSTR ("caf"),
                                            NULL
                                            );

AudioServicesCreateSystemSoundID (
                                  soundFileURLRef,
                                  &soundFileObject
                                  );
CFRelease(soundFileURLRef);

}

我播放的声音就像

AudioServicesPlaySystemSound (soundFileObject);

可惜我们不能做midi

于 2010-06-10T13:36:59.043 回答