|   版权声明:本文为博主原创文章,未经博主允许不得转载。

音效简介:

1.1 在游戏开发的过程中除了华丽的界面,生动的动画之外,适当的音效也是重要的一部分

1.2 游戏中的声音分为两类,一类是音乐,播放时间较长,适合用作背景音乐。另一类是音效,播放时间较短,可以重复的播放,例如,子弹打出的声音,地鼠触动的声音。             
1.3 cocos2d-x集成了CocosDenshion音效引擎,可以方便的实现游戏音效。CocosDenshion提供了多个音效API,它们分别是CDSoundEngine,CDAudioManager和SimpleAudioEngine。前两个是比较底层的可以控制多个声音的播放,在游戏开发中我们一般使用SimpleAudioEngine就足够了。

1.4 使用SimpleAudioEngine比较的简单,只要添加头文件和命名空间就可以使用了。#include
"SimpleAudioEngine.h";即可使用。

1.5
在Cocos2d-x中不同的平台之下,所支持的音乐格式有些差异:

Cocos2d-x在不同平台下支持的背景音乐格式:

Android:  MP3,
WAV, 3GP

IOS: MP3, CAF

Win32:  MID,WAV

Cocos2d-x在不同平台下支持的音效音乐格式:

Android:   OGG, WAV

IOS: CAF

Win32:  MID,WAV

音乐不同平台的判别:

音效文件

#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)

        #define EFFECT_FILE "effect.ogg"

#elif (CC_TARGET_PLATFORM == CC_PLATFROM_MARMALADE)

        #define EFFECT_FILE "effect.raw"

#else

        #define EFFECT_FILE "effect.wav"

#endif // CC_PLATFORM_ANDROID

背景音乐文件

#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)

        #define MUSIC_FILE "music.mid"

#elif (CC_TARGET_PLATFROM == CC_PLATFORM_BLACKBERRY)

        #define MUSIC_FILE "music.ogg"

#else

        #define MUSIC_FILE "music.mp3"

#endif // CC_PLATFORM_WIN32

音效API中的重要函数:

 /** Returns a shared instance of the SimpleAudioEngine.*/
实例化
static SimpleAudioEngine* getInstance();
/** Release the shared Engine object.warning It must be called before the application exit, or it will lead to memory leaks.*/
static void end();
/** Preload background music.param filePath The path of the background music file.*/
预处理背景音乐问ujianjiang压缩格式的文件进行解压处理,如MP3解压为WAV
virtual void preloadBackgroundMusic(const char* filePath);
/** Play background music.param filePath The path of the background music file,or the FileName of T_SoundResInfo.param loop Whether the background music loop or not.*/
播放背景音乐,参数bLoop控制是否循环播放,默认为false
virtual void playBackgroundMusic(const char* filePath, bool loop = false);
/** Stop playing background music.param releaseData If release the background music data or not.As default value is false.*/
停止播放音乐
virtual void stopBackgroundMusic(bool releaseData = false);
/** Pause playing background music.*/
暂停播放背景音乐
virtual void pauseBackgroundMusic();
/** Resume playing background music.*/
继续播放背景音乐
virtual void resumeBackgroundMusic();
/** Rewind playing background music.*/
倒带
virtual void rewindBackgroundMusic();
/** Indicates whether any background music can be played or not.return <i>true</i> if background music can be played, otherwise <i>false</i>.*/
virtual bool willPlayBackgroundMusic();
/** Indicates whether the background music is playing.return <i>true</i> if the background music is playing, otherwise <i>false</i>.*/
判断背景音乐是否在播放
virtual bool isBackgroundMusicPlaying();
/** The volume of the background music within the range of 0.0 as the minimum and 1.0 as the maximum.*/
virtual float getBackgroundMusicVolume();
/** Set the volume of background music.param volume must be within the range of 0.0 as the minimum and 1.0 as the maximum.*/
设置背景音乐的音量大小
virtual void setBackgroundMusicVolume(float volume);
/** The volume of the effects within the range of 0.0 as the minimum and 1.0 as the maximum.*/
获得背景音乐的音量大小
virtual float getEffectsVolume();
/** Set the volume of sound effects.param volume must be within the range of 0.0 as the minimum and 1.0 as the maximum.*/
virtual void setEffectsVolume(float volume);
/** Play sound effect with a file path, pitch, pan and gain.
* @param filePath The path of the effect file.
* @param loop Determines whether to loop the effect playing or not. The default value is false.
* @param pitch Frequency, normal value is 1.0. Will also change effect play time.
* @param pan Stereo effect, in the range of [-1..1] where -1 enables only left channel.
* @param gain Volume, in the range of [0..1]. The normal value is 1.
* @return The sound id.
*
* @note Full support is under development, now there are limitations:
* - no pitch effect on Samsung Galaxy S2 with OpenSL backend enabled;
* - no pitch/pan/gain on win32.
*/
播放音频
virtual unsigned int playEffect(const char* filePath, bool loop = false,float pitch = 1.0f, float pan = 0.0f, float gain = 1.0f);
/** Pause playing sound effect.param soundId The return value of function playEffect.*/
暂停播放所有音效,参数SoundId是playEffect函数返回ID
virtual void pauseEffect(unsigned int soundId);
/** Pause all playing sound effect.*/
暂停播放所有音效
virtual void pauseAllEffects();
/** Resume playing sound effect.param soundId The return value of function playEffect.*/
继续播放音效,参数SoundId是playEffect函数返回ID
virtual void resumeEffect(unsigned int soundId);
/** Resume all playing sound effect.*/
继续播放所有音效
virtual void resumeAllEffects();
/** Stop playing sound effect.param soundId The return value of function playEffect.*/
停止播放所有音效,参数soundId是playEffect函数返回ID
virtual void stopEffect(unsigned int soundId);
/** Stop all playing sound effects.*/
停止播放所有音效
virtual void stopAllEffects();
/** Preload a compressed audio file.The compressed audio will be decoded to wave, then written into an internal buffer in SimpleAudioEngine.param filePath The path of the effect file.*/
预处理音频文件,将压缩格式的文件进行解压处理,如MP3解压为WAV
virtual void preloadEffect(const char* filePath);
/** Unload the preloaded effect from internal buffer.param filePath The path of the effect file.*/
virtual void unloadEffect(const char* filePath);

