本文介绍了播放声音序列与SimpleAudioEngine的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要建一个iOS应用使用cocos2d的2,和我使用SimpleAudioEngine起到一些效果。

有没有办法测序多个声音在previous声后播放完成后?

例如在我的code:

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

在此code运行时, yay.wav youDidIt.wav 在完全相同的打时间,超过对方。

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

如果不SimpleAudioEngine,有没有与AudioToolbox的方式,还是其他什么东西?

感谢您!

== 更新 ==

我想我会使用AVFoundation用这种方法去:

  AVPlayerItem * Sound1例子= [[AVPlayerItem页头] initWithURL:[NSURL fileURLWithPath:[一个NSBundle mainBundle] pathForResource:@耶ofType:@WAV]]];
AVPlayerItem * SOUND2 = [[AVPlayerItem页头] initWithURL:[NSURL fileURLWithPath:[一个NSBundle mainBundle] pathForResource:@YouDidItofType:@WAV]]];
AVQueuePlayer *玩家= [[AVQueuePlayer页头] initWithItems:[NSArray的arrayWithObjects:Sound1例子,SOUND2,无]];
[播放器播放];


解决方案

这很简单,用的Cocos2D 2.1测试:


  1. 在创建一个SimpleAudioEngine.h物业durationInSeconds:

      @属性(只读)浮durationInSeconds;


  2. 合成它们:

      @synthesize durationInSeconds;


  3. 修改playEffect是这样的:

       - (ALuint)playEffect:(* NSString的)文件路径间距:(浮点32)沥青锅:(浮点32)锅增益:(浮点32){增益
        INT soundId = [bufferManager bufferForFile:文件路径创建:YES];
        如果(soundId!= kCDNoBuffer){
            durationInSeconds = [soundEngine bufferDurationInSeconds:soundId];
            返回[soundEngine playSound:soundId sourceGroupId:0间距:间距锅:锅增益:增益循环:假];
        }其他{
            返回CD_MUTE;
        }
    }


  4. 正如你所看到的,我从bufferManager的结果直接插入从soundEngine bufferDurationInSeconds durationInSeconds值。


  5. 现在,只要求值[SimpleAudioEngine sharedEngine] .durationInSeconds和你有浮动持续此声音秒。


  6. 把一个定时器这个秒在此之后,发挥你的声音的下一个迭代或将标志OFF或东西。


I'm building an iOS app with cocos2d 2, and I'm using SimpleAudioEngine to play some effects.

Is there a way to sequence multiple sounds to be played after the previous sound is complete?

For example in my code:

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

When this code is run, yay.wav and youDidIt.wav play at the exact same time, over each other.

I want yay.wav to play and once complete, youDidIt.wav to play.

If not with SimpleAudioEngine, is there a way with AudioToolbox, or something else?

Thank you!

== UPDATE ==

I think I'm going to go with this method using 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];
解决方案

It's simple, tested with Cocos2D 2.1:

  1. Creates a property durationInSeconds on SimpleAudioEngine.h :

    @property (readonly) float durationInSeconds;
    

  2. Synthesize them :

    @synthesize durationInSeconds;
    

  3. Modify playEffect like this :

    -(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. As you can see, I inserted durationInSeconds value directly from soundEngine bufferDurationInSeconds from the result of bufferManager.

  5. Now, only ask for the value [SimpleAudioEngine sharedEngine].durationInSeconds and you've got the float duration in seconds of this sound.

  6. Put a timer this seconds and after this, play the next iteration of your sound or put the flag OFF or something.

这篇关于播放声音序列与SimpleAudioEngine的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-18 15:36