# Creating a Volume Control Slider (tutorial)

**URL:** https://forum.kirupa.com/t/creating-a-volume-control-slider-tutorial/265921
**Category:** flash
**Created:** [July 14, 2008, 4:35pm UTC](https://forum.kirupa.com/t/creating-a-volume-control-slider-tutorial/265921 "2008-07-14T16:35:45Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![pritchie88](https://avatars.discourse-cdn.com/v4/letter/p/ed8c4c/32.png) [@pritchie88](https://forum.kirupa.com/u/pritchie88)
#### Post date: [July 14, 2008, 4:35pm UTC](https://forum.kirupa.com/t/creating-a-volume-control-slider-tutorial/265921/1 "2008-07-14T16:35:45Z")

</div>

Hi There,  
I followed the Kirupa tutorial about ‘Creating a Volume Control Slider’ which was great and worked fine, but I would like the mp3 to start playing at a moderate volume as soon as the user enters the site, at the moment you have to press play and then adjust the slider.  
Thanks in advance

---

<div class="post-metadata">

### Author: ![glosrfc](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/glosrfc/32/8334_2.png) [@glosrfc](https://forum.kirupa.com/u/glosrfc)
#### Post date: [July 14, 2008, 4:44pm UTC](https://forum.kirupa.com/t/creating-a-volume-control-slider-tutorial/265921/2 "2008-07-14T16:44:11Z")

</div>

Just assign a default volume within the onLoad event, e.g.

```auto
onClipEvent (load) {
	mySound = new Sound();
	mySound.loadSound("sound2.mp3", false);
	//set the default volume to whatever you want here
	_root.volume = 50;
}

```

You might also want to reflect the default volume setting on the slider, in which case also add the following line after where you’ve changed the volume:

```auto
dragger._x = line._width*_root.volume/100;

```

---

<div class="post-metadata">

### Author: ![romanoff](https://avatars.discourse-cdn.com/v4/letter/r/e480ec/32.png) [@romanoff](https://forum.kirupa.com/u/romanoff)
#### Post date: [July 14, 2008, 4:52pm UTC](https://forum.kirupa.com/t/creating-a-volume-control-slider-tutorial/265921/3 "2008-07-14T16:52:46Z")

</div>

I’m not quite sure, since I’ve changed the solution from the tutorial somewhat, but i think the following does the trick (let me know):

Add red Script !

1.in the ActionScript in the main scene:

onClipEvent (load) {  
mySound\_1 = new Sound();  
mySound\_1.loadSound(“sound\_1.mp3”, false);  
[COLOR=red] \_root.volume = 70;[/COLOR]  
[COLOR=#ff0000][/COLOR]  
[COLOR=#ff0000][/COLOR]  
[COLOR=black]2. In the ActionScript in the movieclip “slider”:[/COLOR]

[COLOR=black]ratio = Math.round(this.\_x_100/line.\_width);  
\_root.volume = ratio;  
};  
};  
[COLOR=red]dragger.\_x = line.\_width_\_root.volume/100;[/COLOR] [/COLOR]  
[COLOR=black]dragger.onRelease = dragger.onreleaseOutside=stopDrag;[/COLOR]

* * *

That’s it, hopefully :beer2:

Greetings, Romanoff  
[COLOR=#ff0000][/COLOR]

---

<div class="post-metadata">

### Author: ![pritchie88](https://avatars.discourse-cdn.com/v4/letter/p/ed8c4c/32.png) [@pritchie88](https://forum.kirupa.com/u/pritchie88)
#### Post date: [July 15, 2008, 10:55am UTC](https://forum.kirupa.com/t/creating-a-volume-control-slider-tutorial/265921/4 "2008-07-15T10:55:20Z")

</div>

[quote=romanoff;2358393]I’m not quite sure, since I’ve changed the solution from the tutorial somewhat, but i think the following does the trick (let me know):

Add red Script !

1.in the ActionScript in the main scene:

onClipEvent (load) {  
mySound\_1 = new Sound();  
mySound\_1.loadSound(“sound\_1.mp3”, false);  
[COLOR=red] \_root.volume = 70;[/COLOR]

[COLOR=black]2. In the ActionScript in the movieclip “slider”:[/COLOR]

[COLOR=black]ratio = Math.round(this.\_x_100/line.\_width);  
\_root.volume = ratio;  
};  
};  
[COLOR=red]dragger.\_x = line.\_width_\_root.volume/100;[/COLOR] [/COLOR]  
[COLOR=black]dragger.onRelease = dragger.onreleaseOutside=stopDrag;[/COLOR]

* * *

That’s it, hopefully :beer2:

Greetings, Romanoff  
[/quote]  
great thanks that sorts out the volume level:beer2:, what script do I use to have the sound playing as soon as the user enters the page at present you have to click the play button for the music to start?

---

<div class="post-metadata">

### Author: ![pritchie88](https://avatars.discourse-cdn.com/v4/letter/p/ed8c4c/32.png) [@pritchie88](https://forum.kirupa.com/u/pritchie88)
#### Post date: [July 15, 2008, 1:52pm UTC](https://forum.kirupa.com/t/creating-a-volume-control-slider-tutorial/265921/5 "2008-07-15T13:52:06Z")

</div>

[quote=glosrfc;2358390]Just assign a default volume within the onLoad event, e.g.  
ActionScript Code:  
[LEFT][COLOR=#0000FF]onClipEvent[/COLOR] COLOR=#000000[/COLOR] [COLOR=#000000]{[/COLOR]  
&nbsp;&nbsp;&nbsp;&nbsp;mySound = [COLOR=#000000] **new** [/COLOR] [COLOR=#0000FF]Sound[/COLOR]COLOR=#000000[/COLOR];  
&nbsp;&nbsp;&nbsp;&nbsp;mySound.[COLOR=#0000FF]loadSound[/COLOR][COLOR=#000000]([/COLOR][COLOR=#FF0000]“sound2.mp3”[/COLOR], [COLOR=#000000] **false** [/COLOR][COLOR=#000000])[/COLOR];  
&nbsp;&nbsp;&nbsp;&nbsp;[COLOR=#808080]_//set the default volume to whatever you want here_[/COLOR]  
&nbsp;&nbsp;&nbsp;&nbsp;\_root.[COLOR=#000080]volume[/COLOR] = [COLOR=#000080]50[/COLOR];  
[COLOR=#000000]}[/COLOR]  
[/LEFT]

You might also want to reflect the default volume setting on the slider, in which case also add the following line after where you’ve changed the volume:  
ActionScript Code:  
[LEFT]dragger.[COLOR=#0000FF]\_x[/COLOR] = line.[COLOR=#0000FF]\_width[/COLOR]\*[COLOR=#0000FF]\_root[/COLOR].[COLOR=#000080]volume[/COLOR]/[COLOR=#000080]100[/COLOR];  
[/LEFT]

[/quote]  
Thanks glosrfc! IF I want the music to play as soon a user enters the site do I need to change the ‘false’ part to ‘true’? thx  
mySound.[COLOR=#0000FF]loadSound[/COLOR][COLOR=#000000]([/COLOR][COLOR=#FF0000]“sound2.mp3”[/COLOR], [COLOR=#000000] **false** [/COLOR][COLOR=#000000])[/COLOR];

---

<div class="post-metadata">

### Author: ![pritchie88](https://avatars.discourse-cdn.com/v4/letter/p/ed8c4c/32.png) [@pritchie88](https://forum.kirupa.com/u/pritchie88)
#### Post date: [July 15, 2008, 2:21pm UTC](https://forum.kirupa.com/t/creating-a-volume-control-slider-tutorial/265921/6 "2008-07-15T14:21:25Z")

</div>

Thanks for all your help guys, much appreciated, managed to suss out the mp3 playing when user enters the page, but now the mp3 doesn’t loop?  
Whereas it did previously??

Do I need an extra piece of code to get the mp3 to loop?  
thanks
