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[]);
//添加菜单栏项目和点击的回掉函数
for (int i = ; i < m_nTestCount; ++i)
{
CCLabelTTF* label = CCLabelTTF::create(testItems[i].c_str(), "Arial", );
CCMenuItemLabel* pMenuItem = CCMenuItemLabel::create(label, this, menu_selector(TestLayer::menuCallback));
m_pItmeMenu->addChild(pMenuItem, i + );
pMenuItem->setPosition( CCPointMake( s.width / , (s.height - (i + ) * LINE_SPACE) ));
}
//设置菜单栏的位置
m_pItmeMenu->setContentSize(CCSizeMake(s.width, (m_nTestCount + ) * 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() - ; switch(nIdx)
{
// 播放背景音乐
case :
SimpleAudioEngine::sharedEngine()->playBackgroundMusic(std::string(CCFileUtils::fullPathFromRelativePath(MUSIC_FILE)).c_str(), true);
break;
// 停止背景音乐
case :
SimpleAudioEngine::sharedEngine()->stopBackgroundMusic();
break;
// 暂停背景音乐
case :
SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic();
break;
// 恢复背景音乐
case :
SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic();
break;
// 回放背景音乐
case :
SimpleAudioEngine::sharedEngine()->rewindBackgroundMusic();
break;
// 判断背景音乐是否正在播放
case :
if (SimpleAudioEngine::sharedEngine()->isBackgroundMusicPlaying())
{
CCLOG("background music is playing");
}
else
{
CCLOG("background music is not playing");
}
break;
// 播放音效
case :
m_nSoundId = SimpleAudioEngine::sharedEngine()->playEffect(std::string(CCFileUtils::fullPathFromRelativePath(EFFECT_FILE)).c_str());
break;
// 重复播放音效
case :
m_nSoundId = SimpleAudioEngine::sharedEngine()->playEffect(std::string(CCFileUtils::fullPathFromRelativePath(EFFECT_FILE)).c_str(), true);
break;
// 停止播放音效
case :
SimpleAudioEngine::sharedEngine()->stopEffect(m_nSoundId);
break;
// 移除音效
case :
SimpleAudioEngine::sharedEngine()->unloadEffect(std::string(CCFileUtils::fullPathFromRelativePath(EFFECT_FILE)).c_str());
break;
// 添加背景音乐音量
case :
SimpleAudioEngine::sharedEngine()->setBackgroundMusicVolume(SimpleAudioEngine::sharedEngine()->getBackgroundMusicVolume() + 0.1f);
break;
// 减少背景音乐音量
case :
SimpleAudioEngine::sharedEngine()->setBackgroundMusicVolume(SimpleAudioEngine::sharedEngine()->getBackgroundMusicVolume() - 0.1f);
break;
// 添加音效音量
case :
SimpleAudioEngine::sharedEngine()->setEffectsVolume(SimpleAudioEngine::sharedEngine()->getEffectsVolume() + 0.1f);
break;
// 减少背景音乐音量
case :
SimpleAudioEngine::sharedEngine()->setEffectsVolume(SimpleAudioEngine::sharedEngine()->getEffectsVolume() - 0.1f);
break;
//暂停音效播放
case :
SimpleAudioEngine::sharedEngine()->pauseEffect(m_nSoundId);
break;
case :
//恢复音效播放
SimpleAudioEngine::sharedEngine()->resumeEffect(m_nSoundId);
break;
//暂停所有音效播放
case :
SimpleAudioEngine::sharedEngine()->pauseAllEffects();
break;
case :
//恢复所有音效播放
SimpleAudioEngine::sharedEngine()->resumeAllEffects();
break;
case :
//停止暂停所有音效播放
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 + )* LINE_SPACE - winSize.height))
{
m_pItmeMenu->setPosition(ccp(, ((m_nTestCount + )* LINE_SPACE - winSize.height)));
return;
} m_pItmeMenu->setPosition(nextPos);
m_tBeginPos = touchLocation;
}

运行效果:

