CocosDenshion music fade out(CocosDenshion 音乐淡出)
问题描述
我在我的游戏中使用 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 音乐淡出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:CocosDenshion 音乐淡出
基础教程推荐
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01