Volume Slider a Flash CS4 Tutorials
69Changing The Volume
Sound can greatly enhance the interactivity of a website or video game. However, these same sounds can be found very annoying by some people. Many, Flash Applications only allow the user to turn the sound on or off and not control the volume. While, I find this fine when working with my laptop since the speakers do not play very loudly, but it is an entirely different matter at home. I have my computer hooked up to my stereo system and I hate web sites with loud sounds. I immediately close the site, or turn off the sound. So, I recommend that you always use a volume slider. This Flash CS3 Tutorial will show you how easily a volume slider can be created.
- CSS, How To
Great Blog Dedicated to sharing great CSS Tutorials and How To Guides. - IT Admin
IT Admin is you home for all computer related issues. Stop by and check out what is going on in the it world from a real it professional. - Flash CS3 Tutorials, Volume Slider
Flash CS3, CS4 and ActionScript 3.0 Tutorial on How to make a Volume Slider. - Adobe Flash, The Information You Need
This website id dedicated to teaching Flash, You will find all kinds of Flash CS4 Tutorials and other great flash information. - Flash CS4 Tutorials, Home Page
F2, is a website dedicated to teaching Flash CS3, You will find all kinds of Flash CS3 Tutorials - Flash CS4 Tutorials
List of Flash CS4 Tutorials and ActionScript 3.0 Tutorials
Volume Slider
The first section of the code in this Flash CS3 Tutorial is from the First Sound Tutorial, so it will not be covered in detail. Press F9 to open the actions panel and past the example code. The example code is exactly the same as in the first sound tutorial. The ActionScript is simply requesting a new sound, adding that sound to a sound channel and enabling the buttons to control the sound.
var yourRequest:URLRequest = new URLRequest ("SideOfABullet.mp3") var Music:Sound = new Sound() var controlMusic:SoundChannel Music.load(yourRequest)
play_btn.addEventListener(MouseEvent.CLICK, playMusic) function playMusic(event:MouseEvent):void{ controlMusic= Music.play() } stop_btn.addEventListener(MouseEvent.CLICK, stopMusic) function stopMusic(event:MouseEvent):void{ controlMusic.stop() };
Feel free to create your own slider, but for this Flash Tutorial it is best to use the source files provided since everything has been named for you. Create a variable to detect if the slider is moving. Next, create a new rectangle with no color, no height and a width of 100, this rectangle is basically a frame to restrict the sliders motion to left and right.
var moving:Boolean = false; var rectangle:Rectangle = new Rectangle(0,0,100,0);
An event listener needs to be added to the slider to start the drag and drop interactivity. Then, create a function to actually enable the startDrag method. Copy the code from the example. This ActionScript should be rather straight forward, but just in case, the parameters inside the startDrag method (false, rectangle) are referring to the rectangle created earlier. If you remove these parameters you will see that you can drag the slider all over the stage. volume_mc.slider_mc.addEventListener(MouseEvent.MOUSE_DOWN, moveSlider); function moveSlider(event:Event):void { controlMusic= Music.play() };
Add the final example code to the actions panel. You should be familiar with most of this code. You are simple adding event listeners to the stop and play buttons that were created for you. It is the code inside the events that should be new. First when the play button is clicked the sound channel you created earlier is set a Music.play(). remember you earlier defined Music as being equal to the audio file loaded when the .swf is called. So, the code associated with the play button is simple telling the music to play. It would stand to reason that the code associated with the stop button would then simply tell the music to stop. Just, remember that you have to control the sound channel, and not the sound directly. play_btn.addEventListener(MouseEvent.CLICK, playMusic) function playMusic(event:MouseEvent):void{ volume_mc.slider_mc.startDrag(false,rectangle); moving = true; };
Next, an event listener and function needs to be created to enable the stopDrag method. So, once again copy the example code and paste it below the existing code. This time for the stopDrag method the event listener is added to the stage and will listen for the mouse to be released. Whenever, the stage hears the mouse button being released, it checks to see if moving is true. If moving is true, flash then enables the stopDrag method and sets the moving variable to false.
stage.addEventListener(MouseEvent.MOUSE_UP, releaseSlider); function releaseSlider(event:Event):void { if (moving) { volume_mc.slider_mc.stopDrag(); moving = false; } };
A function needs to be created to actually change the volume since the slider works, but isn't controlling anything. This next section of code creates a new variable called vol that is a number and is set as equal to the sliders position divided by 100. Volume control ranges from 0 - 1, so the slider variable to control the volume needs to be less then one, therefore it is divided by 100. A simple trace of volume_mc.slider_mc.x will display this. Next, a SoundTransform class is created. You must create a new soundTransform class when working with the volume. The parameter is set as vol, which is the position of the slider, this parameter when applied to a sound is the actual volume. If you are getting lost it will become clear in a moment. Last, in the function if the sound created earlier isn't null, then actionscript applies the soundTransform to the sound. function changeVolume(event:Event):void { var vol:Number = volume_mc.slider_mc.x / 100; var soundtf:SoundTransform = new SoundTransform(vol); if (controlMusic != null) { controlMusic.soundTransform = soundtf; } };
The short version is that you must apply a sound transformation to any property of the sound you want to change. In this case actionscript is simply modifying the volume.
There is one thing left to do, and that is to add something to call the change volume function. Inside the function releaseSlider there needs to be an event listener. So, add the following code under the moving = false. volume_mc.slider_mc.stopDrag(); moving = false; volume_mc.slider_mc.addEventListener(Event.ENTER_FRAME,changeVolume); }
Press Control-Enter to see you new volume slider in action
Download Flash CS3 Source Files
Adobe Flash, Information and Tutorials
- CSS, How to understand inline and block level elements.
Block level elements are normally displayed as blocks with line breaks before and after. Examples of block level elements include paragraphs, headings, divs and block quotes. Inline elements are... - 4 months ago
- CSS, How To tweak css for only Safari?
If you have spent anytime dealing with CSS you have run into issues with a browser rendering code incorrectly. You may even have gone as far as to make several style sheets and use a browser check... - 4 months ago
- CSS, How to Hide Styles from Older Browsers?
Some older browsers, such as Netscape Navigator 4 and IE 4, have poor support for CSS. It is possible to hide styles from these browsers using specific media types and @import rules. All styles will... - 4 months ago
- CSS, How to use @import styles
@import Styles? Header and external style sheets also can import other style sheets using the @import rule. The @import rule must be placed before all other rules in the header or external style... - 4 months ago
- CSS, How to use External Style Sheets!
The third method of applying styles to document involves linking to external style sheets. External style sheets are the most appropriate method for styling documents. If styles need to be... - 4 months ago
- CSS, How to Use Header Styles
Header styles also can be used to style the <p> element. The CSS rules can be placed in the head of the document using the style element. Like inline styles, header styles, header styles... - 4 months ago
- CSS, How to Apply Inline Styles.
CSS, Inline styles can be applied directly to elements in the HTML code using the style attribute. However, inline styles should be avoided wherever possible because the styles are added to the HTML... - 4 months ago
- Twitter Weekly Updates for 2009-08-09
New blog post: CSS, What is a <div> http://bit.ly/Sbfu # Flash, ActionScript tutorials on loops http://bit.ly/QMq8t # Great PHP Tutorial on using include and require. Include is a great way... - 4 months ago
CSS, How to
- Twitter Weekly Updates for 2009-11-29
looking to make a contact form for your flash website? Then check out this simple tutorial. http://tinyurl.com/yjnocyc # http://twitvid.com/06368 RT @dkprinting A quick vid of @joelcomm Top 1% Report newsletter being bound . Learn more about the report – http:/ # Adopted a Ragdoll Kitten sunday, actually found a true Ragdoll at the pound. # We decided to [...] - 4 days ago
- Twitter Weekly Updates for 2009-11-22
The maker of iFart is sending a message to Steve Jobs and Apple: http://digg.com/d31ARUx?t # An Open Appeal to Steve Jobs: http://digg.com/d31ARUx?t # Powered by Twitter Tools - 2 weeks ago
- CSS, How to work with content areas.
The content are of a box can be given width, height, and color. Width and height can be specified in points (equal to 1/72 of an inch), picas (equal to 12 points), pixels, ems, exes, millimeters, centimeters, inches, or percents. - 2 months ago
- Twitter Weekly Updates for 2009-09-20
My Farmville is broken, cant access, gifts, market, or ribbons # Farmville and zynga are a rip off. Watch out. There are still some glitches in the game and zynga told me to bad Im unimportant. # wouldnt it make more since for a developer to fix broken issues rather then add new features. Well not farmville, [...] - 2 months ago
- Twitter Weekly Updates for 2009-09-06
I need 5 more neighbors on FarmVille help me out. http://bit.ly/tLsni # looking for flash tutorials and how to guides check out – http://frenchsquared.com/ # Powered by Twitter Tools - 3 months ago
- Twitter Weekly Updates for 2009-08-30
New version of Wordpress theme adStyle is live at: http://wordpress.org/extend/themes/adstyle # Just finished installing OS X 10.6 on my new Solid State drive. Very impressed, seems noticeably faster. # I need more neighbors on Farmville for Facebook. Hit me up as a friend. http://bit.ly/tLsni # playin farmville on my macbook 10.6 and new solid sate sata II [...] - 3 months ago
- CSS, How to properly use Border Styles.
The border properties specify the width, color and style of the border of an element. Shorthand border properties include border-top, border-bottom, border-right, border-left and border. - 3 months ago
- CSS, How to Padding Padding and more Padding
Padding can be applied to the outside edges of the content area of any block level or inline element. Padding creates the space between the edge of the element and its content. - 3 months ago
PrintShare it! — Rate it: up down flag this hub
Comments
Link is fixed
Thanks alot for this great tutorial!
Awesome tutorial. Just the one i was looking for.
Thank you.
amazing tutorial
thanks










Bob says:
12 months ago
This is exactly what I was looking for but the download source files link on the next page is broken!!