20 Replies Latest reply on May 6, 2009 10:50 AM by kglad

    Stop a sound looping.

    thepeterep

      Hello,

       

      I am fairly new to Flash, but I'm creating a sort of sampler for a university project. I have 16 circles each with toggle buttons. The on state included a sound file placed on the toggle movie clip's timeline. The sound is set to Start and Loop.

       

      Basically, I want the first frame of the movie clip (the off toggle) to stop the sound from looping but despite my best efforts, nothing seems to work.

       

      I have tried this.stop(), seachord.stop(), this.stop(Sound), this.stopAllSounds. I wonder if there is some sort of actionscript that will allow me to change the state of the sound file from Loop to Repeat, at least that way the sound will stop with a simple stop() script... I hope.

       

      Alternatively, is there a way to stopAllSounds within one movie clip, rather than globally?

       

      Hope that all makes sense.

       

      Thanks,

      Peter.

        • 1. Re: Stop a sound looping.
          kglad Adobe Community Professional & MVP

          you have limited ability to control sounds that are attached to timelines.  you can work-around your problem, but it's more messy than doing it correctly from the start:

           

          use the flash sound class to play sounds in flash.

          1 person found this helpful
          • 2. Re: Stop a sound looping.
            thepeterep Level 1

            Do you think you could possiblt point in the right direction of a good introduction to Sound Class. I searched google before I posed the question but couldn't find anything useful or relevant.

             

            Thanks.

            • 3. Re: Stop a sound looping.
              kglad Adobe Community Professional & MVP

              the flash help files are always the best place to start.  check the sound class.  there will be a list of its methods and properties and there is usually sample code towards the bottom of the class listings.

              1 person found this helpful
              • 4. Re: Stop a sound looping.
                thepeterep Level 1

                Thanks,

                 

                I shall have a look.

                • 5. Re: Stop a sound looping.
                  kglad Adobe Community Professional & MVP

                  you're welcome.

                   

                  if you have trouble, copy and paste the code you tried and i'll correct it.

                  • 6. Re: Stop a sound looping.
                    thepeterep Level 1

                    I'm back, sorry!

                     

                    I had a look at the help file for soundclass, all it seemed to suggest I could do was set the pan levels, volume leves and some other fairly unuseful things.

                     

                    Am I right in thinking to get it to loop I need some sort of script that tells the file to play from 0 when it reaches the end? And if so, how would I go about adding this in to a button within a movie clip. Also, more importantly, how would I stop the script from running the loop function once I toggle the button off?

                     

                    Sorry if I'm being pathetic and hopeless, I just have so much else to do in this Flash project that I can really waste any more time searching around for answers.

                     

                    Again, any help would be much appreciated.

                     

                     

                    Thanks,

                    Peter.

                    • 7. Re: Stop a sound looping.
                      kglad Adobe Community Professional & MVP

                      check the start() method.  it accepts a parameter that specifies the number of loops:

                       

                      yoursound.start(0,999);  // no sound offset (ie, start at the beginning) and loop 999 times.

                      • 8. Re: Stop a sound looping.
                        thepeterep Level 1

                        Brilliant thanks.

                         

                        Is it then possible to stop it looping? Or do I simply need to set it to stop?

                         

                        Also, can I indefinately loop it? Or do I have to use the maximum amount of repeats (63635 or something isn't it??). Basically it's for a sort of sampler in which you can trigger different loops to remix an already existing song if the context helps at all...

                         

                         

                        Thanks so much for your time.

                        • 9. Re: Stop a sound looping.
                          kglad Adobe Community Professional & MVP

                          to stop a looping sound, you apply the stop() method.  if you want to stop it at the end of a loop you would set a boolean variable and check that boolean's value in an onSoundComplete listener.

                           

                          i don't know what the largest number of loops you can set.  but, if it's an issue, you can always start() your sound with no looping and then in the onSoundComplete listener, apply a start() method to the sound to loop it.  you may want to use an offset to bypass dead space for looping with code like this.

                          • 10. Re: Stop a sound looping.
                            thepeterep Level 1

                            Great.

                             

                            Thanks again.

                            • 11. Re: Stop a sound looping.
                              kglad Adobe Community Professional & MVP

                              you're welcome.

                              • 12. Re: Stop a sound looping.
                                thepeterep Level 1

                                OK. I officially feel really bad now.

                                 

                                Am I missing something vital?

                                 

                                I added on(Press) {seachord.start(0,999)} to my button but it doesn't play...

                                 

                                Do I need to make a new sound object or can I just play the sound from my library? And if I do need to make a new sound object, where should I do it? I don't want to have to wait for all the sounds to load but I also don't want a gap when you click each button as it loads the new sound object.

                                 

                                There are sixteen sounds. Each controlled by a button within a movie clip. When the button is pressed, it goes to the next frame in the movie clip (like a toggle button) and on the button in that frame I simply have on(Press) {seachord.stop()}.

                                 

                                Thanks.

                                 

                                EDIT: I just tried setting up a new sound object using:

                                 

                                var seachord:Sound = new Sound();
                                seachord.loadSound("01seachord.aif", false);

                                 

                                I got no errors with script or on launch but there is still nothing playing.

                                I attatched that script to the first frame of the movie clip which holds the two buttons.

                                • 13. Re: Stop a sound looping.
                                  kglad Adobe Community Professional & MVP

                                  is your button a movieclip?  is so, you need to use the following when attaching code to your button (which is not a good idea):

                                   

                                  _parent.seachord.start(0,999);

                                  • 14. Re: Stop a sound looping.
                                    thepeterep Level 1

                                    The button is within a movie clip which is within a movie clip.

                                     

                                    Does _parent correspond to where the sound object is? Or is it talking about the Library?

                                    • 15. Re: Stop a sound looping.
                                      kglad Adobe Community Professional & MVP

                                      _parent reference the parent timeline of the button (and true/simple buttons don't have timelines).

                                       

                                      anyway, remove that reference.

                                       

                                      place trace(this +" button") in your on(release) and place trace(this+" timeline") on the timeline where your sound is instantiated (=created).  then copy and paste the trace statements to find the path from your button to your sound instance.

                                      • 16. Re: Stop a sound looping.
                                        thepeterep Level 1

                                        Firefox is really struggling with this page for some reason so once again, apologies for the faffing.

                                         

                                        As I said in the pm, I have got the sounds to play using the sound objects, but they wont stop individually. When I stop one it stops them both. I have set different targets for each sound object using, for example, seachord=new Sound(SeaChordToggle) but they still both stop on any one button click.

                                         

                                        I tried setting up a load of new blank movie clips and simply placing them on the stage but that didn't work either despite it being a tried and tested mehtod for other people apparently.

                                         

                                         

                                        I really hope theres an answer after all this effort and hassl!

                                         

                                        Thanks,

                                        Peter.

                                        • 17. Re: Stop a sound looping.
                                          kglad Adobe Community Professional & MVP

                                          show the code you use to create both sound instances.

                                          • 18. Re: Stop a sound looping.
                                            thepeterep Level 1

                                            This is on the first frame of the SeaChordToggle move clip:

                                             

                                            stop();
                                            seachord= new Sound(SeaChordToggle);
                                            seachord.attachSound("SeaChord", false)
                                            ;
                                            onPress=function(){
                                                seachord.start(0,65535);
                                                nextFrame()   
                                            }

                                             

                                            And this is on the second:

                                             

                                            stop();

                                             

                                            onPress=function(){
                                                seachord.stop();
                                                prevFrame()   
                                            }

                                             

                                            The same code is used in the same places in the WoodBlockToggle movie clip. Obviously with SeaChordToggle changed to WoodBlockToggle in the new Sound(Target)

                                            • 19. Re: Stop a sound looping.
                                              thepeterep Level 1

                                              SORTED!

                                               

                                              It turns out my targets were just the library names of movie clips instead of instance names. All works fine now. Only thing is that with all 15 loops playing it get's quite distorted. Any way to get round that? Some sort of tally of sounds that could gardually turn the whole volume down as it increases maybe?

                                               

                                              Thanks.

                                              • 20. Re: Stop a sound looping.
                                                kglad Adobe Community Professional & MVP

                                                you have to code for that yourself.  there's no way via actionscript to detect the number of different sound currently playing.