[Cocos2D-x For WP8]CocosDenshion音頻播放

    Cocos2D-x的音頻分爲長時間的背景音樂和短的音效兩種,咱們能夠經過SimpleAudioEngine::sharedEngine()方法來獲取音頻播放的引擎,而後調用對音頻相關的操做方法就能夠了,那麼這個是很是簡單的。函數

    在Cocos2D-x For WP8裏面的要使用音頻播放的API,咱們須要把CocosDenshion這個項目添加到咱們的遊戲項目裏面去,而後添加引用。以下圖所示:this

    須要注意的是若是發如今編譯的時候會出現下面的錯誤,那麼一般是由於沒有添加CocosDenshionWindowsPhone.lib連接庫。spa

錯誤 306 error LNK2019: unresolved external symbol "__declspec(dllimport) public: static class CocosDenshion::SimpleAudioEngine * __cdecl CocosDenshion::SimpleAudioEngine::sharedEngine(void)" (__imp_?sharedEngine@SimpleAudioEngine@CocosDenshion@@SAPAV12@XZ) referenced in function __unwind$26 F:\coco2d\cocos2dx-win8-wp8_v2\cocos2dx-win8-wp8_v2\HelloWorld\HelloWorldScene.obj HelloWorld3d

    那麼咱們就須要在VS2012上經過這個路徑( Project > Properties > Configuration Properties > Linker > Input > Additional Dependencies)來添加CocosDenshionWindowsPhone.lib連接庫, 以下圖所示:code

示例代碼:blog

class TestLayer : public cocos2d::CCLayer
{
public:
    TestLayer(void);
    ~TestLayer(void);
    void menuCallback(cocos2d::CCObject * pSender);
    virtual void ccTouchesMoved(cocos2d::CCSet *pTouches, cocos2d::CCEvent *pEvent);
    virtual void ccTouchesBegan(cocos2d::CCSet *pTouches, cocos2d::CCEvent *pEvent);

private:
    cocos2d::CCMenu* m_pItmeMenu;
    cocos2d::CCPoint m_tBeginPos;
    int m_nTestCount;
    unsigned int m_nSoundId;
};

TestLayer::TestLayer()
{
        std::string testItems[] = {
        "play background music",
        "stop background music",
        "pause background music",
        "resume background music",
        "rewind background music",
        "is background music playing",
        "play effect",
        "play effect repeatly",
        "stop effect",
        "unload effect",
        "add background music volume",
        "sub background music volume",
        "add effects volume",
        "sub effects volume",
        "pause effect",
        "resume effect",
        "pause all effects",
        "resume all effects",
        "stop all effects"
    };
    //建立菜單欄
    m_pItmeMenu = CCMenu::create();
    CCSize s = CCDirector::sharedDirector()->getWinSize();
    m_nTestCount = sizeof(testItems) / sizeof(testItems[0]);
    //添加菜單欄項目和點擊的回掉函數
    for (int i = 0; i < m_nTestCount; ++i)
    {
        CCLabelTTF* label = CCLabelTTF::create(testItems[i].c_str(), "Arial", 24);
        CCMenuItemLabel* pMenuItem = CCMenuItemLabel::create(label, this, menu_selector(TestLayer::menuCallback));
        m_pItmeMenu->addChild(pMenuItem, i + 10000);
        pMenuItem->setPosition( CCPointMake( s.width / 2, (s.height - (i + 1) * LINE_SPACE) ));
    }
    //設置菜單欄的位置
    m_pItmeMenu->setContentSize(CCSizeMake(s.width, (m_nTestCount + 1) * LINE_SPACE));
    m_pItmeMenu->setPosition(CCPointZero);
    addChild(m_pItmeMenu);

    setTouchEnabled(true);

    //預加載背景音樂和音效
    SimpleAudioEngine::sharedEngine()->preloadBackgroundMusic( CCFileUtils::fullPathFromRelativePath(MUSIC_FILE) );
    SimpleAudioEngine::sharedEngine()->preloadEffect( CCFileUtils::fullPathFromRelativePath(EFFECT_FILE) );
    
    //設置默認的音量
    SimpleAudioEngine::sharedEngine()->setEffectsVolume(0.5);
    SimpleAudioEngine::sharedEngine()->setBackgroundMusicVolume(0.5);
}

TestLayer::~TestLayer()
{
}

