Cocos2d-x游戏开发之音效使用
2014-03-01 16:27
501 查看
在我们的游戏开发之中,除了UI是重中之重以外,音乐同样是不可缺少的重要部分。
因为人有两只眼睛和耳朵。
现在先来看引入的库文件,总的来说比较简单。
首先引入的库文件,并使用命名空间:
下面是代码使用:
一、Cocos2d-x中的声音
Cocos2d-iphone中包含CocosDenshion库,从低到高提供三层接口:CDSoundEngine、CDAudioManager和SimpleAudioEngine,但整个库完全依赖于OpenAL来实现。由于在其他平台上无法提供CocosDenshiono的底层支持,所以只采用了最上层的SimpleAudioEngine类来实现跨平台的声音引擎,在使用上是十分简便的。
查看SimpleAudioEngine这个文件就可以知道其中的播放背景音乐和音效的API了。(文件位置如下图:)
注:在Cocos2dx引擎文件夹中的samples/Cpp/TestCpp/proj.ios 文件目录下有一个附带的例子,里面包含了绝大部分的例子介绍,很详细,是学习Cocos2dx的一个好demo集锦。
关于在Cocos2dx中SimpleAudioEngine声音引擎的使用例子----CocosDenshionTest
(1)关于声音文件的预加载,可以使得程序在游戏中的执行效率提高,但是不可以避免的也会增加内存的占用。
[cpp] view
plaincopy
// preload background music and effect
SimpleAudioEngine::sharedEngine()->preloadBackgroundMusic( MUSIC_FILE );
SimpleAudioEngine::sharedEngine()->preloadEffect( EFFECT_FILE );
其中的参数是声音文件的名称即可。查看这两个预加载方法可知,这些方法的实现中会帮助我们获取声音文件的全路径。
[cpp] view
plaincopy
void SimpleAudioEngine::preloadBackgroundMusic(const char* pszFilePath)
{
// Changing file path to full path
std::string fullPath = CCFileUtils::sharedFileUtils()->fullPathForFilename(pszFilePath);
static_preloadBackgroundMusic(fullPath.c_str());
}
void SimpleAudioEngine::playBackgroundMusic(const char* pszFilePath, bool bLoop)
{
// Changing file path to full path
std::string fullPath = CCFileUtils::sharedFileUtils()->fullPathForFilename(pszFilePath);
static_playBackgroundMusic(fullPath.c_str(), bLoop);
}
(2)注意在onExit函数中结束对SimpleAudioEngine的使用
[cpp] view
plaincopy
void CocosDenshionTest::onExit()
{
CCLayer::onExit();
SimpleAudioEngine::sharedEngine()->end();
}
一、Cocos2d-x中的声音
Cocos2d-iphone中包含CocosDenshion库,从低到高提供三层接口:CDSoundEngine、CDAudioManager和SimpleAudioEngine,但整个库完全依赖于OpenAL来实现。由于在其他平台上无法提供CocosDenshiono的底层支持,所以只采用了最上层的SimpleAudioEngine类来实现跨平台的声音引擎,在使用上是十分简便的。
查看SimpleAudioEngine这个文件就可以知道其中的播放背景音乐和音效的API了。(文件位置如下图:)
注:在Cocos2dx引擎文件夹中的samples/Cpp/TestCpp/proj.ios 文件目录下有一个附带的例子,里面包含了绝大部分的例子介绍,很详细,是学习Cocos2dx的一个好demo集锦。
关于在Cocos2dx中SimpleAudioEngine声音引擎的使用例子----CocosDenshionTest
(1)关于声音文件的预加载,可以使得程序在游戏中的执行效率提高,但是不可以避免的也会增加内存的占用。
[cpp] view
plaincopy
// preload background music and effect
SimpleAudioEngine::sharedEngine()->preloadBackgroundMusic( MUSIC_FILE );
SimpleAudioEngine::sharedEngine()->preloadEffect( EFFECT_FILE );
其中的参数是声音文件的名称即可。查看这两个预加载方法可知,这些方法的实现中会帮助我们获取声音文件的全路径。
[cpp] view
plaincopy
void SimpleAudioEngine::preloadBackgroundMusic(const char* pszFilePath)
{
// Changing file path to full path
std::string fullPath = CCFileUtils::sharedFileUtils()->fullPathForFilename(pszFilePath);
static_preloadBackgroundMusic(fullPath.c_str());
}
void SimpleAudioEngine::playBackgroundMusic(const char* pszFilePath, bool bLoop)
{
// Changing file path to full path
std::string fullPath = CCFileUtils::sharedFileUtils()->fullPathForFilename(pszFilePath);
static_playBackgroundMusic(fullPath.c_str(), bLoop);
}
(2)注意在onExit函数中结束对SimpleAudioEngine的使用
[cpp] view
plaincopy
void CocosDenshionTest::onExit()
{
CCLayer::onExit();
SimpleAudioEngine::sharedEngine()->end();
}
因为人有两只眼睛和耳朵。
现在先来看引入的库文件,总的来说比较简单。
首先引入的库文件,并使用命名空间:
#include "SimpleAudioEngine.h" using namespace CocosDenshion;
下面是代码使用:
//播放背景音乐,相对路径,放在resource下面 string music = "Audio/bgAudio.mp3"; SimpleAudioEngine::sharedEngine()->playBackgroundMusic(music.c_str(),true);
/**************************************************************************** Copyright (c) 2010-2012 cocos2d-x.org Copyright (c) 2010 Steve Oldmeadow http://www.cocos2d-x.org Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ****************************************************************************/ #ifndef _SIMPLE_AUDIO_ENGINE_H_ #define _SIMPLE_AUDIO_ENGINE_H_ #include <stddef.h> #include "Export.h" #include <typeinfo> #include <ctype.h> #include <string.h> namespace CocosDenshion { class TypeInfo { public: virtual long getClassTypeInfo() = 0; }; static inline unsigned int getHashCodeByString(const char *key) { unsigned int len = strlen(key); const char *end=key+len; unsigned int hash; for (hash = 0; key < end; key++) { hash *= 16777619; hash ^= (unsigned int) (unsigned char) toupper(*key); } return (hash); } /** @class SimpleAudioEngine @brief offer a VERY simple interface to play background music & sound effect */ class EXPORT_DLL SimpleAudioEngine : public TypeInfo { public: SimpleAudioEngine(); /** * @js NA * @lua NA */ ~SimpleAudioEngine(); /** * @js NA * @lua NA */ virtual long getClassTypeInfo() { return getHashCodeByString(typeid(CocosDenshion::SimpleAudioEngine).name()); } /** @brief Get the shared Engine object,it will new one when first time be called @js getInstance */ static SimpleAudioEngine* sharedEngine(); /** @brief Release the shared Engine object @warning It must be called before the application exit, or a memroy leak will be casued. @lua NA */ static void end(); /** @brief Preload background music @param pszFilePath The path of the background music file,or the FileName of T_SoundResInfo @js preloadMusic */ void preloadBackgroundMusic(const char* pszFilePath); /** @brief Play background music @param pszFilePath The path of the background music file,or the FileName of T_SoundResInfo @param bLoop Whether the background music loop or not @js playMusic */ void playBackgroundMusic(const char* pszFilePath, bool bLoop); /** @js playMusic */ void playBackgroundMusic(const char* pszFilePath) { this->playBackgroundMusic(pszFilePath, false); } /** @brief Stop playing background music @param bReleaseData If release the background music data or not.As default value is false @js stopMusic */ void stopBackgroundMusic(bool bReleaseData); /** @js stopMusic */ void stopBackgroundMusic() { this->stopBackgroundMusic(false); } /** @brief Pause playing background music @js pauseMusic */ void pauseBackgroundMusic(); /** @brief Resume playing background music @js resumeMusic */ void resumeBackgroundMusic(); /** @brief Rewind playing background music @js rewindMusic */ void rewindBackgroundMusic(); /** * @js willPlayMusic */ bool willPlayBackgroundMusic(); /** @brief Whether the background music is playing @return If is playing return true,or return false @js isMusicPlaying */ bool isBackgroundMusicPlaying(); // properties /** @brief The volume of the background music max value is 1.0,the min value is 0.0 @js getMusicVolume */ float getBackgroundMusicVolume(); /** @brief set the volume of background music @param volume must be in 0.0~1.0 @js setMusicVolume */ void setBackgroundMusicVolume(float volume); /** @brief The volume of the effects max value is 1.0,the min value is 0.0 */ float getEffectsVolume(); /** @brief set the volume of sound effecs @param volume must be in 0.0~1.0 */ void setEffectsVolume(float volume); // for sound effects /** @brief Play sound effect @param pszFilePath The path of the effect file,or the FileName of T_SoundResInfo @bLoop Whether to loop the effect playing, default value is false */ unsigned int playEffect(const char* pszFilePath, bool bLoop); unsigned int playEffect(const char* pszFilePath) { return this->playEffect(pszFilePath, false); } /** @brief Pause playing sound effect @param nSoundId The return value of function playEffect */ void pauseEffect(unsigned int nSoundId); /** @brief Pause all playing sound effect @param nSoundId The return value of function playEffect */ void pauseAllEffects(); /** @brief Resume playing sound effect @param nSoundId The return value of function playEffect */ void resumeEffect(unsigned int nSoundId); /** @brief Resume all playing sound effect @param nSoundId The return value of function playEffect */ void resumeAllEffects(); /** @brief Stop playing sound effect @param nSoundId The return value of function playEffect */ void stopEffect(unsigned int nSoundId); /** @brief Stop all playing sound effects */ void stopAllEffects(); /** @brief preload a compressed audio file @details the compressed audio will be decode to wave, then write into an internal buffer in SimpleaudioEngine */ void preloadEffect(const char* pszFilePath); /** @brief unload the preloaded effect from internal buffer @param[in] pszFilePath The path of the effect file,or the FileName of T_SoundResInfo */ void unloadEffect(const char* pszFilePath); }; } // end of namespace CocosDenshion #endif // _SIMPLE_AUDIO_ENGINE_H_
一、Cocos2d-x中的声音
Cocos2d-iphone中包含CocosDenshion库,从低到高提供三层接口:CDSoundEngine、CDAudioManager和SimpleAudioEngine,但整个库完全依赖于OpenAL来实现。由于在其他平台上无法提供CocosDenshiono的底层支持,所以只采用了最上层的SimpleAudioEngine类来实现跨平台的声音引擎,在使用上是十分简便的。
查看SimpleAudioEngine这个文件就可以知道其中的播放背景音乐和音效的API了。(文件位置如下图:)
注:在Cocos2dx引擎文件夹中的samples/Cpp/TestCpp/proj.ios 文件目录下有一个附带的例子,里面包含了绝大部分的例子介绍,很详细,是学习Cocos2dx的一个好demo集锦。
关于在Cocos2dx中SimpleAudioEngine声音引擎的使用例子----CocosDenshionTest
(1)关于声音文件的预加载,可以使得程序在游戏中的执行效率提高,但是不可以避免的也会增加内存的占用。
[cpp] view
plaincopy
// preload background music and effect
SimpleAudioEngine::sharedEngine()->preloadBackgroundMusic( MUSIC_FILE );
SimpleAudioEngine::sharedEngine()->preloadEffect( EFFECT_FILE );
其中的参数是声音文件的名称即可。查看这两个预加载方法可知,这些方法的实现中会帮助我们获取声音文件的全路径。
[cpp] view
plaincopy
void SimpleAudioEngine::preloadBackgroundMusic(const char* pszFilePath)
{
// Changing file path to full path
std::string fullPath = CCFileUtils::sharedFileUtils()->fullPathForFilename(pszFilePath);
static_preloadBackgroundMusic(fullPath.c_str());
}
void SimpleAudioEngine::playBackgroundMusic(const char* pszFilePath, bool bLoop)
{
// Changing file path to full path
std::string fullPath = CCFileUtils::sharedFileUtils()->fullPathForFilename(pszFilePath);
static_playBackgroundMusic(fullPath.c_str(), bLoop);
}
(2)注意在onExit函数中结束对SimpleAudioEngine的使用
[cpp] view
plaincopy
void CocosDenshionTest::onExit()
{
CCLayer::onExit();
SimpleAudioEngine::sharedEngine()->end();
}
一、Cocos2d-x中的声音
Cocos2d-iphone中包含CocosDenshion库,从低到高提供三层接口:CDSoundEngine、CDAudioManager和SimpleAudioEngine,但整个库完全依赖于OpenAL来实现。由于在其他平台上无法提供CocosDenshiono的底层支持,所以只采用了最上层的SimpleAudioEngine类来实现跨平台的声音引擎,在使用上是十分简便的。
查看SimpleAudioEngine这个文件就可以知道其中的播放背景音乐和音效的API了。(文件位置如下图:)
注:在Cocos2dx引擎文件夹中的samples/Cpp/TestCpp/proj.ios 文件目录下有一个附带的例子,里面包含了绝大部分的例子介绍,很详细,是学习Cocos2dx的一个好demo集锦。
关于在Cocos2dx中SimpleAudioEngine声音引擎的使用例子----CocosDenshionTest
(1)关于声音文件的预加载,可以使得程序在游戏中的执行效率提高,但是不可以避免的也会增加内存的占用。
[cpp] view
plaincopy
// preload background music and effect
SimpleAudioEngine::sharedEngine()->preloadBackgroundMusic( MUSIC_FILE );
SimpleAudioEngine::sharedEngine()->preloadEffect( EFFECT_FILE );
其中的参数是声音文件的名称即可。查看这两个预加载方法可知,这些方法的实现中会帮助我们获取声音文件的全路径。
[cpp] view
plaincopy
void SimpleAudioEngine::preloadBackgroundMusic(const char* pszFilePath)
{
// Changing file path to full path
std::string fullPath = CCFileUtils::sharedFileUtils()->fullPathForFilename(pszFilePath);
static_preloadBackgroundMusic(fullPath.c_str());
}
void SimpleAudioEngine::playBackgroundMusic(const char* pszFilePath, bool bLoop)
{
// Changing file path to full path
std::string fullPath = CCFileUtils::sharedFileUtils()->fullPathForFilename(pszFilePath);
static_playBackgroundMusic(fullPath.c_str(), bLoop);
}
(2)注意在onExit函数中结束对SimpleAudioEngine的使用
[cpp] view
plaincopy
void CocosDenshionTest::onExit()
{
CCLayer::onExit();
SimpleAudioEngine::sharedEngine()->end();
}
相关文章推荐
- 【Cocos2d-X开发学习笔记】第28期:游戏中音乐和音效的使用
- 【Cocos2d-X开发学习笔记】第28期:游戏中音乐和音效的使用
- 【iOS-Cocos2d(2.x) 游戏开发之一】自定义CCSprite中-(id) init 重写需注意& 2.x版本中注册触屏事件代码&简述cocos2d/x/Unity3D引擎使用感想!
- cocos2d-x 中的pvr.ccz压缩图片的使用方法【iOS游戏开发征文】
- 【iOS-cocos2d-X 游戏开发之六】使用Base64算法对Cocos2dX自带CCUserDefault游戏存储数据加密!
- 初探使用iOS 7 Sprite Kit与Cocos2d开发游戏的对比
- Cocos2D教程:使用SpriteBuilder和Cocos2D 3.x开发横版动作游戏——Part 2
- iphone游戏开发之cocos2d ( 八 )使用加速计(重力感应)控制精灵移动UIAcceleration
- 基于cocos2d-x的游戏可以完全使用lua开发吗?
- Cocos2d-x游戏开发之CCLabelTTF标签详解之Create方法(设置自动换行的标签)和换行符的巧妙使用(十六)(高级篇) (转)
- 【Cocos2d-X开发学习笔记】第26期:游戏背景之滚动地图背景(CCParallaxNode)的使用
- 【iOS-cocos2d-X 游戏开发之六】使用Base64算法对Cocos2dX自带CCUserDefault游戏存储数据编码!
- iphone游戏开发之cocos2d ( 九 )使用Zwoptex创建纹理图册
- [ IOS-Cocos2d-x (Box2d)游戏开发之四] -Cocos2d-x for android 使用Box2d报错解决
- 【Cocos2d游戏开发之十七】灵活使用精灵可视区域(TextureRect)与锚点(anchorPoint)
- 【iOS-Cocos2d游戏开发之十二】浅析使用C++/C/OC进行iOS游戏混编出现“failed with exit”问题与小结;
- 【iOS-Cocos2d游戏开发之十四】音频/音效/视频播放(利用Cocos2D-iPhone-Extensions嵌入Cocos2d进行视频播放!)
- 初探使用iOS 7 Sprite Kit与Cocos2d开发游戏的对比(一家之言)
- Cocos2d-x游戏开发之SimpleAudioEngine 音乐音效
- 使用 Cocos2d-x 3.1.1 创建 Windows Phone 8 游戏开发环境