cocos2d-x下播放声音SampleAudioEngine

本文详细介绍了Cocos2D-x在不同平台下支持的声音格式及使用方法,包括背景音乐与音效的预加载、播放、停止、控制等操作。重点突出了声音文件格式的选择和程序执行效率的提升。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.Cocos2D-x在不同平台下支持的声音,支持的格式如表:

 

不同平台下支持的背景音乐格式
平台名称支持类型
AndroidMP3、WAV、3GP
IOSMP3、CAF
Win32              MID、WAV

 

不同平台下支持的音效格式
平台名称支持类型
AndroidOGG最好,WAV
IOS              CAF
Win32MID、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();


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值