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.
At the end, I finally find the problem and fix it.
you need modify the init function in CDAudioManager.m file
add code:
after line:
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...