FORUMS: list search recent posts

Sound On/Off Button, but reversed?

COW Forums : Adobe Flash

<< PREVIOUS   •   FAQ   •   VIEW ALL   •   PRINT   •   NEXT >>
Charles Ferran
Sound On/Off Button, but reversed?
on Feb 20, 2010 at 2:09:51 am

I keep finding all over the web how to create a mute button, but I need something else. I already have the mute button but how do I have it so the swf starts with no sound and when the user clicks the sound button the sound turns on?

Charles Ferran
845-699-5270
http://www.charlesferran.com

"Far better is to dare mighty things, to win glorious triumphs, even though checkered by failure than to rank with the poor spirits who neither enjoy much, nor suffer much because they live in a gray twilight that knows no victory or defeat."



Return to posts index

demetri tashie
Re: Sound On/Off Button, but reversed?
on Feb 20, 2010 at 3:37:36 am

you are asking about the sound of a SWF ??? do you mean the sound of a flv video ? or is it just loaded sound in the swf ?

and if you do mean video, are you using a flv video component, or NetStream?

either way, it is of course possible, but all these cases would require different coding - some very easy and straight forward, others a little less so.

if you clarify, it will be easy to help you .


Return to posts index


demetri tashie
Re: Sound On/Off Button, but reversed?(multiple solutions)
on Feb 20, 2010 at 7:08:25 pm

here is some code i gathered together that i have found useful for the following 4 circumstances of controlling a sound:

1) to control an embedded sound
2) to load and control an external sound
3) to control the sound in a video component
4) to code a custom video playback using Netstream etc, and to control its sound.

for these examples:
the initial sound playing will be set very low - to 0.2
a button click will increase the sound volume to full (1.0)


each example works with the SoundTransform class attached to either the SoundChannel or NetStream.
you'll see that example 3 is the easiest way, being that the video component works with all this behind the scenes - and all you have to do is state the volume you want.

so what you do is declare a SoundTransform, and then set it's property

for the sake of brevity i omit code you might want to use when loading objects ( i.e. loading 'onComplete', capturing errors, etc)

(SoundTransform also works great for setting or changing the panning value of a sound - whether it is playing through the right or left speaker, or a combination of both. pan=0 is balanced right and left; pan=1 is all the sound only in the right side; pan=-1 is only the left side.)

NOTE: be careful of the difference between "SoundTransform" the class (with both letters S and T capitalized), and "soundTransform" the property (with only the letter T capitalized).

1) for an embedded sound. A sound file in the Library with a linkage name "Mantho" , and a movieclip/button onthe stage with instance name "sound_btn", to control the sound :

var man:Mantho = new Mantho(); //linkage/class name of imported sound file
var snCh:SoundChannel = new SoundChannel();

var snTr:SoundTransform = new SoundTransform();

snCh = man.play();

snTr.volume= .2;

snCh.soundTransform=snTr;

///the button function
sound_btn.addEventListener(MouseEvent.CLICK, increaseSound);
function increaseSound(evt:MouseEvent):void {
snTr.volume= 1;
snCh.soundTransform=snTr;
}



2) for an externally loaded sound. This imports and controls sound file "Mantho.mp3" with the same 'sound_btn' as above:

var mySong:URLRequest = new URLRequest("Mantho.mp3");

var song:Sound = new Sound();
var snCh:SoundChannel = new SoundChannel();
var snTr:SoundTransform = new SoundTransform();

song.load(mySong);
snCh = song.play();

snTr.volume= .2;

snCh.soundTransform=snTr;

//the button function
sound_btn.addEventListener(MouseEvent.CLICK, increaseSound);
function increaseSound(evt:MouseEvent):void {
snTr.volume= 1;
snCh.soundTransform=snTr;
}



3) video component. This controls the sound of a video "lucy1.flv" playing through a video component with instance name " videoPlayer", and changes its sound using the same 'sound_btn' as above.

//set the video source, and give it an initial volume level:
videoPlayer.source="lucy1.flv";
videoPlayer.volume=.2;

//the button function
sound_btn.addEventListener(MouseEvent.CLICK, increaseSound);
function increaseSound(evt:MouseEvent):void {
videoPlayer.volume= 1;
}


4) NetStream This controls the sound of a video "lucy1.flv" playing through NetStream. This is very simple and concise way to code a video player :

var video:Video = new Video();
//pass the size of the video above if you want: var video:Video = new Video(320, 240);
addChild(video);

var nc:NetConnection = new NetConnection();
nc.connect(null);//uses 'null' for progressive download or URL for streaming application

var ns:NetStream = new NetStream(nc);

var snTransfrm:SoundTransform = new SoundTransform();
snTransfrm.volume=.2;

video.attachNetStream(ns);
ns.play("lucy1.flv");
ns.soundTransform=snTransfrm;

//the button function
sound_btn.addEventListener(MouseEvent.CLICK, increaseSound);
function increaseSound(evt:MouseEvent):void {
snTransfrm.volume=1;
ns.soundTransform=snTransfrm;
}

// Steve Gudmundson's tip to add this event listener and function:
ns.addEventListener(AsyncErrorEvent.ASYNC_ERROR, asyncErrorHandler);
function asyncErrorHandler(event: AsyncErrorEvent): void {
// bogus function to avoid error message
}


hope this helps having these examples gathered in one place..

some of this code reviewed from:
Derrick Ypenburg's "ActionScript 3.0" (Peachpit Press). and
Rich Shupe and Zevan Rosser's 'Learning ActionScript 3.0" (O'Reilly - Adobe Developer Library)
and also Steve Gudmundson of http://www.sray.squidpower.com for the tip for including the Async listener and function to avoid an error message



Return to posts index


Charles Ferran
Re: Sound On/Off Button, but reversed?(multiple solutions)
on Feb 22, 2010 at 8:38:51 pm

sorry, I thought I would get an email when there was a response, thanks for your code. I'm not really sure since its someone else's project, but I will try your suggestions, thank you!

Charles Ferran
845-699-5270
http://www.charlesferran.com

"Far better is to dare mighty things, to win glorious triumphs, even though checkered by failure than to rank with the poor spirits who neither enjoy much, nor suffer much because they live in a gray twilight that knows no victory or defeat."



Return to posts index

<< PREVIOUS   •   VIEW ALL   •   PRINT   •   NEXT >>
© 2017 CreativeCOW.net All Rights Reserved
[TOP]