CocosDenshion 音乐淡出

2024-08-11移动开发问题
16

本文介绍了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 音乐淡出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

硬件音量按钮更改应用程序音量
Hardware Volume buttons change in app volume(硬件音量按钮更改应用程序音量)...
2024-08-12 移动开发问题
10

Cocos2d - 如何检查不同层中对象之间的交集
Cocos2d - How to check for Intersection between objects in different layers(Cocos2d - 如何检查不同层中对象之间的交集)...
2024-08-12 移动开发问题
8

恢复游戏 cocos2d
Resume game cocos2d(恢复游戏 cocos2d)...
2024-08-12 移动开发问题
6

突出显示朗读文本(在 iPhone 的故事书类型应用程序中)
Highlight Read-Along Text (in a storybook type app for iPhone)(突出显示朗读文本(在 iPhone 的故事书类型应用程序中))...
2024-08-12 移动开发问题
9

Cocos2D + 仅禁用 Retina iPad 图形
Cocos2D + Disabling only Retina iPad Graphics(Cocos2D + 仅禁用 Retina iPad 图形)...
2024-08-12 移动开发问题
10

如何将 32 位 PNG 转换为 RGB565?
How to convert 32 bit PNG to RGB565?(如何将 32 位 PNG 转换为 RGB565?)...
2024-08-12 移动开发问题
21