码迷,mamicode.com
首页 > 其他好文 > 详细

cocos2d 播放音乐

时间:2014-09-24 14:51:46      阅读:226      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   io   os   ar   div   sp   on   

#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 instance
bool 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 播放音乐

标签:style   blog   color   io   os   ar   div   sp   on   

原文地址:http://www.cnblogs.com/yufenghou/p/3990481.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!