how to loop a sound effect in Simple Audio Engine

1.7k views Asked by At

I have searched extensively on how to loop a sound effect with simple audio engine, but haven't made much progress apart from hello with looping sfx on the cocos2d forum which has several issues. How can I loop a sound effect in Simple Audio Engine?

2

There are 2 answers

0
mattblessed On

You would need to edit the SimpleAudioEngine

add this to SimpleAudioEngine.h

-(int) playEffect:(NSString*) file loop:(BOOL) loop;

add this method to SimpleAudioEngine.m

-(int) playEffect:(NSString*) file loop:(BOOL) loop
{
    int handle = [[SimpleAudioEngine sharedEngine] playEffect:file];
    if (loop) {
        alSourcei(handle, AL_LOOPING, 1);
    }
    return handle;
}

to loop sound effects or music simply do this

ALuint yourSoundALuint = [[SimpleAudioEngine sharedEngine] playEffect:@"yourSound.caf" loop:YES];

and to stop the looping music when necessary

[[SimpleAudioEngine sharedEngine] stopEffect:yourSoundALuint]
0
S.H. On

For Sound Effect Infinite Looping until you want it to stop. This is how you do it without messing up anything else.

Make Sure you save the CDSoundSource strong reference in the class or object that is using it so that way you are able to stop it whenever you want.

Since its a unique ID that identifies that sound effect, and this way you can have multiple instances of this sound effect running. For example: when you need 2 helicopters and each with its own infinite engine sounds.

//Create SFX
-(CDSoundSource*)playInfiniteHeliPad
{
    CDSoundSource *loopSound = [[SimpleAudioEngine sharedEngine] soundSourceForFile:@"Sound.m4a"];
    loopSound.looping = YES;
    loopSound.gain = 0.5f;//Volume
    [loopSound play];
   return loopSound; //Return the CDSoundSourse
}


//Stop Infinite loop SFX.
-(void)stopInfiniteHeliPad:(CDSoundSource*)loopSound {
    [loopSound stop];
}

This is easy to implement and require no modification to root files from Cocos2Denshion