Reputation: 2853
So I saw other game examples and saw how the games have this feature that the sound paused when pause button was pressed, and when resume, the sound resume exactly where it was.
Found the answer myself.
Upvotes: 0
Views: 418
Reputation: 2853
So I did some research and found this awesome fix. Well not a fix, but an addition to the code. More importantly, it does work!
In CocosDenshion that include SimpleAudioEngine to ease out the audio problem of your games you need to edit the file to be able to PAUSE your sound effects.
This is the code you need to insert in CDSoundEngine class which is inside SimpleAudioEngine class inside CocosDenshion class. Copy it Exactly like below:
- (void)pauseAllSounds
{
for (int i=0; i < sourceTotal_; i++)
{
ALint state;
alGetSourcei(_sources[i].sourceId, AL_SOURCE_STATE, &state);
if(state == AL_PLAYING)
{
alSourcePause(_sources[i].sourceId);
}
}
alGetError();
}
- (void)resumeAllSounds
{
for (int i=0; i < sourceTotal_; i++)
{
ALint state;
alGetSourcei(_sources[i].sourceId, AL_SOURCE_STATE, &state);
if(state == AL_PAUSED)
{
alSourcePlay(_sources[i].sourceId);
}
}
alGetError();
}
The following code needs to be added in SimpleAudioEngine class to be able to use the method directly in your cocos2d game code.
- (void)pauseAllEffects
{
[soundEngine pauseAllSounds];
}
- (void)resumeAllEffects
{
[soundEngine resumeAllSounds];
}
I used this code in my game and works perfectly.
DISCLAIMER: I DON'T CLAIM AT ANY TIME CREDIT FOR THIS FIX. THIS WAS FOUND BY ME IN THIS SITE: http://nial.me/2012/06/pausing-and-resuming-sound-effects-in-cocos2d/ HOWEVER, SINCE I DIDN'T SEE ANY SIMILAR SOLUTION TO THIS PROBLEM IN STACKOVERFLOW I PROVIDE IT.
Upvotes: 3