1.Cocos2D-x在不同平台下支持的声音,支持的格式如表:
平台名称 | 支持类型 |
Android | MP3、WAV、3GP |
IOS | MP3、CAF |
Win32 | MID、WAV |
平台名称 | 支持类型 |
Android | OGG最好,WAV |
IOS | CAF |
Win32 | MID、WAV |
如果要使用该类,在文件中添加include"SampleAudioEngine.h"和using namespace CocosDenshion;
使用挺简单,但是一定要注意声音文件的格式,一般试用应该在windows下,win32下用mid和wav才行。
SimpleAudioEngine::sharedEngine()->preloadBackgroundMusic("Sound//BGM01.mp3");
SimpleAudioEngine::sharedEngine()->setBackgroundMusicVolume(1.0);
SimpleAudioEngine::sharedEngine()->playBackgroundMusic("Sound//BGM01.mp3",true);
“Sound//BGM01.mp3”文件是放在Resources下,程序会自动找到Resources目录,所以只需要注明后面的路径即可。
【注意】:1.设置音量函数的参数值的区间是0.0-1.0之间,如果有大于1的值要除以100,如33/100;
2.预加载声音会使得程序在游戏中的执行效率提高,但是会增加内存的占用。
1.播放背景音乐
SimpleAudioEngine::sharedEngine()->playBackgroundMusic(std::string(CCFileUtils::sharedFileUtils()->fullPathFromRelativePath(MUSIC_FILE)).c_str(), true);
2.也可以判断目前有没有背景音乐
if (SimpleAudioEngine::sharedEngine()->isBackgroundMusicPlaying())
{
CCLOG("背景音乐正在播放");
}
else
{
CCLOG("没有背景音乐播放");
}
3.停止背景音乐
SimpleAudioEngine::sharedEngine()->stopBackgroundMusic();
4.暂停背景音乐
SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic();
5.恢复背景音乐
SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic();
6.重头调用背景音乐
SimpleAudioEngine::sharedEngine()->rewindBackgroundMusic();
音效部分
1.播放音效
m_nSoundId = SimpleAudioEngine::sharedEngine()->playEffect(std::string(CCFileUtils::sharedFileUtils()->fullPathFromRelativePath(EFFECT_FILE)).c_str());
2.重复播放音效
m_nSoundId = SimpleAudioEngine::sharedEngine()->playEffect(std::string(CCFileUtils::sharedFileUtils()->fullPathFromRelativePath(EFFECT_FILE)).c_str(), true);
3.停止播放音效
SimpleAudioEngine::sharedEngine()->stopEffect(m_nSoundId);
4.卸载音效
SimpleAudioEngine::sharedEngine()->unloadEffect(std::string(CCFileUtils::sharedFileUtils()->fullPathFromRelativePath(EFFECT_FILE)).c_str());
5.暂停音效
SimpleAudioEngine::sharedEngine()->pauseEffect(m_nSoundId);
6.恢复音效
SimpleAudioEngine::sharedEngine()->resumeEffect(m_nSoundId);
7.暂停所有音效
SimpleAudioEngine::sharedEngine()->pauseAllEffects();
8.恢复所有音效
SimpleAudioEngine::sharedEngine()->resumeAllEffects();
9.停止所有音效
SimpleAudioEngine::sharedEngine()->stopAllEffects();