Simple enough… I have a background song playing on my game, and I would like to crossfade a 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];
}
You can’t do that using
SimpleAudioEngine. Here is how I did it:Create a class that extends
CDAudioManagerIn the method that you call to start playing a new background music, first check if the ivar
backgroundMusic(inherited fromCDAudioManager) is not nil and is playing. If it is, then fade it out usingCDLongAudioSourceFader.Load the new background music into
backgroundMusic(it’s an instance ofCDLongAudioSource— look it up). Fade it in usingCDLongAudioSourceFader.Here’s a snippet of the method in step 2 (sorry can’t show you the rest as it is part of my own proprietary library)