void TestLayer::menuCallback(CCObject * pSender)
{
    //獲取調用回掉函數的菜單,獲取它的ZOrder,用於判斷是那個菜單項觸發的
    CCMenuItem* pMenuItem = (CCMenuItem *)(pSender);
    int nIdx = pMenuItem->getZOrder() - 10000;

    switch(nIdx)
    {
    // 播放背景音樂
    case 0:
        SimpleAudioEngine::sharedEngine()->playBackgroundMusic(std::string(CCFileUtils::fullPathFromRelativePath(MUSIC_FILE)).c_str(), true);
        break;
    // 中止背景音樂
    case 1:
        SimpleAudioEngine::sharedEngine()->stopBackgroundMusic();
        break;
    // 暫停背景音樂
    case 2:
        SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic();
        break;
    // 恢復背景音樂
    case 3:
        SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic();
        break;
    // 回放背景音樂
    case 4:
        SimpleAudioEngine::sharedEngine()->rewindBackgroundMusic();
        break;
    // 判斷背景音樂是否正在播放
    case 5:
        if (SimpleAudioEngine::sharedEngine()->isBackgroundMusicPlaying())
        {
            CCLOG("background music is playing");
        }
        else
        {
            CCLOG("background music is not playing");
        }
        break;
    // 播放音效
    case 6:
        m_nSoundId = SimpleAudioEngine::sharedEngine()->playEffect(std::string(CCFileUtils::fullPathFromRelativePath(EFFECT_FILE)).c_str());
        break;
    // 重複播放音效
    case 7:
        m_nSoundId = SimpleAudioEngine::sharedEngine()->playEffect(std::string(CCFileUtils::fullPathFromRelativePath(EFFECT_FILE)).c_str(), true);
        break;
    // 中止播放音效
    case 8:
        SimpleAudioEngine::sharedEngine()->stopEffect(m_nSoundId);
        break;
    // 移除音效
    case 9:
        SimpleAudioEngine::sharedEngine()->unloadEffect(std::string(CCFileUtils::fullPathFromRelativePath(EFFECT_FILE)).c_str());
        break;
        // 添加背景音樂音量
    case 10:
        SimpleAudioEngine::sharedEngine()->setBackgroundMusicVolume(SimpleAudioEngine::sharedEngine()->getBackgroundMusicVolume() + 0.1f);
        break;
        // 減小背景音樂音量
    case 11:
        SimpleAudioEngine::sharedEngine()->setBackgroundMusicVolume(SimpleAudioEngine::sharedEngine()->getBackgroundMusicVolume() - 0.1f);
        break;
        // 添加音效音量
    case 12:
        SimpleAudioEngine::sharedEngine()->setEffectsVolume(SimpleAudioEngine::sharedEngine()->getEffectsVolume() + 0.1f);
        break;
        // 減小背景音樂音量
    case 13:
        SimpleAudioEngine::sharedEngine()->setEffectsVolume(SimpleAudioEngine::sharedEngine()->getEffectsVolume() - 0.1f);
        break;
        //暫停音效播放
    case 14:
        SimpleAudioEngine::sharedEngine()->pauseEffect(m_nSoundId);
        break;
    case 15:
        //恢復音效播放
        SimpleAudioEngine::sharedEngine()->resumeEffect(m_nSoundId);
        break;
        //暫停全部音效播放
    case 16:
        SimpleAudioEngine::sharedEngine()->pauseAllEffects();
        break;
    case 17:
        //恢復全部音效播放
        SimpleAudioEngine::sharedEngine()->resumeAllEffects();
        break;
    case 18:
        //中止暫停全部音效播放
        SimpleAudioEngine::sharedEngine()->stopAllEffects();
        break;
    }
    
}

void TestLayer::ccTouchesBegan(CCSet *pTouches, CCEvent *pEvent)
{
    CCSetIterator it = pTouches->begin();
    CCTouch* touch = (CCTouch*)(*it);

    m_tBeginPos = touch->getLocationInView();    
    m_tBeginPos = CCDirector::sharedDirector()->convertToGL( m_tBeginPos );
}
//處理菜單欄的滑動
void TestLayer::ccTouchesMoved(CCSet *pTouches, CCEvent *pEvent)
{
    CCSetIterator it = pTouches->begin();
    CCTouch* touch = (CCTouch*)(*it);

    CCPoint touchLocation = touch->getLocationInView();    
    touchLocation = CCDirector::sharedDirector()->convertToGL( touchLocation );
    float nMoveY = touchLocation.y - m_tBeginPos.y;

    CCPoint curPos  = m_pItmeMenu->getPosition();
    CCPoint nextPos = ccp(curPos.x, curPos.y + nMoveY);
    CCSize winSize = CCDirector::sharedDirector()->getWinSize();
    if (nextPos.y < 0.0f)
    {
        m_pItmeMenu->setPosition(CCPointZero);
        return;
    }

    if (nextPos.y > ((m_nTestCount + 1)* LINE_SPACE - winSize.height))
    {
        m_pItmeMenu->setPosition(ccp(0, ((m_nTestCount + 1)* LINE_SPACE - winSize.height)));
        return;
    }

    m_pItmeMenu->setPosition(nextPos);
    m_tBeginPos = touchLocation;
}

運行效果:
遊戲

相關文章
相關標籤/搜索