How to cross music in cocos2d?

Simple enough ... I have a background song playing in my game, and I would like to cross out the track instead of a hard stop.

//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];
         }
+5
source share
3 answers

You cannot do this using SimpleAudioEngine. Here is how I did it:

  • Create a class that extends CDAudioManager

  • In the method you invoke to start playing new background music, first check if ivar backgroundMusic(inherited from CDAudioManager) is null and is playing. If so, then fade out with CDLongAudioSourceFader.

  • Download the new background music in backgroundMusic(this is an instance CDLongAudioSource- find it). Put it out using 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];
    }
}
+4

:

-(void)replaceBackgroundMusic:(NSString *)filePath volume:(float)volume
{
    // no music 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];
}

, !

+5

Thanks for the @ adam.artajew answer! I changed it to use with Cocos2D v3and made a category Crossfadeon the sound engine to use it from anywhere in the game. Now it performs the following actions:

  • Attenuation
  • Change track
  • Attenuation

To OALSimpleAudio+Crossfade.minclude these imports:

#import "CCDirector_Private.h"
#import "CCActionManager.h"

And we implement the method:

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

Usage: Turn On OALSimpleAudio+Crossfade.hand Call

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

All Articles