依次使用SimpleAudioEngine播放声音

问题描述:

我正在用cocos2d 2构建一个iOS应用程序,并且我正在使用SimpleAudioEngine播放一些效果。依次使用SimpleAudioEngine播放声音

在上一个声音完成后,是否有排序多个声音的方法?

举例来说,在我的代码:

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

当这个代码运行,yay.wav并在准确的同时youDidIt.wav戏,过对方。

我想yay.wav玩,一旦完成,youDidIt.wav玩。

如果不是SimpleAudioEngine,那么AudioToolbox还有其他的方法吗?

谢谢!

== UPDATE ==

我想我会使用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]; 
+0

编辑:找到了一种当前正在工作的方法。 – Corey 2013-02-20 19:10:12

最简单的方式会得到使用-[CDSoundSource durationInSeconds],然后安排轨道持续时间在适当延迟后播放第二个效果:

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

获得音频持续时间的更简单的方法woul d是修补SimpleAudioManager并添加查询其CDSoundEngine(静态全局)的音频持续时间的方法:在音频引擎的状态

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

第二种方法是轮询和等待它停止播放。

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

sourceIdplayEffect返回ALuint

+0

你真的能从ALuint声音中获得durationInSeconds吗? – Jonny 2013-05-31 00:52:32

很简单,用的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. 如您所见,我直接从bufferManager的结果中的soundEngine bufferDurationInSeconds中插入du​​rationInSeconds值。

  5. 现在,只需询问[SimpleAudioEngine sharedEngine] .durationInSeconds的值,并获得该声音的秒数。

  6. 把一个计时器这秒,然后播放你的声音的下一个迭代或将标志关闭或什么。