Flash: How to lower background music when other sound effects play? - actionscript-3

I am using sound files from the library, not URLs.
I am making a soundboard and have a button that plays background music. The reason I have a button instead of automatically playing on load is that there are a few different background musics to choose from, each with their own button.
I also have several other buttons that play sounds. I want the background music volume to lower to something like 0.3 while other sounds are playing. I believe I need to create a variable like soundCount that goes up when a sound plays, and decreases when the sound finishes playing (so that if there are multiple sounds playing at once, the background music won't go back to normal until the count is at 0).
What's the best way to set this up? I made the soundCount variable but I'm having trouble creating an event listener for when sound effect finishes playing.

To change the volume of a sound in AS3, you need to use the SoundChannel and SoundTransform classes:
var backgroundMusic:Sound = // Assuming you've loaded a sound
var myChannel:SoundChannel = new SoundChannel();
myChannel = backgroundMusic.play();
var myTransform = new SoundTransform();
myChannel.soundTransform = myTransform;
Keep track of total sounds in the background by incrementing your count during calls to play and decrementing during SOUND_COMPLETE events. Add event listeners to other sounds' channels so they can modify background's volume:
var totalCurrentSounds:int = 0;
otherSoundChannel.addEventListener(Event.SOUND_COMPLETE, otherSoundComplete);
// Make sure this is on a SoundChannel, not the Sound itself.
Finally, modify the SoundTransform's volume member when the count is 0:
function otherSoundComplete(e:Event):void {
totalCurrentSounds--;
if (totalCurrentSounds <= 0)
myTransform.volume = 0.5;
}

Related

Flash AS3 | Looping sound for each animation set

with University done I've finally had time to go back to practicing my Flash work, and I need some help with getting sound to loop and change depending on what part of the animation is playing.
This is also partly a follow up question to an older one I asked, so look here to get more detail as to where I'm coming from if you don't understand! (I've solved that issue, the current one relates to following up on it!)
Flash AS3 | Finishing current animation set before code is executed
The situation is basic: I have two different scenes, one of a man walking and one of a man running. It loops seamlessly of the man walking until you hit a button, where it finishes the animation then starts looping the run animation. Likewise, hitting the button again, finishes the loop before going back to the looping walk animation. The code for the button is below.
import flash.events.MouseEvent;
Next2.addEventListener(MouseEvent.CLICK, Change_2);
function Change_2(event: MouseEvent): void
{
addEventListener(Event.ENTER_FRAME, enterFrame);
function enterFrame(e:Event):void {
if (currentFrame == 30) {
gotoAndPlay(31);
removeEventListener(Event.ENTER_FRAME, enterFrame);
}
}
}
My issue is trying to get a footstep sound to match up with these animations. So while the man is walking, the 'walking footsteps' sound clip will play and loop alongside the animation, and when the button is pushed and the animation moves into the running animation, the 'running footsteps'' sound will play.
Any help on this would be much appreciated. I'm terrible when it comes to anything audio/code based.
Well u can check about soundChannel. soundChannel can stop() and play() ur audio.
So import ur external sound or use internal one. ur code probably seems like this.
var mySound:Sound = new Sound(new URLRequest("YourSoundPath"));
var sc:SoundChannel = new SoundChannel();
//when u wanna play
sc = mySound.play();
//when u wanna stop
sc = mySound.stop();
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/media/SoundChannel.html
also look this link for more info
If you want to really MATCH the footsteps to the animation (for example, the step sound should play when the player have put his feet down) the only sure way to do that is to place the sound on a different layer where your animation is and set the sound to play as "stream" (if i remember correctly).
Otherwise the animation might be out of sync because of different computer capabilities, current computer or graphics card load etc.
When the sound is set to play as "stream" the flash player is orienting on the sound and drop frames if the graphics are slow to keep the sound and animation in sync

How call volume slider function in different keyFrames with different sound channels?

I have this code for my volume slider:
var stVolume:SoundTransform=new SoundTransform();
slider.addEventListener(SliderEvent.CHANGE,volslider);
function volslider(ev:SliderEvent):void
{
stVolume.volume=(ev.value/100);
sch1.soundTransform = stVolume;
}
I have many frames with different sound channels. How can I call this function with specific sound channels in each frame like shc2, sch3 and others instead use repeatedly all of function?
Is it possible send SoundChannel name whit eventListener for example:
slider.addEventListener(SliderEvent.CHANGE,volslider(/*something i don't know*/,sch10));
An AudioSource is attached to a GameObject for playing back sounds in a 3D environment. In order to play 3D sounds you also need to have a AudioListener. The audio listener is normally attached to the camera you want to use. Whether sounds are played in 3D or 2D is determined by AudioImporter settings.

Disabling Audio Flash

When creating an AS3 project and adding audio what I usually do is drag the audio I want to use (for a game menu for example) and when I want to stop the audio I enter
SoundMixer.stopAll();
I do this because if I say go to the about game frame in this menu then I will have the audio, but if I come back to the main frame where I originally had the music it sorta doubles in and the two different timings of the music are playing at the same time.How do I stop this without having to disable the audio when I change frame?
In these cases, it is easier to start and stop the sound directly via code. First, right click the sound clip in your Library panel, and select Properties. In the ActionScript tab, check "Export for ActionScript", and give it a class name of, say, Music. Then, to play and stop this clip, you can use this code:
import flash.media.Sound;
// create the music Sound object so that we can start playing
var music : Sound = new Music();
var musicChannel : SoundChannel;
// play the music
// the play() method returns a SoundChannel,
// which you can later use to stop the music
// notice that we first check to make sure musicChannel is null
// if it isn't null, music is already playing, so don't play it twice
if(musicChannel != null) musicChannel = music.play();
// when you switch screens, stop the music
// clear out the channel, so that we know that it is safe to start the music again
musicChannel.stop();
musicChannel = null;
Alternatively, if you play music and sounds by placing them on the timeline, you can stop it by using the Sync setting of the frame. On the timeline, create a keyframe where you switch screens and want the music to stop playing. Select that frame, and drag the sound clip you want to stop onto that frame. In the Properties panel, change the Sync option to Stop. This will cause only the selected sound to stop playing.
Yet another method is to create an empty MovieClip with the sound placed inside of it. Change the Sync setting to Stream, and pad the clip out with empty frames so that it will play the entire clip. Then, if you place this clip on your main menu screen, then it will only play during the main menu. Stream sounds only play the sound for as long as the MovieClip exists and is playing. Compare this to normal Event sounds, which will play the entire sound regardless.
The latter methods are more old-school and movie-centric. If you are programming a game, it would be more appropriate to use the former method to give you more control of the audio in code.

Cross fading sounds after event in Flash CS5

I am looking to cross-fade two sound files after an event (such as a button press) in Flash CS5.
Ideally, the first sound loops round until a button press at which point a fade out will begin with sound 2 simultaneously fading in. I have a pretty limited understanding of actionscript 3 .
Thanks for your help :)
In order to achieve this effect you will need to use two soundChannels. Since your using cs5 I am assuming your are looping your first sound file like so (and if not you should be):
var sc1:SoundChannel = myFirstSound.play(0, 999);
now when the event is called (button is clicked) you will want to decrease the volume of the first sound file and increase the volume of the second sound file. To do this you can use the soundTransform property on your soundChannels. this would be an example of decreasing the volume.
sc1.soundTransform = new SoundTransform(sc1.soundTransform.volume - .1, 0);
you would decrease the volume of the first sound file like this in an enterframe loop. also in this loop you would need to increase the volume of the second sound file:
var sc2:SoundChannel = mySecondSound.play(0, 999);
sc2.soundTransform = new SoundTransform(0, 0);
then inside the enterframe loop to increase the second sound it would be the same as above, but addition instead of subtraction:
sc2.soundTransform = new SoundTransform(sc2.soundTransform.volume + .1, 0);
the .1 in this case is how fast to fade the sound. you may have to play around with that. and once the first sound files volume hits zero you should remove it and end the loop. Anyways, I think this is enough information in order to complete your goal. good luck.

Is there a way to use a sound channel on a movieclip?

The only way i can describe this is showing the code I have already then trying to explain what i want to do..
Basically, I am creating a soundboard game, where at the bottom of the screen I will have a bar which is a movieclip, and I will be dragging other movieclips onto it and then clicking pay, and using an array and .push they will play in order. I am trying to put the sounds onto the movieclips using code. So far, I have this:
var snd1:Sound = newSound();
snd.load(newURLRequest("naturefrog.wav"));
var channel:SoundChannel;
snd.addEventListener
I am now stuck with what I would put for the listener to listen for.
See this answer for knowing when sounds have finished loading, if the sound is external:
Loading a sound from an external source
Now once you're ready to play the sound and listen for when it completes, that's answered here:
demonstrates setting up listeners for sound_complete, as well as looping
You should use the Flash API for Sound to determine what listeners to add. There are some helpful examples at the bottom of the page.
I assume you'll want to add:
snd.addEventListener(Event.COMPLETE, completeHandler);
snd.addEventListener(Event.ID3, id3Handler);
snd.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
snd.addEventListener(ProgressEvent.PROGRESS, progressHandler);
You are pushing the sound reference of a movieclip into an array when user drag it into a particular movieclilp, right? If you do so you can shift an element from array(array.shift, usually this removes and returns the first element of the array).
function playSoundInArray(){
if(array.length){
var snd:Sound = array.shift() as Sound;
channel = snd.play();
channel.addEventListener(Event.SOUND_COMPLETE, onSoundPlayed);
}else{
//Do something here if there is no sound reference or the array is empty
}
}
function onSoundPlayed(e:EVent){
channel.removeEventListener(Event.SOUND_COMPLETE, onSoundPlayed);
playSoundInArray();
}