【Cocos2D-X 游戏引擎】初窥门径(9)播放音乐/音效

Cocos2D对音乐/音效播放的封装已经很好了,下边是例子:

定义2个文件:

#define MUSIC_FILE        "LOLBgMusic.mp3"
#define EFFECT_FILE		 "Attack.wav"

在初始化的时候预加载音乐/音效文件:

//预加载
	CocosDenshion::SimpleAudioEngine::sharedEngine()->preloadBackgroundMusic(MUSIC_FILE);
	CocosDenshion::SimpleAudioEngine::sharedEngine()->preloadEffect( EFFECT_FILE );
	//设置音量
	CocosDenshion::SimpleAudioEngine::sharedEngine()->setEffectsVolume(0.5);
	CocosDenshion::SimpleAudioEngine::sharedEngine()->setBackgroundMusicVolume(0.5);

播放:

		//循环播放背景音乐,
		CocosDenshion::SimpleAudioEngine::sharedEngine()->playBackgroundMusic(MUSIC_FILE,true);
		//播放音效
		CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect(EFFECT_FILE);

其他相关函数:

// stop background music
SimpleAudioEngine::sharedEngine()->stopBackgroundMusic();
 // pause background music
 SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic();
// resume background music
SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic();
// rewind background music
SimpleAudioEngine::sharedEngine()->rewindBackgroundMusic();
// is background music playing
SimpleAudioEngine::sharedEngine()->isBackgroundMusicPlaying()
// add bakcground music volume
SimpleAudioEngine::sharedEngine()->setBackgroundMusicVolume(SimpleAudioEngine::sharedEngine()->getBackgroundMusicVolume() + 0.1f);


SimpleAudioEngine::sharedEngine()->pauseEffect(m_nSoundId);
SimpleAudioEngine::sharedEngine()->resumeEffect(m_nSoundId);
SimpleAudioEngine::sharedEngine()->pauseAllEffects();
SimpleAudioEngine::sharedEngine()->resumeAllEffects();
SimpleAudioEngine::sharedEngine()->stopAllEffects();




转载请注明出处: http://blog.csdn.net/Vestigge





评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值