微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

cocos2dx基础篇——音乐音效SimpleAudioEngine

http://shahdza.blog.51cto.com/2410787/1545820/


【SimpleAudioEngine】

音乐音效是每个游戏中不可或缺的部分,一个好的声音会给玩家留下深刻的印象,当一听到游戏的声音,就会不自觉得说出游戏的名称来。就像《中国好声音》一样,笔者对那首《斑马,斑马》印象就非常深刻,当然唱这首歌的那个妹子也不错。

好了,不扯了,继续学习……


1、音乐音效介绍

声音分为两类:背景音乐、音效。

(1)背景音乐:一般是贯穿整个游戏的音乐,时间长、重复次数少。

(2)音效: 一般是很短的那种声音特效,如砍杀、爆炸、走路等,短暂但使用比较频繁。

cocos2dx是一个跨平台的游戏引擎,它支持多种声音格式(比如MP3、WAV等)。可是呢,对于不同的平台,声音的格式可不一定是统一的。这就需要大家根据不同的平台,选择合适的声音格式了。

根据两种声音的特点,其采用的声音格式也有所不同。以下列出各平台所推荐的声音格式(当然并不是全部支持的格式,如IOS中背景音乐除了MP3和CAF外,还可以使用AAC、AMR等等)。


背景音乐 音效
Win32 MP3、MID、WAV MID、WAV
Android OGG
IOS MP3、CAF CAF

由此可见,背景音乐最适合的选择是MP3格式。虽然音效也可以使用MP3格式,但是MP3是一种有损的压缩格式,而音效多数是短暂而快速的声响,有损的压缩方式可能导致有些采样音源消失,故音效不推荐使用MP3格式。而每个平台的音效格式均不相同,故需要根据不同平台,选用合适的格式。

2、SimpleAudioEngine

cocos2dx为我们提供了声音模块CocosDenshion。而其中有个SimpleAudioEngine类,是我们学习研究的对象。

使用SimpleAudioEngine时需要引入头文件和命名空间:

1
2
3
4
//
#include"SimpleAudioEngine.h"
using namespace CocosDenshion;
//

SimpleAudioEngine和CCDirector一样,也是一个单例类。说的通俗一点,它就是一个全局静态类。第一次调用时会创建一个全局静态对象,整个游戏的运行过程中会一直存在,全局都可以访问。

3、获取单例对象

SimpleAudioEngine的单例对象获取方法

//通过SimpleAudioEngine::sharedEngine()获得
static SimpleAudioEngine*sharedEngine();
4、背景音乐的函数

相关操作:预加载、播放、停止、暂停、恢复、重播,以及设置音量等。

