cocos2dx基础篇——音乐音效SimpleAudioEngine
发布时间:2020-12-14 16:24:24 所属栏目:百科 来源:网络整理
导读:http://shahdza.blog.51cto.com/2410787/1545820/ 【SimpleAudioEngine】 音乐音效是每个游戏中不可或缺的部分,一个好的声音会给玩家留下深刻的印象,当一听到游戏的声音,就会不自觉得说出游戏的名称来。就像《中国好声音》一样,笔者对那首《斑马,斑马》
http://shahdza.blog.51cto.com/2410787/1545820/
//通过SimpleAudioEngine::sharedEngine()获得
static
SimpleAudioEngine*sharedEngine();
4、背景音乐的函数
|
void
preloadBackgroundMusic(
'音乐路径constchar*'
);
//预加载
playBackgroundMusic(
,
bool
bLoop=
false
//播放,是否循环,默认不循环
stopBackgroundMusic(
bReleaseData=
//停止,是否释放音乐资源
pauseBackgroundMusic();
//暂停
resumeBackgroundMusic();
//恢复
rewindBackgroundMusic();
//重播
//返回是否将要播放背景音乐
willPlayBackgroundMusic();
//返回是否正在播放背景音乐
//注意:暂停也算正在播放,只有停止了才算未播放。
isBackgroundMusicPlaying();
//设置音量,取值范围0~1.0
//查看了内部源码,发现音量的设置没有实现,即音量永远是1.0
float
getBackgroundMusicVolume();
setBackgroundMusicVolume(
volume);
5、音效的函数
|
preloadEffect(
unsigned
int
playEffect(
//播放,返回该音效的ID。是否循环
stopEffect(unsigned
nSoundId);
//停止指定ID的音效
stopAllEffects();
//停止所有音效
pauseEffect(unsigned
//暂停指定ID的音效
pauseAllEffects();
//暂停所有音效
resumeEffect(unsigned
//恢复指定ID的音效
resumeAllEffects();
//恢复所有音效
unloadEffect(
const
char
*pszFilePath);
//卸载音效资源
getEffectsVolume();
setEffectsVolume(
6、关于预加载
|
//
/**
* 根据平台选择音乐音效的格式
*/
//音效文件
//Android平台只支持OGG的音效格式
#if(CC_TARGET_PLATFORM==CC_PLATFORM_ANDROID)
#defineEFFECT_FILE"music/effect2.ogg"
#elif(CC_TARGET_PLATFORM==CC_PLATFORM_MARMALADE)
#defineEFFECT_FILE"music/effect1.raw"
#else
#defineEFFECT_FILE"music/effect1.wav"
#endif
//音乐文件
#if(CC_TARGET_PLATFORM==CC_PLATFORM_WIN32)
#defineMUSIC_FILE"music/music.mid"
#elif(CC_TARGET_PLATFORM==CC_PLATFORM_BLACKBERRY||CC_TARGET_PLATFORM==CC_PLATFORM_LINUX)
#defineMUSIC_FILE"music/background.ogg"
#elif(CC_TARGET_PLATFORM==CC_PLATFORM_WP8)
#defineMUSIC_FILE"music/background.wav"
#else
#defineMUSIC_FILE"music/background.mp3"
#endif
/**
* 预加载音乐音效
*/
//加载背景音乐
SimpleAudioEngine::sharedEngine()->preloadBackgroundMusic(MUSIC_FILE);
//加载音效
SimpleAudioEngine::sharedEngine()->preloadBackgroundMusic(EFFECT_FILE);
7、关于音量调节
|
SimpleAudioEngine::getBackgroundMusicVolume()
{
return
1.0;
}
SimpleAudioEngine::setBackgroundMusicVolume(
volume)
{
}
SimpleAudioEngine::getEffectsVolume()
{
1.0;
}
SimpleAudioEngine::setEffectsVolume(
volume)
{
}
8、使用技巧
|
SimpleAudioEngine::sharedEngine()->end()
(3)加载音乐和音效通常是一个耗时的过程,为了防止由即时加载产生的延迟导致实际播放与游戏不协调的现象发生,在播放音效和背景音乐之前,记得要预加载音乐文件preload。
|
//创建控制音乐音效的菜单按钮
std::stringtestItems[]={
"playbackgroundmusic"
"playeffect"
"addbackgroundmusicvolume"
};
//创建菜单
CCMenu*pMenu=CCMenu::create();
pMenu->setContentSize(CCSizeMake(480,1000));
for
(
i=0;i<19;++i)
{
CCLabelTTF*label=CCLabelTTF::create(testItems[i].c_str(),
"Arial"
pMenu->addChild(pMenuItem,i);
}
//将菜单作为容器,放入滚动视图中
CCScrollView*scrollView=CCScrollView::create(CCSizeMake(480,320),pMenu);
scrollView->setDirection(kCCScrollViewDirectionVertical);
scrollView->setPosition(CCPointZero);
pMenu->setPosition(ccp(0,320-1000));
->addChild(scrollView);
5、编写菜单按钮回调函数 |
HelloWorld::menuCallback(CCObject*sender)
{
//获取菜单按钮编号
idx=((CCMenuItem*)sender)->getTag();
switch
(idx)
{
//音乐控制
case
0:SimpleAudioEngine::sharedEngine()->playBackgroundMusic(MUSIC_FILE,153)!important; background:none!important">true
);
break
;
//播放音乐,循环
1:SimpleAudioEngine::sharedEngine()->stopBackgroundMusic();
//停止音乐
2:SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic();
//暂停音乐
3:SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic();
//恢复音乐
4:SimpleAudioEngine::sharedEngine()->rewindBackgroundMusic();
//重播音乐
5:
if
(SimpleAudioEngine::sharedEngine()->isBackgroundMusicPlaying())
//是否正在播放背景音乐
{
CCLOG(
"backgroundmusicisplaying"
);
}
else
{
"backgroundmusicisnotplaying"
);
}
;
//音效控制
6:m_soundID=SimpleAudioEngine::sharedEngine()->playEffect(EFFECT_FILE);
//播放音效,不循环
7:m_soundID=SimpleAudioEngine::sharedEngine()->playEffect(EFFECT_FILE,0)!important; background:none!important">//播放音效,循环
8:SimpleAudioEngine::sharedEngine()->stopEffect(m_soundID);
//停止指定ID的音效
9:SimpleAudioEngine::sharedEngine()->pauseEffect(m_soundID);
//暂停指定ID的音效
10:SimpleAudioEngine::sharedEngine()->resumeEffect(m_soundID);
//恢复指定ID的音效
11:SimpleAudioEngine::sharedEngine()->pauseAllEffects();
//暂停所有音效
12:SimpleAudioEngine::sharedEngine()->resumeAllEffects();
//恢复所有音效
13:SimpleAudioEngine::sharedEngine()->stopAllEffects();
//停止所有音效
14:SimpleAudioEngine::sharedEngine()->unloadEffect(EFFECT_FILE);
//卸载音效
//音量控制
15:
SimpleAudioEngine::sharedEngine()->setBackgroundMusicVolume(SimpleAudioEngine::sharedEngine()->getBackgroundMusicVolume()+0.1f);
;
16:
SimpleAudioEngine::sharedEngine()->setBackgroundMusicVolume(SimpleAudioEngine::sharedEngine()->getBackgroundMusicVolume()-0.1f);
;
17:
SimpleAudioEngine::sharedEngine()->setEffectsVolume(SimpleAudioEngine::sharedEngine()->getEffectsVolume()+0.1f);
;
18:
SimpleAudioEngine::sharedEngine()->setEffectsVolume(SimpleAudioEngine::sharedEngine()->getEffectsVolume()-0.1f);
;
}
}
6、运行结果 |