实例:

.h files

#ifndef _SOUNDTEST_SCENE_H_
#define _SOUNDTEST_SCENE_H_
//使用音效比较的简单我们只要包含托文件SimpleAudioEngine.h和命名空间CocosDenshion
#include "cocos2d.h"
#include "ui\CocosGUI.h"
#include "editor-support/cocostudio/CCSGUIReader.h"
#include "SimpleAudioEngine.h" //设置音乐格式的宏
#define BG_FILES "MainMenuMusic1.wav"
#define EFFECT_FILES "mole.wav"
USING_NS_CC;
using namespace cocos2d::ui;
using namespace CocosDenshion;
class soundTest : public cocos2d::Layer
{
private:
public:
static cocos2d::Scene* createScene();
virtual bool init();
//Player music
void testPlayBGMusic(Ref* snedef);
void testPlayEffMusic(Ref* snedef);
//Pause music
void testPauseBGMusic(Ref* snedef);
void testPauseEffMusic(Ref* snedef);
//Resume music
void testResumeBGMusic(Ref* snedef);
void testResumeEffMusic(Ref* snedef);
//Stop music
void testStopBGMusic(Ref* snedef);
void testStopEffMusic(Ref* snedef);
//CallBack function
void sliderEvent1(Ref* sendef, SliderEventType type);
void sliderEvent2(Ref* sendef, SliderEventType type);
CREATE_FUNC(soundTest);
};
#endif // _SOUNDTEST_SCENE_H_ .cpp files #include "SoundTest.h" Scene* soundTest::createScene()
{
auto scene = Scene::create();
auto layer = soundTest::create();
scene->addChild(layer);
return scene;
}
bool soundTest::init()
{
if (!Layer::init())
{
return false;
} Size size = Director::getInstance()->getWinSize();
Vec2 origin = Director::getInstance()->getVisibleOrigin(); SimpleAudioEngine::getInstance()->preloadBackgroundMusic(BG_FILES);
SimpleAudioEngine::getInstance()->preloadEffect(EFFECT_FILES); SimpleAudioEngine::getInstance()->setBackgroundMusicVolume(0.2);
SimpleAudioEngine::getInstance()->setEffectsVolume(0.2); auto item1 = MenuItemFont::create("Play bg", CC_CALLBACK_1(soundTest::testPlayBGMusic, this));
auto item2 = MenuItemFont::create("Play eff", CC_CALLBACK_1(soundTest::testPlayEffMusic, this)); auto item3 = MenuItemFont::create("Pause bg", CC_CALLBACK_1(soundTest::testPauseBGMusic, this));
auto item4 = MenuItemFont::create("Pause ef", CC_CALLBACK_1(soundTest::testPauseEffMusic, this)); auto item5 = MenuItemFont::create("Resume bg", CC_CALLBACK_1(soundTest::testResumeBGMusic, this));
auto item6 = MenuItemFont::create("Resume eff", CC_CALLBACK_1(soundTest::testResumeEffMusic, this)); auto item7 = MenuItemFont::create("Stop bg", CC_CALLBACK_1(soundTest::testStopBGMusic, this));
auto item8 = MenuItemFont::create("Stop eff", CC_CALLBACK_1(soundTest::testStopEffMusic, this)); auto menu1 = Menu::create(item1, item3, item5, item7, NULL);
menu1->setPosition(Vec2(100, size.height - 100));
menu1->alignItemsVertically();
this->addChild(menu1); auto menu2 = Menu::create(item2, item4, item6, item8, NULL);
menu2->setPosition(Vec2(300, size.height - 100));
menu2->alignItemsVertically();
this->addChild(menu2); Slider* slider1 = Slider::create(); slider1->loadBarTexture("sliderTrack.png");
slider1->loadSlidBallTextures("sliderThumb.png", "sliderThumb.png", "");
slider1->loadProgressBarTexture("sliderProgress.png");
slider1->setPosition(Vec2(200, 300));
slider1->addEventListenerSlider(this, sliderpercentchangedselector(soundTest::sliderEvent1));
this->addChild(slider1); Slider* slider2 = Slider::create(); slider2->loadBarTexture("sliderTrack.png");
slider2->loadSlidBallTextures("sliderThumb.png", "sliderThumb.png", "");
slider2->loadProgressBarTexture("sliderProgress.png");
slider2->setPosition(Vec2(size.width - 200, 10));
slider2->addEventListenerSlider(this, sliderpercentchangedselector(soundTest::sliderEvent1));
this->addChild(slider2); return true;
}
//设置调节音量大小条的回调函数
void soundTest::sliderEvent1(Ref* sendef, SliderEventType type)
{
if (type == SLIDER_PERCENTCHANGED)
{
Slider* slider = dynamic_cast<Slider*>(sendef);
int percent = slider->getPercent();
SimpleAudioEngine::getInstance()->setBackgroundMusicVolume(percent*0.1);
}
}
void soundTest::sliderEvent2(Ref* sendef, SliderEventType type)
{
if (type == SLIDER_PERCENTCHANGED)
{
Slider* slider = dynamic_cast<Slider*>(sendef);
int percent = slider->getPercent();
SimpleAudioEngine::getInstance()->setBackgroundMusicVolume(percent*0.1);
}
}
//开始播放音乐
void soundTest::testPlayBGMusic(Ref* sendef)
{
SimpleAudioEngine::getInstance()->playBackgroundMusic(BG_FILES, true);
}
//开始播放音效
void soundTest::testPlayEffMusic(Ref* sendef)
{
SimpleAudioEngine::getInstance()->playEffect(EFFECT_FILES);
}
//暂停播放的音乐
void soundTest::testPauseBGMusic(Ref* sendef)
{
SimpleAudioEngine::getInstance()->pauseBackgroundMusic();
}
//暂停播放所有的音效
void soundTest::testPauseEffMusic(Ref* sendef)
{
SimpleAudioEngine::getInstance()->pauseAllEffects();
}
//恢复播放的音乐
void soundTest::testResumeBGMusic(Ref* sendef)
{
SimpleAudioEngine::getInstance()->resumeBackgroundMusic();
}
//恢复播放所有的音效
void soundTest::testResumeEffMusic(Ref* sendef)
{
SimpleAudioEngine::getInstance()->resumeAllEffects();
}
//停止播放的音乐
void soundTest::testStopBGMusic(Ref* sendef)
{
SimpleAudioEngine::getInstance()->stopBackgroundMusic();
}
//停止播放所有的音效
void soundTest::testStopEffMusic(Ref* sendef)
{
SimpleAudioEngine::getInstance()->stopAllEffects();
}

