首页 > 代码库 > cocos2d 播放音乐

cocos2d 播放音乐

#include "HelloWorldScene.h"#include "SimpleAudioEngine.h"  #define EFFECT_FILE   "effect1.wav"#define MUSIC_FILE    "background.mp3"USING_NS_CC;using namespace CocosDenshion;CCScene* HelloWorld::scene(){    // ‘scene‘ is an autorelease object    CCScene *scene = CCScene::create();    // ‘layer‘ is an autorelease object    HelloWorld *layer = HelloWorld::create();    // add layer as a child to scene    scene->addChild(layer);    // return the scene    return scene;}// on "init" you need to initialize your instancebool HelloWorld::init(){    //////////////////////////////    // 1. super init first    if ( !CCLayer::init() )    {        return false;    }    SimpleAudioEngine::sharedEngine()->preloadBackgroundMusic( MUSIC_FILE );    SimpleAudioEngine::sharedEngine()->preloadEffect( EFFECT_FILE );    SimpleAudioEngine::sharedEngine()->setEffectsVolume(0.5);    SimpleAudioEngine::sharedEngine()->setBackgroundMusicVolume(0.5);    CCLabelTTF *abelttf=CCLabelTTF::create("play","Moncao", 48);    CCMenuItemLabel *item=CCMenuItemLabel::create(abelttf,this, menu_selector(HelloWorld::menuCallback));    CCMenu *menu=CCMenu::create(item,NULL);    menu->setPosition(ccp(100,100));    this->addChild(menu);    return true;}void HelloWorld::menuCallback(CCObject* pSender){    SimpleAudioEngine::sharedEngine()->playBackgroundMusic(MUSIC_FILE);    //SimpleAudioEngine::sharedEngine()->playBackgroundMusic(MUSIC_FILE, true);    //SimpleAudioEngine::sharedEngine()->stopBackgroundMusic();    //SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic();    //SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic();    //SimpleAudioEngine::sharedEngine()->rewindBackgroundMusic();    /*    if (SimpleAudioEngine::sharedEngine()->isBackgroundMusicPlaying())    {        CCLOG("background music is playing");    }    else    {        CCLOG("background music is not playing");    }    */    m_nSoundId = SimpleAudioEngine::sharedEngine()->playEffect(EFFECT_FILE);    //m_nSoundId = SimpleAudioEngine::sharedEngine()->playEffect(EFFECT_FILE, true);    //SimpleAudioEngine::sharedEngine()->stopEffect(m_nSoundId);    //SimpleAudioEngine::sharedEngine()->unloadEffect(EFFECT_FILE);    //SimpleAudioEngine::sharedEngine()->setBackgroundMusicVolume(SimpleAudioEngine::sharedEngine()->getBackgroundMusicVolume() + 0.1f);    //SimpleAudioEngine::sharedEngine()->setBackgroundMusicVolume(SimpleAudioEngine::sharedEngine()->getBackgroundMusicVolume() - 0.1f);    //SimpleAudioEngine::sharedEngine()->setEffectsVolume(SimpleAudioEngine::sharedEngine()->getEffectsVolume() + 0.1f);    //SimpleAudioEngine::sharedEngine()->setEffectsVolume(SimpleAudioEngine::sharedEngine()->getEffectsVolume() - 0.1f);    //SimpleAudioEngine::sharedEngine()->pauseEffect(m_nSoundId);    //SimpleAudioEngine::sharedEngine()->resumeEffect(m_nSoundId);    //SimpleAudioEngine::sharedEngine()->pauseAllEffects();    //SimpleAudioEngine::sharedEngine()->resumeAllEffects();    //SimpleAudioEngine::sharedEngine()->stopAllEffects();}

 

cocos2d 播放音乐