Grazie per la risposta di @adam.artajew! L'ho modificato da utilizzare con Cocos2D v3
e ho creato una categoria Crossfade
sul motore audio per usarlo da qualsiasi punto del gioco. Ora essa svolge seguenti azioni:
- Fade Out
- Cambiare il brano
- Fade in
In OALSimpleAudio+Crossfade.m
includono queste importazioni:
#import "CCDirector_Private.h"
#import "CCActionManager.h"
e metodo implementano:
- (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];
}
Usage: Includere OALSimpleAudio+Crossfade.h
e chiamare
[[OALSimpleAudio sharedInstance] playBg:@"MainBgMusic.mp3" crossfade:YES];
Va bene, grazie. –