[Cocos2D-x For WP8]CocosDenshion音频播放的更多相关文章

  1. iOS开发系列--音频播放、录音、视频播放、拍照、视频录制

    --iOS多媒体 概览 随着移动互联网的发展,如今的手机早已不是打电话.发短信那么简单了,播放音乐.视频.录音.拍照等都是很常用的功能.在iOS中对于多媒体的支持是非常强大的,无论是音视频播放.录制, ...

  2. HTML5 音频播放器-Javascript代码(短小精悍)

    直接上干货咯! //HTML5 音频播放器 lzpong 2015/01/19 var wavPlayer = function () { if(window.parent.wavPlayer) re ...

  3. IOS开发之简单音频播放器

    今天第一次接触IOS开发的UI部分,之前学OC的时候一直在模拟的使用Target-Action回调模式,今天算是真正的用了一次.为了熟悉一下基本控件的使用方法,和UI部分的回调,下面开发了一个特别简易 ...

  4. ios原声音频播放AVAudioSession 总结

    //音频播放/*英译:record:录音 */ 1 导入头文件#import<AVFoundation/AVFoundation.h>//AVAudioSession是一个单例模式.在IO ...

  5. 微信小程序-图片、录音、音频播放、音乐播放、视屏、文件

    图片: wx.chooseImage(OBJECT) 从本地相册选择图片或使用相机拍照. OBJECT参数说明: 注:文件的临时路径,在小程序本次启动期间可以正常使用,如需持久保存,需在主动调用 wx ...

  6. Android音频播放之SoundPool

    SoundPool 一.基本概念 在Android应用程序的开发过程中,经常需要播放多媒体文件,也许最先想到的会是MediaPlayer类了,该类提供了播放.暂停.停止及重复播放等功能性方法(该类位于 ...

  7. iOS开发----音频播放、录音、视频播放、拍照、视频录制

    随着移动互联网的发展,如今的手机早已不是打电话.发短信那么简单了,播放音乐.视频.录音.拍照等都是很常用的功能.在iOS中对于多媒体的支持是非常强大的,无论是音视频播放.录制,还是对麦克风.摄像头的操 ...

  8. 【jquery】一款不错的音频播放器——Amazing Audio Player

    前段时间分享了一款视频播放器,点击这里.今天介绍一款不错的音频播放器——Amazing Audio Player. 介绍: Amazing Audio Player 是一个使用很方便的 Windows ...

  9. Android 学习笔记多媒体技术之 AsyncTask+实现音频播放...

    PS:今天搞了一下如何实现音频播放...结果被坑了,看书上写的代码是挺简单的,但是有个函数就是死活没看懂,这真是受不了...最后才弄明白,原来是一个实现异步任务的一个类...这个类使用java.uti ...

随机推荐

  1. 设计模式学习之观察者模式(Observer,行为型模式)(7)

    1.观察者模式又叫做发布-订阅模式. 2.观察者模式定义了一种一对多的依赖关系,让多个观察者对象同时监听某一个主题对象.这个主题对象在状态发生变化时,会通知所有观察者对象,使它们能够自动更新自己. 3 ...

  2. 使用JDBC的addBatch()方法提高效率

    在批量更新SQL操作的时候建议使用addBatch,这样效率是高些,数据量越大越能体现出来 Statement接口里有两个方法:void     addBatch(String sql)将给定的 SQ ...

  3. fis3-postpackager-loader插件说明

    fis3-postpackager-loader 静态资源前端加载器,用来分析页面中使用的和依赖的资源(js或css), 并将这些资源做一定的优化后插入页面中.如把零散的文件合并. 注意 此插件做前端 ...

  4. Linux crontab 定时任务详解

    1.每小时执行一次脚本 * */1 * * * /etc/init.d/smb restart #不是所有的系统都支持“*/1”这种写法可以试试: 0 * * * * /etc/init.d/smb  ...

  5. 网上下载的CHM帮助文件打不开的解决办法。

    我的机器 装的是 Windows server 2008 操作系统.他的安全性比较高. 我在网上下载了一个 CHM 帮助文档.结果打不开. 现象: 打开时 ,提示 安全警告, 提示:来自Interne ...

  6. 对android录制的NV21视频数据进行旋转(90,180,270)与剪切

    android默认的视频采集格式是NV21,(属于YUV420) 在onPreviewFrame中传进来的byte[] data即为NV21格式. 旋转算法 对NV21进行顺时针旋转90度,180度和 ...

  7. Codeforces Round #354 (Div. 2)-C

    C. Vasya and String 题目链接:http://codeforces.com/contest/676/problem/C High school student Vasya got a ...

  8. node.js整理 02文件操作-常用API

    NodeJS不仅能做网络编程,而且能够操作文件. 拷贝 小文件拷贝 var fs = require('fs'); function copy(src, dst) { fs.writeFileSync ...

  9. js 的一点用法

     js 中的json对象,ajax返回数据dataType为json否则无法将数据转换成json对象 也就无法通过json字符串转换成对象object,那么他将始终是个字符串,也就无法进行 对象操作. ...

  10. redis 的使用 (基础, key操作, string类型操作)

    使用redis set 类型: 没有重复元素 list 链表类型 有重复累型 sort set 类型 没有重复元素 1.1 存储数据 读取数据 // 数据储存在 内存中 set name laowen ...