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

    Cocos2D-x的音频分为长时间的背景音乐和短的音效两种,我们可以通过SimpleAudioEngine::sharedEngine()方法来获取音频播放的引擎,然后调用对音频相关的操作方法就可以了,那么这个是非常简单的。

    在Cocos2D-x For WP8里面的要使用音频播放的API,我们需要把CocosDenshion这个项目添加到我们的游戏项目里面去,然后添加引用。如下图所示:

    需要注意的是如果发现在编译的时候会出现下面的错误,那么通常是因为没有添加CocosDenshionWindowsPhone.lib链接库。

错误 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 HelloWorld

    那么我们就需要在VS2012上通过这个路径( Project > Properties > Configuration Properties > Linker > Input > Additional Dependencies)来添加CocosDenshionWindowsPhone.lib链接库, 如下图所示:

示例代码:

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;
}

运行效果:

posted on 2013-08-31 16:46  linzheng  阅读(2606)  评论(0编辑  收藏  举报

导航