4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
void
preloadBackgroundMusic( '音乐路径constchar*' ); //预加载
playBackgroundMusic( , bool bLoop= false //播放,是否循环,认不循环
stopBackgroundMusic( bReleaseData= //停止,是否释放音乐资源
pauseBackgroundMusic(); //暂停
resumeBackgroundMusic(); //恢复
rewindBackgroundMusic(); //重播
//返回是否将要播放背景音乐
willPlayBackgroundMusic();
//返回是否正在播放背景音乐
//注意:暂停也算正在播放,只有停止了才算未播放。
isBackgroundMusicPlaying();
//设置音量,取值范围0~1.0
//查看了内部源码,发现音量的设置没有实现,即音量永远是1.0
float getBackgroundMusicVolume();
setBackgroundMusicVolume( volume);
5、音效的函数

相关操作:预加载、播放、停止、暂停、恢复、卸载,以及设置音量等。

21
preloadEffect(
unsigned int playEffect( //播放,返回该音效的ID。是否循环
stopEffect(unsigned nSoundId); //停止指定ID的音效
stopAllEffects(); //停止所有音效
pauseEffect(unsigned //暂停指定ID的音效
pauseAllEffects(); //暂停所有音效
resumeEffect(unsigned //恢复指定ID的音效
resumeAllEffects(); //恢复所有音效
unloadEffect( const char *pszFilePath); //卸载音效资源
getEffectsVolume();
setEffectsVolume( 6、关于预加载

加载音乐和音效通常是一个耗时的过程,为了防止由即时加载产生的延迟导致实际播放与游戏不协调的现象发生,在播放音效和背景音乐之前,记得要预加载音乐文件preload。另外需要根据不同的平台,选择不同的预加载音乐格式。

具体操作如下:

其中MUSIC_FILE就是音乐文件相对Resources文件夹的相对路径下的文件名。

21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
//
/**
* 根据平台选择音乐音效的格式
*/
//音效文件
//Android平台只支持OGG的音效格式
#if(CC_TARGET_PLATFORM==CC_PLATFORM_ANDROID)
#defineEFFECT_FILE"music/effect2.ogg"
#elif(CC_TARGET_PLATFORM==CC_PLATFORM_MARMALADE)
#defineEFFECT_FILE"music/effect1.raw"
#else
#defineEFFECT_FILE"music/effect1.wav"
#endif
//音乐文件
#if(CC_TARGET_PLATFORM==CC_PLATFORM_WIN32)
#defineMUSIC_FILE"music/music.mid"
#elif(CC_TARGET_PLATFORM==CC_PLATFORM_BLACKBerry||CC_TARGET_PLATFORM==CC_PLATFORM_LINUX)
#defineMUSIC_FILE"music/background.ogg"
#elif(CC_TARGET_PLATFORM==CC_PLATFORM_WP8)
#defineMUSIC_FILE"music/background.wav"
#else
#defineMUSIC_FILE"music/background.mp3"
#endif
/**
* 预加载音乐音效
*/
//加载背景音乐
SimpleAudioEngine::sharedEngine()->preloadBackgroundMusic(MUSIC_FILE);
//加载音效
SimpleAudioEngine::sharedEngine()->preloadBackgroundMusic(EFFECT_FILE);
7、关于音量调节

我在测试调节音量大小的时候,惊人的发现:cocos2dx中的音量设置没有实现,即使你设置了音量大小也没用,它永远都是1.0。所以要想调节音量大小,还是通过设置系统的音量吧……

PS:在V3.x中已经实现了音量的调节。

源码如下:

19
SimpleAudioEngine::getBackgroundMusicVolume()
{
return 1.0;
}
SimpleAudioEngine::setBackgroundMusicVolume( volume)
{
}
SimpleAudioEngine::getEffectsVolume()
{
1.0;
}
SimpleAudioEngine::setEffectsVolume( volume)
{
}
8、使用技巧

(1)通过SimpleAudioEngine::sharedEngine()获取单例对象,然后调用相关函数

(2)当退出程序时,记得要通过如下函数来释放单例对象,释放所有声音资源。

3
SimpleAudioEngine::sharedEngine()->end()
(3)加载音乐和音效通常是一个耗时的过程,为了防止由即时加载产生的延迟导致实际播放与游戏不协调的现象发生,在播放音效和背景音乐之前,记得要预加载音乐文件preload。



代码实战】

代码来源于cocos2dx的官方项目TestCpp中。

1、资源文件

将音乐音效文件资源放在项目的“Resources\music”目录下。

资源来自官方项目TestCpp中。

2、引入头文件和命名空间

3、预加载音乐音效

4、创建控制音乐音效的菜单按钮

菜单按钮来控制音乐的播放、暂停、停止、恢复等。

35
36
37
38
39
40
41
42
43
44
45
46
//创建控制音乐音效的菜单按钮
std::stringtestItems[]={
"playbackgroundmusic" ottom:auto!important; float:none!important; left:auto!important; line-height:1.1em!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,
"stopbackgroundmusic" ottom:auto!important; float:none!important; left:auto!important; line-height:1.1em!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,
"pausebackgroundmusic" ottom:auto!important; float:none!important; left:auto!important; line-height:1.1em!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,
"resumebackgroundmusic" ottom:auto!important; float:none!important; left:auto!important; line-height:1.1em!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,
"rewindbackgroundmusic" ottom:auto!important; float:none!important; left:auto!important; line-height:1.1em!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,
"isbackgroundmusicplaying" ottom:auto!important; float:none!important; left:auto!important; line-height:1.1em!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,
"playeffect" ottom:auto!important; float:none!important; left:auto!important; line-height:1.1em!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,
"playeffectrepeatly" ottom:auto!important; float:none!important; left:auto!important; line-height:1.1em!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,
"stopeffect" ottom:auto!important; float:none!important; left:auto!important; line-height:1.1em!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,
"pauseeffect" ottom:auto!important; float:none!important; left:auto!important; line-height:1.1em!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,
"resumeeffect" ottom:auto!important; float:none!important; left:auto!important; line-height:1.1em!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,
"pausealleffects" ottom:auto!important; float:none!important; left:auto!important; line-height:1.1em!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,
"resumealleffects" ottom:auto!important; float:none!important; left:auto!important; line-height:1.1em!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,
"stopalleffects" ottom:auto!important; float:none!important; left:auto!important; line-height:1.1em!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,
"unloadeffect" ottom:auto!important; float:none!important; left:auto!important; line-height:1.1em!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,
"addbackgroundmusicvolume" ottom:auto!important; float:none!important; left:auto!important; line-height:1.1em!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,
"subbackgroundmusicvolume" ottom:auto!important; float:none!important; left:auto!important; line-height:1.1em!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,
"addeffectsvolume" ottom:auto!important; float:none!important; left:auto!important; line-height:1.1em!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,
"subeffectsvolume"
};
//创建菜单
Ccmenu*pMenu=Ccmenu::create();
pMenu->setContentSize(CCSizeMake(480,1000));
for ( i=0;i<19;++i)
{
cclabelTTF*label=cclabelTTF::create(testItems[i].c_str(), "Arial" ottom:auto!important; float:none!important; left:auto!important; line-height:1.1em!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,24);
CcmenuItemLabel*pMenuItem=CcmenuItemLabel::create(label, this ottom:auto!important; float:none!important; left:auto!important; line-height:1.1em!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,menu_selector(HelloWorld::menuCallback));
pMenuItem->setPosition(ccp(mysize.width/2,1000-(i+1)*50));
pMenu->addChild(pMenuItem,i);
}
//将菜单作为容器,放入滚动视图中
CCScrollView*scrollView=CCScrollView::create(CCSizeMake(480,320),pMenu);
scrollView->setDirection(kCCScrollViewDirectionVertical);
scrollView->setPosition(CCPointZero);
pMenu->setPosition(ccp(0,320-1000));
->addChild(scrollView);
5、编写菜单按钮回调函数

根据点击的菜单项Tag,来控制音乐音效。

46
47
48
49
50
51
52
53
54
55
HelloWorld::menuCallback(CCObject*sender)
{
//获取菜单按钮编号
idx=((CcmenuItem*)sender)->getTag();
switch (idx)
{
//音乐控制
case 0:SimpleAudioEngine::sharedEngine()->playBackgroundMusic(MUSIC_FILE,153)!important; background:none!important">true ); break ; //播放音乐,循环
1:SimpleAudioEngine::sharedEngine()->stopBackgroundMusic(); //停止音乐
2:SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic(); //暂停音乐
3:SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic(); //恢复音乐
4:SimpleAudioEngine::sharedEngine()->rewindBackgroundMusic(); //重播音乐
5:
if (SimpleAudioEngine::sharedEngine()->isBackgroundMusicPlaying()) //是否正在播放背景音乐
{
cclOG( "backgroundmusicisplaying" );
}
else
{
"backgroundmusicisnotplaying" );
}
;
//音效控制
6:m_soundID=SimpleAudioEngine::sharedEngine()->playEffect(EFFECT_FILE); //播放音效,不循环
7:m_soundID=SimpleAudioEngine::sharedEngine()->playEffect(EFFECT_FILE,0)!important; background:none!important">//播放音效,循环
8:SimpleAudioEngine::sharedEngine()->stopEffect(m_soundID); //停止指定ID的音效
9:SimpleAudioEngine::sharedEngine()->pauseEffect(m_soundID); //暂停指定ID的音效
10:SimpleAudioEngine::sharedEngine()->resumeEffect(m_soundID); //恢复指定ID的音效
11:SimpleAudioEngine::sharedEngine()->pauseAllEffects(); //暂停所有音效
12:SimpleAudioEngine::sharedEngine()->resumeAllEffects(); //恢复所有音效
13:SimpleAudioEngine::sharedEngine()->stopAllEffects(); //停止所有音效
14:SimpleAudioEngine::sharedEngine()->unloadEffect(EFFECT_FILE); //卸载音效
//音量控制
15:
SimpleAudioEngine::sharedEngine()->setBackgroundMusicVolume(SimpleAudioEngine::sharedEngine()->getBackgroundMusicVolume()+0.1f);
;
16:
SimpleAudioEngine::sharedEngine()->setBackgroundMusicVolume(SimpleAudioEngine::sharedEngine()->getBackgroundMusicVolume()-0.1f);
;
17:
SimpleAudioEngine::sharedEngine()->setEffectsVolume(SimpleAudioEngine::sharedEngine()->getEffectsVolume()+0.1f);
;
18:
SimpleAudioEngine::sharedEngine()->setEffectsVolume(SimpleAudioEngine::sharedEngine()->getEffectsVolume()-0.1f);
;
}
}
6、运行结果

声音不是视觉效果,所以自己实现代码后,玩耍去吧!

本文出自 “夏天的风博客,请务必保留此出处http://www.jb51.cc/article/p-usxxzuqa-wx.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


    本文实践自 RayWenderlich、Ali Hafizji 的文章《How To Create Dynamic Textures with CCRenderTexture in Cocos2D 2.X》,文中使用Cocos2D,我在这里使用Cocos2D-x 2.1.4进行学习和移植。在这篇文章,将会学习到如何创建实时纹理、如何用Gimp创建无缝拼接纹
Cocos-code-ide使用入门学习地点:杭州滨江邮箱:appdevzw@163.com微信公众号:HopToad 欢迎转载,转载标注出处:http://blog.csdn.netotbaron/article/details/424343991.  软件准备 下载地址:http://cn.cocos2d-x.org/download 2.  简介2.1         引用C
第一次開始用手游引擎挺激动!!!进入正题。下载资源1:从Cocos2D-x官网上下载,进入网页http://www.cocos2d-x.org/download,点击Cocos2d-x以下的Download  v3.0,保存到自定义的文件夹2:从python官网上下载。进入网页https://www.python.org/downloads/,我当前下载的是3.4.0(当前最新
    Cocos2d-x是一款强大的基于OpenGLES的跨平台游戏开发引擎,易学易用,支持多种智能移动平台。官网地址:http://cocos2d-x.org/当前版本:2.0    有很多的学习资料,在这里我只做为自己的笔记记录下来,错误之处还请指出。在VisualStudio2008平台的编译:1.下载当前稳
1.  来源 QuickV3sample项目中的2048样例游戏,以及最近《最强大脑》娱乐节目。将2048改造成一款挑战玩家对数字记忆的小游戏。邮箱:appdevzw@163.com微信公众号:HopToadAPK下载地址:http://download.csdn.net/detailotbaron/8446223源码下载地址:http://download.csdn.net/
   Cocos2d-x3.x已经支持使用CMake来进行构建了,这里尝试以QtCreatorIDE来进行CMake构建。Cocos2d-x3.X地址:https://github.com/cocos2d/cocos2d-x1.打开QtCreator,菜单栏→"打开文件或项目...",打开cocos2d-x目录下的CMakeLists.txt文件;2.弹出CMake向导,如下图所示:设置
 下载地址:链接:https://pan.baidu.com/s/1IkQsMU6NoERAAQLcCUMcXQ提取码:p1pb下载完成后,解压进入build目录使用vs2013打开工程设置平台工具集,打开设置界面设置: 点击开始编译等待编译结束编译成功在build文件下会出现一个新文件夹Debug.win32,里面就是编译
分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!http://www.captainbed.net前言上次用象棋演示了cocos2dx的基本用法,但是对cocos2dx并没有作深入的讨论,这次以超级马里奥的源代码为线索,我们一起来学习超级马里奥的实
1. 圆形音量button事实上作者的本意应该是叫做“电位计button”。可是我觉得它和我们的圆形音量button非常像,所以就这么叫它吧~先看效果:好了,不多解释,本篇到此为止。(旁白: 噗。就这样结束了?)啊才怪~我们来看看代码:[cpp] viewplaincopyprint?CCContro
原文链接:http://www.cnblogs.com/physwf/archive/2013/04/26/3043912.html为了进一步深入学习贯彻Cocos2d,我们将自己写一个场景类,但我们不会走的太远,凡是都要循序渐进,哪怕只前进一点点,那也至少是前进了,总比贪多嚼不烂一头雾水的好。在上一节中我们建