6 Replies Latest reply on May 9, 2010 5:42 PM by celebritymusic

    Stopping sound playing on button click?

    celebritymusic Level 1

      Hi

       

      I found this code to play sound:

       

      1. myButton_btn.addEventListener(MouseEvent.CLICK, buttonIsClicked);
      2. function buttonIsClicked(event:Event):void
      3. {
      4. var sndcontext:SoundLoaderContext = new SoundLoaderContext(4000);
      5. var snd:Sound = new Sound(new URLRequest("http://www.yourdomain.com/yoursound.mp3"), sndcontext);
      6. var sndchannel:SoundChannel = snd.play(0,1000);
      7. sndchannel.soundTransform = new SoundTransform(0.2, 1);
      8. }

       

      I got it working fine, but was wondering how to program a function for another button to stop stop playing that sound?  I tried a function with snd.stop(); but that seems to be incorrect.

       

      Thanks for your help guys.

       

      Shaun.

        • 1. Re: Stopping sound playing on button click?
          Cancerinform Level 2

          I would use a Singleton script as described here

          http://flashscript.biz/Flash10/inshort/PlayOneSound.php

          and within the button function also add a script to play the movieclips. Then you don't have to add any audio inside of the movieclips.

          1 person found this helpful
          • 2. Re: Stopping sound playing on button click?
            celebritymusic Level 1

            Hi Cancerinform

             

            Thanks for that...

             

            I set it up, but Flash tells me it can't find definition biz.Flashscript.media:PlaySound

             

            What do I save that external .as file as?

             

            I've used .as files before, and I usually save the file as the same name as the package, but that didn't work.

             

            Any ideas?

             

            Cheers

             

            Shaun

            • 3. Re: Stopping sound playing on button click?
              Cancerinform Level 2

              Ah. Create an actionscript file and place the whole script in there. Then name the file PlaySound.as. Instead of package biz.Flashscript.media eliminate biz.Flashscript.media. Now have the file in the same location as your fla file and try it again.

              • 4. Re: Stopping sound playing on button click?
                celebritymusic Level 1

                Ahh - that fixed it - THANKS Cancerinform!

                 

                Just one other question if you would be so kind..

                 

                I've set up a button to act as a STOP button.

                 

                How do I access channel.stop(); in the PlaySound .as from the stop button function in my Fla?

                 

                Cheers

                 

                Shaun

                • 5. Re: Stopping sound playing on button click?
                  Cancerinform Level 2

                  In the PlaySound file make a change:

                   

                  /*
                  * We stop the sound here to finish the former Sound.
                  * Then the new Sound object will not overlap with any prviuos sound. Here we can
                  * also stop the sound completely.
                  */
                              if (channel != null || url == null)
                              {
                                  channel.stop ();
                                  if(url == null)
                                  {
                                      return _instance;
                                  }
                              }

                   

                  In the fla file add a button named stopBut and use this script, which I modifed from the original:

                   

                  import flash.events.*;

                   

                  var soundArray:Array = new Array({but:'button1',audio:'track1.mp3'},

                   

                  {but:'button2',audio:'track2.mp3'},

                   

                  {but:'button3',audio:'track3.mp3'},

                   

                  {but:'stopBut',audio:null});

                   

                  function startAudio(e:MouseEvent)

                   

                  {

                   

                      var but:Sprite = e.currentTarget as Sprite;

                   

                      var myAudio:String;

                   

                      var myPath:String = 'mp3/';

                   

                      for (var count01:int = 0; count01 < soundArray.length; count01++)

                   

                      {

                   

                          var myBut:Button = getChildByName(soundArray[count01].but) as Button;

                   

                          if (but.name == myBut.name)

                   

                          {

                   

                              if (but.name == "stopBut")

                   

                              {

                   

                                  myAudio = soundArray[count01].audio;

                   

                              }

                   

                              else

                   

                              {

                   

                                  myAudio = myPath + soundArray[count01].audio;

                   

                              }

                   

                          }

                   

                      }

                   

                      try

                   

                      {

                   

                          var bb:PlaySound = PlaySound.getInstance(myAudio);

                   

                      }

                   

                      catch (e:Error)

                   

                      {

                   

                          trace('Error');

                   

                      }

                   

                  }

                   

                  for (var count:int = 0; count < soundArray.length; count++)

                   

                  {

                   

                      var myBut:Button = getChildByName(soundArray[count].but) as Button;

                   

                      myBut.label = 'Track ' + count;

                   

                      if (soundArray[count].but == "stopBut")

                   

                      {

                   

                          myBut.label = "Stop Audio";

                   

                      }

                   

                      myBut.addEventListener(MouseEvent.CLICK, startAudio);

                   

                  }
                  1 person found this helpful
                  • 6. Re: Stopping sound playing on button click?
                    celebritymusic Level 1

                    Hey Cancerinform

                     

                    That's perfect mate - thanks so much for your effort - I really appreciate it.

                     

                    Shaun