http://shahdza.blog.51cto.com/2410787/1545820/
|
背景音乐 | 音效 |
Win32 | MP3、MID、WAV | MID、WAV |
Android | OGG | |
IOS | MP3、CAF | CAF |
1
2
3
4
|
//通过SimpleAudioEngine::sharedEngine()获得
static
SimpleAudioEngine*sharedEngine();
4、背景音乐的函数
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
void
preloadBackgroundMusic(
'音乐路径constchar*'
);
//预加载
playBackgroundMusic(
,
bool
bLoop=
false
//播放,是否循环,默认不循环
stopBackgroundMusic(
bReleaseData=
//停止,是否释放音乐资源
pauseBackgroundMusic();
//暂停
resumeBackgroundMusic();
//恢复
rewindBackgroundMusic();
//重播
@H_987_301@
//返回是否将要播放背景音乐
willPlayBackgroundMusic();
//返回是否正在播放背景音乐
//注意:暂停也算正在播放,只有停止了才算未播放。
isBackgroundMusicPlaying();
//设置音量,取值范围0~1.0
//查看了内部源码,发现音量的设置没有实现,即音量永远是1.0
float
getBackgroundMusicVolume();
setBackgroundMusicVolume(
volume);
5、音效的函数
21
preloadEffect(
unsigned
int
playEffect(
//播放,返回该音效的ID。是否循环
stopEffect(unsigned
nSoundId);
//停止指定ID的音效
stopAllEffects();
//停止所有音效
pauseEffect(unsigned
//暂停指定ID的音效
pauseAllEffects();
//暂停所有音效
@H_987_301@
resumeEffect(unsigned
//恢复指定ID的音效
resumeAllEffects();
//恢复所有音效
unloadEffect(
const
char
*pszFilePath);
//卸载音效资源
getEffectsVolume();
setEffectsVolume(
6、关于预加载
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
//
/**
* 根据平台选择音乐音效的格式
*/
//音效文件
//Android平台只支持OGG的音效格式
#if(CC_TARGET_PLATFORM==CC_PLATFORM_ANDROID)
#defineEFFECT_FILE"music/effect2.ogg"
@H_987_301@
#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、关于音量调节
19
SimpleAudioEngine::getBackgroundMusicVolume()
{
return
1.0;
}
SimpleAudioEngine::setBackgroundMusicVolume(
volume)
{
}
@H_987_301@
SimpleAudioEngine::getEffectsVolume()
{
1.0;
}
SimpleAudioEngine::setEffectsVolume(
volume)
{
}
8、使用技巧
3
SimpleAudioEngine::sharedEngine()->end()
//创建控制音乐音效的菜单按钮
std::stringtestItems[]={
"playbackgroundmusic"
"stopbackgroundmusic"
"pausebackgroundmusic"
"resumebackgroundmusic"
"rewindbackgroundmusic"
"isbackgroundmusicplaying"
301@
"playeffect"
"playeffectrepeatly"
"stopeffect"
"pauseeffect"
"resumeeffect"
"pausealleffects"
"resumealleffects"
"stopalleffects"
"unloadeffect"
"addbackgroundmusicvolume"
"subbackgroundmusicvolume"
"addeffectsvolume"
"subeffectsvolume"
};
//创建菜单
CCMenu*pMenu=CCMenu::create();
pMenu->setContentSize(CCSizeMake(480,1000));
for
(
i=0;i<19;++i)
{
CCLabelTTF*label=CCLabelTTF::create(testItems[i].c_str(),
"Arial"
CCMenuItemLabel*pMenuItem=CCMenuItemLabel::create(label,
this
pMenuItem->setPosition(ccp(mysize.width/2,1000-(i+1)*50));
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);
HelloWorld::menuCallback(CCObject*sender)
{
idx=((CCMenuItem*)sender)->getTag();
switch
(idx)
@H_987_301@
{
//音乐控制
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:
{
CCLOG(
"backgroundmusicisplaying"
);
}
else
{
"backgroundmusicisnotplaying"
);
}
;
//音效控制
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:
@H_301_1268@
SimpleAudioEngine::sharedEngine()->setEffectsVolume(SimpleAudioEngine::sharedEngine()->getEffectsVolume()+0.1f);
;
18:
SimpleAudioEngine::sharedEngine()->setEffectsVolume(SimpleAudioEngine::sharedEngine()->getEffectsVolume()-0.1f);
;
}
}
6、运行结果
猜你在找的Cocos2d-x相关文章