cocos2dx3.1.1如何播放声音

1,加载

CocosDenshion::SimpleAudioEngine::sharedEngine()->preloadBackgroundMusic("music.mid");

CocosDenshion::SimpleAudioEngine::sharedEngine()->preloadEffect("pew-pew-lei.wav");

这两句,分别读取了背景音乐文件和音效文件

2,播放

CocosDenshion::SimpleAudioEngine::sharedEngine()->playBackgroundMusic("music.mid", true);

CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect("pew-pew-lei.wav");

这两句分别是播放音乐和音效

3,停止

CocosDenshion::SimpleAudioEngine::sharedEngine()->stopBackgroundMusic(false);

这句是停止播放音乐。

相关的还有暂停等接口,但是我在win32下测试暂停后无法恢复,不知是什么原因,大家自己进这个类的头文件看看,接口很少。

// 预先加载背景音乐
//CocosDenshion::SimpleAudioEngine::sharedEngine()->preloadBackgroundMusic("huahuo.mp3");  
//播放背景音乐
//CocosDenshion::SimpleAudioEngine::sharedEngine()->playBackgroundMusic("huahuo.mp3",true); 


程序不需要音乐时,需要调用SimpleAudioEngine::sharedEngine()->end();释放sharedEngine()。

 

其他播放音效的相关函数

SimpleAudioEngine::sharedEngine()->stopBackgroundMusic();//停止背景音乐,可以代一个布尔型参数,表示是否释放音乐文件
SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic();//暂停背景音乐
SimpleAudioEngine::sharedEngine()->rewindBackgroundMusic();//重头调用背景音乐
SimpleAudioEngine::sharedEngine()->isBackgroundMusicPlaying()//返回布尔型参数,是否在放着背景音乐
SimpleAudioEngine::sharedEngine()->setBackgroundMusicVolume(0.5);//设置音量0.0-1.0</span>

 

<span style="font-family:Microsoft YaHei;font-size:18px;">preloadEffect( );
playEffect();
stopEffect();//停止音效,可以选择单独停掉一个音效,通过创建时的m_nSoundId停止
stopAllEffects();//停止全部音效
pauseEffect(m_nSoundId);//暂停单个音效
resumeEffect(m_nSoundId);//重新开始音效
pauseAllEffects();//暂停全部音效
resumeAllEffects();//重新开始全部音效
setEffectsVolume(0.5);//设置音效音量
unloadEffect(std::string(CCFileUtils::fullPathFromRelativePath(EFFECT_FILE)).c_str());//卸载音效</span>

 

不懂的可以关注我的公众号,里面有你想要的(有问题公众号留言必回答)https://mp.weixin.qq.com/s?__biz=MzIyOTM4MDMxNw==&mid=2247483836&idx=2&sn=1e1d922b5f2511e550da712921992d32&chksm=e842d80edf35511810bc8e383992c2e6a38665e466328200265386ab16fc4601d4822badf546&token=573588601&lang=zh_CN#rd

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值