Search code examples
objective-ccocos2d-iphonesimpleaudioengine

Detect whether a sound effect is currently playing in SimpleAudioEngine


I want to detect whether [SimpleAudioEngine sharedEngine] is currently playing any effect. For Background music there is a method that gives you the information whether background music is playing:

[[SimpleAudioEngine sharedEngine] isBackgroundMusicPlaying];

Does something similar exist for sound effects? If not how else can I detect whether I am already playing an effect?


Solution

  • SimpleAudioEngine Doesn't have a method like isBackgroundMusicPlaying for effects, but you can store a BOOL called isPlaying and use CDSoundSource

    CDSoundSource* currentSound = [[CDAudioManager sharedManager] audioSourceForChannel:kASC_Right];
    [currentSound load:audioFile];
    currentSound.delegate = self;
    currentSound.backgroundMusic = NO;
    isPlaying = YES;
    [currentSound play];
    

    Then implement the callback:

    - (void) cdAudioSourceDidFinishPlaying:(CDLongAudioSource *) audioSource {
        isPlaying = NO;
    }
    

    I don't know exactly if that's the correct way to initialise the CDSoundSource since I've stolen shamelessly the code from this topic . Maybe you should take a look at the CDAudioManager Class Reference

    Hope this helps to point you in the right direction.