问题描述
我在我的游戏中使用 cocos denshion 作为音乐.我目前正在使用代码播放背景音乐:
[[SimpleAudioEngine sharedEngine] playBackgroundMusic:@"backSong.mp3"];
但是,当游戏结束时,我需要背景音乐逐渐淡出.我怎样才能淡出背景音乐,有没有办法做到这一点?提前致谢!此外,ObjectAL 是否比 CocosDenshion 更好?如果有,有什么区别/优势?
I'm using cocos denshion for the music in my game.
I'm currently playing background music with the code:
[[SimpleAudioEngine sharedEngine] playBackgroundMusic:@"backSong.mp3"];
However, when the game ends, I need the background music to fade out gradually. How can I fade out the background music, is there a way to do this? Thanks in advance!
Additionally, is ObjectAL any better than CocosDenshion? If so, what are the differences/advantages?
推荐答案
我发现这样做的唯一方法是安排一个执行方法并相应地更改音量设置,如下所示:
The only way i found of doing that is to schedule a method for execution and change the volume setting accordingly, kind of as follows:
-(void) fadeOutBackgroundMusic{
if (!currentBackgroundMusic_) {
CCLOG(@"GESoundServicesProviderImpl<fadeOutBackgroundMusic> : No background music at this time, ignoring.");
return;
}
fadeOutActionTickerCount_=0;
[self schedule:@selector(tickMusicFadeOut:)];
}
-(BOOL) isPlayingBackgroundMusic{
return isPlayingBackgroundMusic_;
}
#pragma mark sequencing stuff
-(void) tickMusicFadeOut:(ccTime) dt{
static float fadeTime;
static float volume;
static float maxVolume;
fadeOutActionTickerCount_++;
if (1==fadeOutActionTickerCount_) {
isPerformingFadeOutAction_ =YES;
fadeTime=0.0f;
volume=0.0f;
maxVolume=audioSettings_.masterVolumeGain*audioSettings_.musicCeilingGain;
} else {
fadeTime+=dt;
volume=MAX(0.0f, maxVolume*(1.0 - fadeTime/K_MUSIC_FADE_TIME));
[self setMusicVolume:volume];
if (fadeTime>K_MUSIC_FADE_TIME) {
volume=0.0f; // in case we have a .000000231 type seting at that moment.
}
if (volume==0.0f) {
CCLOG(@"GESoundServicesProviderImpl<tickMusicFadeOut> : background music faded out in %f seconds.",fadeTime);
[self setMusicVolume:0.0f];
[sharedAudioEngine_ stopBackgroundMusic];
self.currentBackgroundMusic=nil;
isPlayingBackgroundMusic_=NO;
isPerformingFadeOutAction_=NO;
[self unschedule:@selector(tickMusicFadeOut:)];
}
}
}
这是来自我的声音服务提供者实现类的简化(编辑)示例(未测试,如此处所示).一般的想法是为自己安排一个在一段时间内逐渐淡出音乐的方法(这里是一个应用范围的常量,K_MUSIC_FADE_TIME).
this is a simplified (edited) sample from my sound services provider implementation class (not tested as shown here). The general idea is to schedule yourself a method that will gradually fade out the music over a period of time (here an app-wide constant, K_MUSIC_FADE_TIME).
这篇关于CocosDenshion 音乐淡出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!