【问题标题】:Objective C sound lagging when multiple repeat多次重复时Objective C声音滞后
【发布时间】:2010-06-10 21:36:59
【问题描述】:

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

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

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

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

这里是调用它的函数

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

有什么好的方法吗? 谢谢

【问题讨论】:

    标签: objective-c audio avaudioplayer lag


    【解决方案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);
    

    【讨论】:

      【解决方案2】:

      好的,我发现了这个:

      块引用 -(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 (声音文件对象);

      可惜我们不能做midi

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-12-27
        • 2012-10-15
        • 2014-01-05
        相关资源
        最近更新 更多