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
LVL 1
John CarneyReliability Business Tools Analyst IIAsked:
Who is Participating?
 
fixitbenConnect With a Mentor Commented:
Check out this answer that I did a little while ago http://experts-exchange.com/Web/WebDevSoftware/Flash/Q_21121789.html 

Then For the mute button just Create a Movie clip with two frames in one frame have this

globalsound = new Sound();
globalsound.setVolume(100);
stop();

Then in the other have

globalsound = new Sound();
globalsound.setVolume(0);
stop();

Then just have a button and add this to it

on(release)
{_root.McWith2Frames.nextFrame();}

Also I have a Volume slider if you would like to use that all you have to do is drop it into the fla you are working on.  

Hope This Helps
Fixitben
0
 
John CarneyReliability Business Tools Analyst IIAuthor Commented:
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
0
 
mrdilliardConnect With a Mentor Commented:
I would make a function to do this:

function fadeIn(){
   if(soundFile.getVolume() < 100){
      soundFile.setVolume(soundFile.getVolume()+1);
   }else{
      clearInterval(fadeIt);
   }
}

function fadeOut(){
   if(soundFile.getVolume() > 100){
      soundFile.setVolume(soundFile.getVolume()-1);
   }else{
      clearInterval(fadeIt);
   }
}

Then just use:
var fadeIt = setInterval(fadeIn(),100);
or
var fadeIt = setInterval(fadeOut(),100);

100 is the number of milliseconds between when the function runs. With these settings, they'll run 10 times per second (1000/100), meaning volume will take 10 seconds to completely fade out or in. If you want it to take about 5 seconds, take that number down to about 50.
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
John CarneyReliability Business Tools Analyst IIAuthor Commented:
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



0
 
mrdilliardCommented:
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.
0
 
fixitbenCommented:
Yup that is what You gotta do.  I was just giving you an easier but more labor intensive way.  

Fixitben
0
 
fixitbenCommented:
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
0
 
John CarneyReliability Business Tools Analyst IIAuthor Commented:
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




0
 
fixitbenCommented:
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);
}
0
 
John CarneyReliability Business Tools Analyst IIAuthor Commented:
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?
0
 
fixitbenCommented:
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
0
 
John CarneyReliability Business Tools Analyst IIAuthor Commented:
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.

0
 
fixitbenCommented:
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
0
 
mrdilliardCommented:
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)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.