如何在cocos2d中实现音乐渐隐效果?

5

很简单... 我的游戏中有一首背景音乐正在播放,我想交叉淡入淡出一首曲目,而不是突然停止。

//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];
         }
3个回答

5

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

-(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];
}

希望这能帮上忙!

4
你不能使用SimpleAudioEngine来实现这个。以下是我实现的方法:
  1. 创建一个继承自CDAudioManager的类。

  2. 在调用开始播放新背景音乐的方法时,首先检查ivar backgroundMusic(从CDAudioManager继承)是否不为nil并且正在播放。如果是,则使用CDLongAudioSourceFader将其淡出。

  3. 将新的背景音乐加载到backgroundMusic中(它是CDLongAudioSource的实例-请查找)。使用CDLongAudioSourceFader将其淡入。

这是第2步中的方法片段(很抱歉无法展示其余部分,因为它是我的专有库的一部分)。
- (void)audioBackgroundPlay:(NSString *)bgmfile crossfade:(float)fade {
    CDLongAudioSource *audioSource = [self audioBackgroundLoad:bgmfile];
    if (audioSource != backgroundMusic) {
        if (backgroundMusic) {
            [self audioBackgroundControl:AUDIOCTRL_STOP fade:fade];
            backgroundMusic.audioSourcePlayer.meteringEnabled = NO;
            [backgroundMusic release];
        }
        backgroundMusic = [audioSource retain];
        if (meteringEnabled_) {
            backgroundMusic.audioSourcePlayer.meteringEnabled = YES;
        }
    }
    if (![backgroundMusic isPlaying]) {
        [self audioBackgroundControl:AUDIOCTRL_PLAY fade:fade];
    }
}

0

感谢@adam.artajew的答案!我修改了它以与Cocos2D v3一起使用,并在音频引擎上创建了一个类别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];

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接