Hi! Is there a way to fade out/in the background music using CDSoundEngine? I meam, can i control the volume at least? Thanks!
Fading music with CDSoundEngine
(6 posts) (4 voices)-
Posted 2 years ago #
-
Ok, if you promise not to laugh....but given my basic skills and not seeing any other way to handle it, here's what I did to fade in and out background music:
for (i = 0; i < 300; i++) { audioVolume = audioVolume + 0.001f; [CDAudioManager sharedManager].backgroundMusic.volume = audioVolume; }It's a quick fade in but at least it takes off the edge versus just starting or stopping it.
Posted 2 years ago # -
@pabloruiz - in CDSoundEngine the volume is called gain. You can control the volume of all sound effects with masterGain or set the gain of individual effects. If you want to dynamically modify the gain of an effect while it is playing then the best approach is to use a CDSourceWrapper - this is demonstrated in the CocosDenshionDemo but adjusting pitch, however, the approach is the same for gain.
Eventually I plan to have some actions available for fades. I was hoping someone would write some generic property modifying actions and I could use those.
@Tim - your method is okay but it is synchronous so it will impact the frame rate. For a short fade it is okay but imagine fading over 2 or 3 seconds - your application would lock up while the fade is happening.
Posted 2 years ago # -
Hey Steve,
Yes, I just wanted something to eliminate the abruptness of starting and stopping the sound. I of course use the following to manipulate other in game sounds as you mentioned above:
[[SimpleAudioEngine sharedEngine] playEffect:@"Slowjet.wav" pitch:1.0 pan:1.0 gain:0.7];Posted 2 years ago # -
In my game I subclassed SimpleAudioEngine and added this:
- (void)fadeBackgroundMusicFrom:(Float32)startVolume to:(Float32)endVolume duration:(ccTime)duration { fadeTimeTaken = 0; fadeDuration = duration; fadeStartVolume = startVolume; fadeEndVolume = endVolume; [[CCScheduler sharedScheduler] scheduleUpdateForTarget:self priority:1 paused:NO]; } - (void)update:(ccTime)delta { fadeTimeTaken += delta; CGFloat timeProportion = fadeTimeTaken / fadeDuration; if (timeProportion < 1.0) { CGFloat newMusicVolume = fadeStartVolume + (timeProportion * (fadeEndVolume - fadeStartVolume)); [self setBackgroundMusicVolume:newMusicVolume]; } else { [[CCScheduler sharedScheduler] unscheduleUpdateForTarget:self]; } }Posted 1 year ago # -
^ great but there is already functionality in CocosDenshion to fade background music and effects. It is demonstrated in the FadeToGrey demo.
Since that original post cocos2d also now has a TweenAction that can be used to modify any numeric property so that could also be used.
Posted 1 year ago #
Reply
You must log in to post.