Groups | Blog | Home
all groups > flash actionscript > october 2006 >

flash actionscript : Volume & Slider Position When Loaded



Dyer55
10/13/2006 5:44:31 PM
You people have been so good at helping me and I hope I can get a little
direction with this issue as well. First let me say I am not using components,
just my own movie clips and coding.
I made a little mp3 player and so far it works beautifully. The only issue I
have is this... When my mp3 player is loaded and begins to play the song, the
volume slider is all the way to the left which turns my volume all the way
down. Making the user turn up the volume(with my slider) before they can hear
anything.

I want the volume and the slider's position to be at half when it loads, but
have the capability to turn down if the user chooses.

Ive tried to code it in various ways and here are the two issues I have run
into....
Either my mp3 loads with the volume at about half which I want but the slider
doesn't function.
Or.. if I do get both the volume and slider to load at 50% I am unable to move
my slider any lower (just higher) and thus the volume can not be lowered.

Here is my code, I hope its readable. I'm not the best at coding. :smile;

//-----------------------<Create Sound Object And Loader>-----------------\\
this.createEmptyMovieClip("mcSoundHolder", this.GetNextHighestDepth());
var mySound:Sound = new Sound(mcSoundHolder);
mySound.loadSound("NG52005.mp3", true);
//-----------------------/<Create Sound Object And Loader>-----------------\\

//-----------------------------<text box &
volume>------------------------------\\
//below code gets info on loading sound placing text into our textfield as
well as set our volume to our controller
this.onEnterFrame = function():Void {
finished = mySound.getBytesLoaded(mcSoundHolder);
total = mySound.getBytesTotal(mcSoundHolder);
if (finished != total) { //if our song is not totaly downloaded
mp3TextBox.songBox.text = "downloading.....";
} else {
//complete = 1; //only use this if not streaming
mp3TextBox.songBox.text = "music by.....";
}
mySound.setVolume(_root.volume*2);
}

//----------------------------</text box>------------------------------\\

//------------------------<volume slider's dragger>-----------------------\\
mySlider.ratio = 0;
mySlider.dragger.onPress = function() {
mySlider.dragger.startDrag(true, 0, 0, mySlider.line._width, 0); //postition
of our dragger
mySlider.dragger.onEnterFrame = function() {
ratio = Math.round(mySlider.dragger._x*100/mySlider.line._width);
_root.volume = ratio;
};
};
mySlider.dragger.onRelease = mySlider.dragger.onReleaseOutside = stopDrag;


//-------------------------</volume slider's dragger>---------------------\\

//----------------------------<start & stop
buttons>--------------------------\\

stopBtn.onRelease = function():Void {
mySound.stop();
}
playBtn.onRelease = function():Void {
mySound.start(mySound.position/1000); //To allow user to pause sound yet not
start over when unpaused.
/* use this below if you are not Streaming the mp3
if (_root.mySlider.complete == 1) {
mySound.start(0, 99); //99 means to loop it 99 times
} */
}
//---------------------------</start & stop
buttons>--------------------------\\
S4Potential
10/13/2006 7:14:07 PM
You problem is with the onEnterFrame event handler. It's executed each time a
frame is entered. A frame is entered as many times per seconds as you frame
rate is set and this even if you have only one frame in your timeline or even
if you issued a stop command. Thus, reseting your volume to 50 non stop.

Replace it with the Sound.onLoad event handler which will be executed only
once after the sound is completly loaded.

See actionscript help for full details.

Dyer55
10/13/2006 7:36:06 PM
Hi, Thanks for the reply.

I tried to code it as you suggested but Its still opening up and playing the
song with the volume all the way down. The volume slider (which I have coded as
"dragger") is still opening up all the way to the left.

I am wondering if maybe this has to do with the way the slider dragger is
coded. Since it is coded to follow that pink line when its dragged up and down
to change the volume.
S4Potential
10/13/2006 8:20:38 PM
Once again, once an enterFrame event is triggered, it will be running ALL the
time even when the dragging has stopped. Kill the enterFrame once it is not
needed anymore:

delete this.enterFrame
delete mySlider.dragger.onEnterFrame

Put some traces here and there to make sure that the load completion is
actually detected and the enterFrames are deleted

this.onEnterFrame = function():Void {
trace("Enter this.EnterFrame");
finished = mySound.getBytesLoaded(mcSoundHolder);
total = mySound.getBytesTotal(mcSoundHolder);
if (finished != total) { //if our song is not totaly downloaded
mp3TextBox.songBox.text = "downloading.....";
} else {
//complete = 1; //only use this if not streaming
mp3TextBox.songBox.text = "music by.....";
trace("Completed sound loading detection in this.EnterFrame");
}
mySound.setVolume(_root.volume*2);
}

Do the same for the ;volume drag function.

Traces are you friends. Use them everywhere you can and take them out once you
know it is working well.

Also,

mySound.setVolume(_root.volume*2);

Where did you declare _root.volume? if undefined or even 0 it won't work.
0*2=0;

Why not simply do mySound.setVolume(50);

Dyer55
10/14/2006 2:13:48 AM
Thanks again for your help.

Like I said I tried it with out onEnterFrame previously and I wasn't
successful however what you are saying makes a lot of sense. I'm going to
remove both of them and add the suggestions you have given me.

About the _root.volume.
It's declared in the slider.dragger function. I think what you have suggested
is much better though ( the mySound.setVolume(50); ) and I will try that.

Thanks for trying to help me, sometimes coding just gets the best of me.
:embarrassment; I am still learning and working it out. I am going to work on
it again... cross fingers.
AddThis Social Bookmark Button