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 {
if (self.bgPlaying &&
[self.backgroundTrack.currentlyLoadedUrl.lastPathComponent isEqualToString:name]) {
return;
}
if (!crossfade) {
[self playBg:name loop:true];
}
NSMutableArray *actions = [NSMutableArray array];
if (self.bgPlaying) {
id fadeOut = [CCActionTween actionWithDuration:0.5 key:@"bgVolume" from:self.bgVolume to:0.0f];
[actions addObject:fadeOut];
}
id playNew = [CCActionCallBlock actionWithBlock:^{
[self playBg:name loop:true];
}];
id fadeIn = [CCActionTween actionWithDuration:0.5 key:@"bgVolume" from:0.0f to:1.0f];
[actions addObjectsFromArray:@[playNew, fadeIn]];
id sequence = [CCActionSequence actionWithArray:actions.copy];
[[[CCDirector sharedDirector] actionManager] addAction:sequence target:self paused:NO];
}
Usage: Turn On OALSimpleAudio+Crossfade.hand Call
[[OALSimpleAudio sharedInstance] playBg:@"MainBgMusic.mp3" crossfade:YES];
source
share