Iphone 如何在cocos2d中交叉淡入音乐?

Iphone 如何在cocos2d中交叉淡入音乐?,iphone,objective-c,ios,cocos2d-iphone,Iphone,Objective C,Ios,Cocos2d Iphone,很简单。。。我有一首背景歌在我的游戏中播放,我想交叉淡入一个轨道,而不是一个硬停止 //Prep Background Music if (![[SimpleAudioEngine sharedEngine]isBackgroundMusicPlaying]) { [[SimpleAudioEngine sharedEngine] preloadBackgroundMusic:@"song.mp3"]; } [[Simpl

很简单。。。我有一首背景歌在我的游戏中播放,我想交叉淡入一个轨道,而不是一个硬停止

//Prep Background Music
        if (![[SimpleAudioEngine sharedEngine]isBackgroundMusicPlaying]) {
             [[SimpleAudioEngine sharedEngine] preloadBackgroundMusic:@"song.mp3"];
        }
        [[SimpleAudioEngine sharedEngine] setBackgroundMusicVolume:1.0]; 

        //Play Background Music
         if (![[SimpleAudioEngine sharedEngine]isBackgroundMusicPlaying]) {
             [[SimpleAudioEngine sharedEngine] playBackgroundMusic:@"song.mp3" loop:YES];
         }

您不能使用
SimpleAudioEngine
来实现这一点。我是这样做的:

  • 创建扩展
    CDAudioManager

  • 在您调用的开始播放新背景音乐的方法中,首先检查ivar
    backgroundMusic
    (继承自
    CDAudioManager
    )是否为零且正在播放。如果是,则使用
    CDLongAudioSourceFader
    将其淡出

  • 将新的背景音乐加载到
    backgroundMusic
    (它是
    CDLongAudioSource
    的一个实例--查找它)。使用
    CDLongAudioSourceFader将其淡入淡出

  • 下面是第2步中方法的一个片段(很抱歉,无法向您展示其余部分,因为它是我自己专有库的一部分)


    我使用这个简单的方法来替换当前的背景音乐:

    -(void)replaceBackgroundMusic:(NSString *)filePath volume:(float)volume
    {
        // no music's playing right now
        if (![[SimpleAudioEngine sharedEngine] isBackgroundMusicPlaying])
            return [self playBackgroundMusic:filePath volume:volume];
    
        // already playing requested track
        if ([filePath isEqualToString:[[[CDAudioManager sharedManager] backgroundMusic] audioSourceFilePath]])
            return;
    
        // replace current track with fade out effect
        float currentVolume = [SimpleAudioEngine sharedEngine].backgroundMusicVolume;
        id fadeOut = [CCActionTween actionWithDuration:1 key:@"backgroundMusicVolume" from:currentVolume to:0.0f];
        id playNew =
        [CCCallBlock actionWithBlock:^
         {
             [[SimpleAudioEngine sharedEngine] setBackgroundMusicVolume:volume];
             [[SimpleAudioEngine sharedEngine] playBackgroundMusic:filePath];
         }];
    
        [[[CCDirector sharedDirector] actionManager] addAction:[CCSequence actions:fadeOut, playNew, nil] target:[SimpleAudioEngine sharedEngine] paused:NO];
    }
    

    希望有帮助

    谢谢@adam.artajew的回答!我将其修改为与
    cocos2dv3
    一起使用,并在音频引擎上创建了一个类别
    Crossfade
    ,以便在游戏中的任何位置使用它。现在它执行以下操作:

    • 淡出
    • 变轨
    • 淡入
    OALSimpleAudio+Crossfade.m
    中包括以下导入:

    #import "CCDirector_Private.h"
    #import "CCActionManager.h"
    
    设计和实施方法:

    - (void)playBg:(NSString *)name crossfade:(BOOL)crossfade {
    
        // Skip if already playing requested track
        if (self.bgPlaying &&
            [self.backgroundTrack.currentlyLoadedUrl.lastPathComponent isEqualToString:name]) {
            return;
        }
    
        // Play right now if no crossfade needed
        if (!crossfade) {
            [self playBg:name loop:true];
        }
    
        // Fade out just if music's playing right now
        NSMutableArray *actions = [NSMutableArray array];
        if (self.bgPlaying) {
            id fadeOut = [CCActionTween actionWithDuration:0.5 key:@"bgVolume" from:self.bgVolume to:0.0f];
            [actions addObject:fadeOut];
        }
        // Replace current track with fade in effect
        id playNew = [CCActionCallBlock actionWithBlock:^{
            [self playBg:name loop:true];
        }];
        id fadeIn = [CCActionTween actionWithDuration:0.5 key:@"bgVolume" from:0.0f to:1.0f];
        // Combime final action
        [actions addObjectsFromArray:@[playNew, fadeIn]];
        id sequence = [CCActionSequence actionWithArray:actions.copy];
    
        // Run action
        [[[CCDirector sharedDirector] actionManager] addAction:sequence target:self paused:NO];
    }
    
    用法:包括
    OALSimpleAudio+Crossfade.h
    和调用

    [[OALSimpleAudio sharedInstance] playBg:@"MainBgMusic.mp3" crossfade:YES];
    
    [[OALSimpleAudio sharedInstance] playBg:@"MainBgMusic.mp3" crossfade:YES];