Controlling sound on a button in Flash CS5
I am new to flash so please excuse the question if its super beginner.
In my flash documents I'm creating several buttons on a page that will trigger sound when the user press a given button. The attached audio ranges from 1 - 2 min in length, so I want to give the user the option to turn off the sound for a given piece if they so choose. I've done so by having a button on the page with the following AS3:
import flash.media.SoundMixer;
myButton.addEventListener(MouseEvent.CLICK,fn_clickHandler);
function fn_clickHandler(IN_Event:MouseEvent):void
{
SoundMixer.stopAll();
}
I 开发者_如何学Pythonwant to make sure that a user does not play more than one sound at a time, could someone inform me of how to apply that?
Thank you
I'm not quite sure that I completely understood your question. But if i did it right then here is the answer:
import flash.events.MouseEvent;
import flash.media.Sound;
import flash.media.SoundChannel;
var SChannel : SoundChannel;
var sound : Sound;
// your sound managing buttons
sound_1.addEventListener ( MouseEvent.CLICK, handleChangeSound );
sound_2.addEventListener ( MouseEvent.CLICK, handleChangeSound );
sound_3.addEventListener ( MouseEvent.CLICK, handleChangeSound );
// sound off button
turn_off.addEventListener ( MouseEvent.CLICK, turnOffSound );
// sound off managining function
function turnOffSound ( e : MouseEvent ) : void
{
SChannel.stop();
SChannel = null;
sound = null;
}
// are sounds in your library Sound_1 (), Sound_2 (), Sound_3 ()
function handleChangeSound ( e : MouseEvent ) : void
{
// in my situation since the button names were sound_1, .._2, .._3
// i can extract the sound id from names of the button.
// if you have some different approach you just need to adopt it.
var soundID : uint = e.target.name.split ( '_' )[1];
// ifi sound is playing, stop it
if ( SChannel )
{
turnOffSound(null);
}
// create new sound, from ID given
switch ( soundID )
{
case 1 :
sound = new Sound_1 ();
break;
case 2 :
sound = new Sound_2 ();
break;
case 3 :
sound = new Sound_3 ();
break;
}
// if sound was in the list start playing it.
if ( sound )
{
SChannel = sound.play();
}
}
And by the way, use SoundMixer
, only when you are refering to the whole flash page to go silent.
精彩评论