2 Replies Latest reply: Oct 31, 2009 7:14 AM by clbeech RSS

    Need Help with Audio In ActionScript

    Federal Blue Community Member

      Hello I'm new here and to Flash. I have been trying to figure out how to work out the problems I stumbled across, but to no luck.

       

      I'm working on a animation that is currently 40 frames. Until a image is clicked the animation loops from frame 1 to 20, and then when it is clicked the full animation is shown. I used the gotoAndPlay code to loop until the button was clicked.


      I also added an audio file that is longer then 40 frames. I was wondering if there was a way to have the full audio play out until it is completed and then loop back to frame 1. What's happening now is whenever the animations loop so does the audio, so I have two or more tracks playing at once.

       

      Also after the image is clicked is there a way to have the audio stop?

       

      Thanks in advance for any help!

        • 1. Re: Need Help with Audio In ActionScript
          Federal Blue Community Member

          Alright! So I figured out how to do it another way. It took me hours being a complete newbie, but I removed the audio from the stage and instead found out I could use it from the library. I used this code and was suprised to hear the tune.

           

          var audio: Tune = new Tune();

          audio.play();

           

          And now it plays without looping with the animation each time the frame goes back to 1, well I actually placed the animation to loop back to frame 2. It worked though so I don't think there is a problem in doing that.

           

          Now I stumbled across a new problem though. I've been spending too much time trying to figure out what to do next. How do I stop the audio once the animaton gets to a certain frame?

           

          I used a code like the one below and I even tried different words like end, or finish. I even looked all around online for any type of tutorial, but none of them show anything that I was looking for.

           

          var audio: Tune = new Tune();

          audio.stop();

           

          I'm sure there's a way to do it, I'm just not skilled enough to know it myself. I would appreicate help.

          • 2. Re: Need Help with Audio In ActionScript
            clbeech Community Member

            ok - you have the right idea, but you do not have to 'declare' the variable 'Tune' again in order to target audio.stop() - however this also will depend on the structure of your document.  if you want to stop the audio when the image is clicked - assuming that you have the event handler on the first frame you should be able to simply place - audio.stop(); within the mouse handler.

             

            but if you wish the sound to stop at a 'different' time related to a particular frame - then you'll need to 'monitor' the location of the playhead and use a condition to determine the action taken.  to do this one method is to use a EnterFrame loop - like this:

             

            function monitor():void {

              if(currentFrame == 21) {

                removeEventListener(Event.ENTER_FRAME, monitor);

                audio.stop();

              }

            }

            addEventListener(Event.ENTER_FRAME, monitor);

             

            placed in the first frame this will run until the condition is satisfied then kill the enterframe loop and the sound.

             

            -----

            another method would be to use: SoundMixer.stopAllSounds();  - placing this line directly on the frame you wish to kill the sound.  much simpler, but not as easily 'adjusted' if you change something about the length of you timeline.

             

            -----

            considering that last statement (''...change something about the length...") it's often much better to use 'frame labels' in lue of a 'number' - because you can easily change the label position - but (especially with long in depth code) one may have to comb through your code and change all the 'numbers' if you change the position of the timeline animation.  to do this you would change the monitor to look for the label:

             

            if(currentLabel == 'continueLoop') { ... }

             

            -----

            one last tip   - there is a huge amount of imformation on every class, property, method and event of AS in the Flash Help files (F1) - reading these sections, for instance on the Sound Class will give you most of the information you need about any particular aspect.