【问题标题】:Playing sounds in sequence with SimpleAudioEngine使用 SimpleAudioEngine 按顺序播放声音
【发布时间】:2013-02-05 21:06:59
【问题描述】:

我正在用 cocos2d 2 构建一个 iOS 应用,我正在使用 SimpleAudioEngine 来播放一些效果。

有没有办法在前一个声音完成后对多个声音进行排序?

例如在我的代码中:

[[SimpleAudioEngine sharedEngine] playEffect:@"yay.wav"];
[[SimpleAudioEngine sharedEngine] playEffect:@"youDidIt.wav"];

运行此代码时,yay.wavyouDidIt.wav 会同时播放,相互重叠。

我希望 yay.wav 播放,完成后,youDidIt.wav 播放。

如果不使用 SimpleAudioEngine,是否有使用 AudioToolbox 或其他方式的方法?

谢谢!

== 更新 ==

我想我将使用 AVFoundation 来使用这种方法:

AVPlayerItem *sound1 = [[AVPlayerItem alloc] initWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"yay" ofType:@"wav"]]];
AVPlayerItem *sound2 = [[AVPlayerItem alloc] initWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"YouDidIt" ofType:@"wav"]]];    
AVQueuePlayer *player = [[AVQueuePlayer alloc] initWithItems: [NSArray arrayWithObjects:sound1, sound2,  nil]];
[player play];

【问题讨论】:

  • 已编辑:找到一个当前有效的方法。

标签: ios audio simpleaudioengine


【解决方案1】:

简单的方法是使用-[CDSoundSource durationInSeconds] 获取曲目持续时间,然后在适当的延迟后安排第二个效果播放:

[[SimpleAudioEngine sharedEngine] performSelector:@selector(playEffect:) withObject:@"youDidIt.wav" afterDelay:duration];

获取音频持续时间的更简单方法是修补 SimpleAudioManager 并添加一个方法来查询其 CDSoundEngine(静态全局)以获取音频持续时间:

- (float)soundDuration {
    return [_engine bufferDurationInSeconds:_soundId];
}

第二种方法是轮询音频引擎的状态并等待它停止播放。

    alGetSourcei(sourceId, AL_SOURCE_STATE, &state);
    if (state == AL_PLAYING) {
      ...

sourceIdplayEffect 返回的ALuint

【讨论】:

  • 你真的能从 ALuint 声音中得到 durationInSeconds 吗?
【解决方案2】:

很简单,用 Cocos2D 2.1 测试过:

  1. 在 SimpleAudioEngine.h 上创建一个属性 durationInSeconds :

    @property (readonly) float durationInSeconds;
    
  2. 合成它们:

    @synthesize durationInSeconds;
    
  3. 像这样修改playEffect:

    -(ALuint) playEffect:(NSString*) filePath pitch:(Float32) pitch pan:(Float32) pan gain:(Float32) gain {
        int soundId = [bufferManager bufferForFile:filePath create:YES];
        if (soundId != kCDNoBuffer) {
            durationInSeconds = [soundEngine bufferDurationInSeconds:soundId];
            return [soundEngine playSound:soundId sourceGroupId:0 pitch:pitch pan:pan gain:gain loop:false];
        } else {
            return CD_MUTE;
        }
    }
    
  4. 如您所见,我直接从 soundEngine bufferDurationInSeconds 中插入了来自 bufferManager 的结果的 durationInSeconds 值。

  5. 现在,只需询问 [SimpleAudioEngine sharedEngine].durationInSeconds 的值,您就会得到此声音的浮动持续时间(以秒为单位)。

  6. 在这几秒钟内设置一个计时器,然后在此之后播放下一次重复的声音或关闭标志或其他操作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-29
    • 2011-09-28
    • 1970-01-01
    • 2012-02-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多