Link to home
Start Free TrialLog in
Avatar of John Carney
John CarneyFlag for United States of America

asked on

Actionscript to fade out a looped sound in a movie clip

I have a sound event called "bikeRiderMusic" on frame 20 of a 35-frame movie clip called "bikeRider".    I want it to fade out after about 15 seconds, stay muted for 3-5 seconds, then fade back in ... etc., etc.  

Also I would like to have a button on the main timeline which enables the user to mute that audio only.

Thank you

Gabriel
ASKER CERTIFIED SOLUTION
Avatar of fixitben
fixitben

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of John Carney

ASKER

Excellent!  Thank you.

With regard to the first half (fading out):  Is there a way to do this with action script on frame 1 of my 1-frame main timeline?  Since I accidentally created this animation at 24fps, I would need 480 frames to complete the full 20-second cycle, which would be okay except that I'm trying to be more lean and streamlined in my flash files.

Thanks again, and yes I would love your volume Slider!  How do I get it?

Gabriel
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
To:  mrdilliard
from: gabrielPennyback

This is great! but I'm not sure what I need to do in terms of instance names, where to place the action script (what frame of what timeline?), etc.  

I tried replacing every instance of "soundFile" with the name of my sound clip as it appears in the library, but that didn't have any effect.

I definitely need more specific guidance here.

Thank you,

gabriel



Avatar of mrdilliard
mrdilliard

Sorry I wasn't more specific.

You can place the functions anywhere on the timeline, as long as they're placed before you actually call them somewhere.

soundFile is going to refer to a new sound that you create using the library sound.

The syntax goes like:

var soundFile = new Sound()
soundFile.attachSound("name of file in library")
soundFile.start()

You can get more info at http://www.macromedia.com/support/flash/action_scripts/actionscript_dictionary/actionscript_dictionary654.html

By the way, you could do what I'm doing in an onEnterFrame event, but setInterval's a little cleaner to use, since it runs independently of the clips.
Yup that is what You gotta do.  I was just giving you an easier but more labor intensive way.  

Fixitben
Oh yeah and here is the file for the slider just copy and paste the slider to yours and it works http://theyac.org/experts/slider.fla 

Fixitben
I think I keep hitting submit by accident before I've finished the comment.  Sorry about that!

Once again,

to fixitben:  what else do I have to do in my .fla in order for the volume Slider to affect the volume of my audio clip called "loop1"?  If I place it as is, nothing happens.

To mrdilliard:

After defining the two functions for fading in and out on frame 1 of my MC, do I then physically place my sound on frame 36 where I want it to begin?

Or do I just insert this script: ?

var soundFile = new Sound()
soundFile.attachSound("name of file in library")
soundFile.start()

Or both?  And where do I place the script?

Finally, where do I place these scripts:

var fadeIt = setInterval(fadeIn(),100);
or
var fadeIt = setInterval(fadeOut(),100);

Sorry for being so dense.  Thank you for all your help.

gabriel




Did you copy it from the library or the stage you need to copy it from the stage.  If  you did that then change this

onClipEvent (load) {
globalsound = new Sound();
}
onClipEvent (enterFrame) {
      globalsound.setVolume(_root.volume*2);
}

to

onClipEvent (enterFrame) {
      soundFile.setVolume(_root.volume*2);
}
I tried copying from both the stage and the library.

The only script I see on the slider.fla that I copied is this:

this.ratio = 0;
_root.volume = 100;
dragger._x = 100;
dragger.onPress = function() {
      this.startDrag(true, 0, 0, line._width, 0);
      this.onEnterFrame = function() {
            ratio = Math.round(this._x*100/line._width);
            _root.volume = ratio;
      };
};
dragger.onRelease = dragger.onreleaseOutside=stopDrag;

that's it!  Where do I go to find this other bit of script to modify?
just click on the slider on the main stage once don't go inside it  The code you posted above is inside the MC slider.  If that isn't there then it won't work that is added  to the Slider Mc look at the File that I sent you and you will see where it is.  

Fixitben
To Fixitben:  Well I must have never actually copied it from the stage before ... But that did it!!  Thanks!

I still can't find that other bit of script anywhere.  For future reference, where exactly would you place that script ...

Also, mr dilliard's all-actionScript solution looks great, I'd like to start using stuff like that, but I can't figure out how to make it work.  For additional points, can you walk me through the steps with extreme specificity?  I always seem to miss something that no tutorial will ever tell you, such as your hint that I needed to copy the slider from the stage!

Thanks again,

Also, I want to accept your answer so you can get the points.  But I'm new to Experts Exchange, and I don't how to keep in communication with you once I close the question.  Please advise.

OK on the Main Stage Just Click on the Slider Once Then look at the AS box at the bottom it will be there.

And The thing is I tried his code And couldn't get it to work either Everything looks right But I just can't find the problem.  What I would do is wait a couple of days before you give the points away because he will probaly post again.   Then if he doesn't anwser you back then Just either Split the points by clicking that button on your page or just accept one answer by clicking the accept answer button.  Also about communication afiter the question is closed  Just Post something on here Like hey fixitben got a question about this slider and it will still send me a email even after the question is closed.  Then I will check it and see what is up.  So Basically a Question is never closed just the points are awarded to a certian person.

Hope this Helps
Fixitben
Yes, I'll certainly post...

I had a logic problem with my last set of functions...I was checking to see if the volume was greater than 100...yeah....

Anyway, here's some code you could slap into the first frame of your movie, and just replace instance names, as necessary.

function fadeIn() {
      if (soundFile.getVolume()<100) {
            soundFile.setVolume(soundFile.getVolume()+1);
      } else {
            clearInterval(app.fadeIt);
      }
}
function fadeOut() {
      if (soundFile.getVolume()>0) {
            soundFile.setVolume(soundFile.getVolume()-1);
      } else {
            clearInterval(app.fadeIt);
      }
}
mcButton.onRelease = function() {
      clearInterval(app.fadeIt);
      if (this.playing || this.playing == undefined) {
            this.txtDisplay.text = "FADE IN";
            this.playing = false;
            app.fadeIt = setInterval(fadeOut, 10);
      } else {
            this.txtDisplay.text = "FADE OUT";
            this.playing = true;
            clearInterval(fadeIt);
            app.fadeIt = setInterval(fadeIn, 10);
      }
};
_global.app = this;
//reference to _root
var soundFile= new Sound();
//located in library with linkage set to "mySound"
soundFile.attachSound("mySound");
soundFile.start(0, 999999);


Also, you can get the fla at my site if you'd prefer that. I even put in a crappy techno beat as my sound.

http://www.squirreltweezers.com/development/sound_file.fla (231K)