There has a bug with OpenAl or Cocos2dx

947 views Asked by At

play sfx for loop on the scene:

CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect("sfx_timesup.mp3", true);

set a bt to press play another sfx:

CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect("sfx_bt.mp3", false);

it will stop the loop sfx after 31 times press bt.

#include "HelloWorldScene.h"
#include "SimpleAudioEngine.h"
USING_NS_CC;

CCScene* HelloWorld::scene(){
    CCScene *scene = CCScene::create();
    HelloWorld *layer = HelloWorld::create();
    scene->addChild(layer);
    return scene;
}
bool HelloWorld::init(){
    if ( !CCLayer::init() ) {
        return false;
    }
    CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect("sfx_timesup.wav", true);

    CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();
    CCMenuItemImage *pCloseItem = CCMenuItemImage::create(
                                        "CloseNormal.png",
                                        "CloseSelected.png",
                                        this,
                                        menu_selector(HelloWorld::menuCloseCallback));
    pCloseItem->setPosition(ccp(visibleSize.width/2 ,visibleSize.height/2));
    pCloseItem->setScale(10);
    CCMenu* pMenu = CCMenu::create(pCloseItem, NULL);
    pMenu->setPosition(CCPointZero);
    this->addChild(pMenu, 1);
    return true;
}
void HelloWorld::menuCloseCallback(CCObject* pSender){
    CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect("sfx_bt.wav", false);
}

in the cocosdenshion.mm this line :

int sourceIndex = [self _getSourceIndexForSourceGroup:sourceGroupId];

can't recognize the index whether loop or playing.

After 31 times click the bt. it will loop the sound channel and end the loop effect.

2

There are 2 answers

0
zszen On

At the end, I finally find the problem and fix it.

you need modify the init function in CDAudioManager.m file

add code:

[soundEngine setSourceGroupNonInterruptible:0 isNonInterruptible:TRUE];

after line:

soundEngine = [[CDSoundEngine alloc] init];

default cocos2d sound engine don't check with looping or playing sfx. Now it will do it. Why me :< find so hard. Because game warning sfx stop strangely, and then find the sfx play problem, and then go into the black hole (sound engine) :< so sad...

1
Makalele On

This bug is still there in cocos2d-x 3.8 and I've just run at this :) I have a better fix for this:

in CocosDension.m in function playSound add this code:

for(int i = 0; i < sourceTotal_; i++){
    ALint state;
    ALuint source = _sources[i].sourceId;
    alGetSourcei(source, AL_SOURCE_STATE, &state);
    if (state != AL_PLAYING && state != AL_PAUSED) {
        sourceIndex = i;
        break;
    }
}

between:

int sourceIndex = [self _getSourceIndexForSourceGroup:sourceGroupId];//This method ensures sourceIndex is valid

and:

if (sourceIndex != CD_NO_SOURCE) {