首页 > 代码库 > 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 播放音乐
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。