Cocos2d-x之Sound的更多相关文章

  1. 【Cocos2d入门教程八】浅析Cocoss2d下的音频引擎及封装音频类

    Cocos2d-x提供了一个音频CocosDenshion引擎,CocosDenshion引擎可以独立于Cocos2d-x单独使用,CocosDenshion引擎本质上封装了OpenAL音频处理库.具 ...

  2. Cocos2D iOS之旅:如何写一个敲地鼠游戏(十一):完善游戏逻辑

    大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请告诉我,如果觉得不错请多多支持点赞.谢谢! hopy ;) 免责申明:本博客提供的所有翻译文章原稿均来自互联网,仅供学习交流 ...

  3. Cocos2D:塔防游戏制作之旅(十八)

    在Enemy.m的getDamaged:方法只给你添加如下1行(在if条件内): [theGame awardGold:200]; 现在运行游戏你将注意到你不能放置超出你资源金币的炮塔了.当然杀死敌人 ...

  4. 基于cocos2d开发的android小游戏——採花仙

    /*cocos 2d 已经成为了如今移动端游戏开发的强有力的工具,眼下主流游戏中多採用cocos 2d游戏引擎. 我也尝试了一下该引擎.我是用的是cocos2d-android,以后要移植到Cocos ...

  5. java sound初探

    网上关于java sound的正规资源讲解的非常好,本文不再给出示例,主要提供一些好的资源,并说说我的一些理解,用于形成对java sound的整体认识. 一.几个词汇 TTS:text-to-spe ...

  6. 小尝试一下 cocos2d

    好奇 cocos2d 到底是怎样一个框架,正好有个项目需要一个游戏框架,所以稍微了解了一下.小结一下了解到的情况. 基本概念 首先呢,因为 cocos2d 是基于 pyglet 做的,你完全可以直接用 ...

  7. TeamViewer 12.0.71503 Patch By.Sound

    TeamViewer - the All-In-One Software for Remote Support and Online Meetings - Remote control any com ...

  8. 采用cocos2d-x lua 制作数字滚动效果样例

    require "Cocos2d"require "Cocos2dConstants"local testscene = class("testsce ...

  9. Cocos2d 利用继承Draw方法制作可显示三维数据(宠物三维等)的三角形显示面板

    很久没有写博客了,这段时间比较忙,又是搬家又是做自己的项目,还有太多琐碎的事情缠身,好不容易抽出时间把最近自己做的一些简单例子记录一下. 在我的项目中,我需要一个显示面板来显示游戏中的一个三维数据,例 ...

随机推荐

  1. 如何做PPT

    如何做出更漂亮的PPT? 1.文字对齐,PPT字体最好使用微软雅黑,文档最好使用宋体 2.总分总的叙述形式 3.颜色最好使用冷色系,蓝色.灰色.灰蓝色等等 4.每段话中的重点内容标出特殊颜色 5.可使 ...

  2. [JavaScript深入系列]JavaScript深入之执行上下文栈(转载)

    顺序执行? 如果要问到 JavaScript 代码执行顺序的话,想必写过 JavaScript 的开发者都会有个直观的印象,那就是顺序执行,毕竟: var foo = function () { co ...

  3. CodeForces - 343D 树链剖分

    题目链接:http://codeforces.com/problemset/problem/343/D 题意:给定一棵n个n-1条边的树,起初所有节点权值为0,然后m个操作. 1 x:把x为根的子树的 ...

  4. rabbitmq必须应答

    当autoAck设置为true时,只要消息被消费者处理,不管成功与否,服务器都会删除该消息, 而当autoAck设置为false时,只有消息被处理,且反馈结果后才会删除 https://www.cnb ...

  5. Retrofit与RXJava整合(转)

    Retrofit 除了提供了传统的 Callback 形式的 API,还有 RxJava 版本的 Observable 形式 API.下面我用对比的方式来介绍 Retrofit 的 RxJava 版 ...

  6. shell 函数传递参数的几种方式

    1.最近总结了 shell 中 function 的传递变量的几种方式 1.传递单个变量 2.传递数组变量   #!/bin/bash   #trying to pass an variable.   ...

  7. 使用jstack排查线程问题

    以一个例子来演示排查服务器cpu占用率过高的问题. 准备 将下面的代码文件上传到服务器上,然后使用javac编译,并使用java命令将程序跑起来. public class JStackCase { ...

  8. SpringBoot集成H2database

    转载:https://blog.csdn.net/chenhao_c_h/article/details/80332260 h2database为我们提供了十分轻量,十分快捷方便的内嵌式数据库 H2是 ...

  9. 前端每日实战:47# 视频演示如何用纯 CSS 创作一个蝴蝶标本展示框

    效果预览 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/xzgZzQ 可交互视频教程 此视频 ...

  10. 分享学做的一个jsp注册页面

    分享一个自己学习时,用bootstrap,多方搜索做的注册页面,包括页面的非空验证.导入相关的bootstrap的js和css文件就可以了.背景很丑,可以自己换一个.后面进一步完善<( ̄︶ ̄)↗ ...