CocosDenshion音乐淡出

CocosDenshion音乐淡出

本文介绍了CocosDenshion音乐淡出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用科科斯denshion在我的游戏音乐。
我目前正在播放的背景音乐与code:结果
[[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音乐淡出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 22:03