Take the 2-minute tour ×

Background: My background music is being played as a sound effect because I want to change the pitch between each repetition of the tune.

The Issue: While the background music is playing there's a lot of other short sound effects happening. After a certain number of sound effects play, my background music (also a sound effect) cuts out. The queue seems to cycle so that after say 50 sound effects have played, when the 51st plays, the 1st will be released whether it has completed or not.

Request for direction: There are two directions I can see possibly going with this issue. 1. Not play the background music as an effect and figure out how to change the pitch as background music instead of an effect 2. Some how make sure that the effect will be retained until it has completed.


share|improve this question
add comment

1 Answer

up vote 4 down vote accepted

The SimpleAudioEngine doesn't allow you to control channels, so it'll just play your sound effects in one of the channels it has allocated internally. Eventually all channels are playing audio. In that case, when a new audio file is to be played and all channels are already playing, SimpleAudioEngine will cancel one of the existing audio files. On occasion this will be your background music. Dang.

What you normally do to fix this is to assign (allocate) one audio channel specifically for the background music. No other audio will ever attempt to play audio on that channel. You will have to use the "regular" CocosDenshion API. It looks like you either need to use CDAudioManager or CDSoundEngine, but I have no experience with using channels with CocosDenshion.

Personally I never used CocosDenshion, whenever I needed more control I found the ObjectAL API to be easier to understand and it has excellent documentation. I made the following helper function for ObjectAL that plays a sound on a particular channel. I simply instantiated a ALChannelSource for my background music, and only played background music through that channel. The returned ALSoundSource even allows you to change pitch, pan, etc. while the audio is playing.

+(id<ALSoundSource>) playEffect:(NSString*)effect channel:(ALChannelSource*)channel loop:(bool)loop
    id<ALSoundSource> soundSource = nil;

    if (channel)
        ALBuffer* buffer = [[OALAudioSupport sharedInstance] bufferFromFile:effect];
        soundSource = [channel play:buffer loop:loop];

    return soundSource;

All other audio files I'm playing through the OALSimpleAudio class.

share|improve this answer
Ah, I see. SimpleAudioEngine is just a tad too simple, eh? Thanks for the comprehensive answer, I'll be giving it a go and will report back. –  Jason Pawlak Oct 15 '11 at 22:25
Where does OALAudioSupport come from? –  Jason Pawlak Oct 15 '11 at 23:46
you mean where you can get ObjectAL? Here: kstenerud.github.com/ObjectAL-for-iPhone –  LearnCocos2D Oct 16 '11 at 10:06
I was having some major issues with adding ObjectAL to my project. Your guidance on the channels was spot on and very helpful, though. I ended up implementing with CDAudioManager. Thanks. –  Jason Pawlak Oct 17 '11 at 3:24
@LearnCocos2D, that code doesn't seem to work anymore. The class OALAudioSupport would appear to have been removed from the latest stable release of ObjectAL. I've migrated GameSoundManager (that I originally got (I think) from a CocosDension demo app to use ObjectAL. It was pretty painless, though I found transitioning from one background track to another a little more problematic. I also note that running with ObjectAL, the sound can be negetively affected by communications between the device and the debugger on the host Mac. –  PKCLsoft Nov 28 '12 at 4:04
show 1 more comment

Your Answer


