Andy Olle
Andy Olle

Reputation: 7

Stopping a sound in AS3

I'm making a little interactive ad for an album. It has 4 draggable song titles, that when dropped on a target, starts the song. Trying to figure out how to stop the song that's playing when a new song is started. Here's my code:

 package {
      import flash.display.*;
      import flash.events.*;
      import flash.media.Sound;
      import flash.media.SoundChannel;
      import flash.net.URLRequest;

  public class Deftones extends MovieClip{

      function Deftones() {

          swerve1.addEventListener(MouseEvent.MOUSE_DOWN, dragStartS);
          swerve1.addEventListener(MouseEvent.MOUSE_UP, dragEndS);
          polt1.addEventListener(MouseEvent.MOUSE_DOWN, dragStartS);
          polt1.addEventListener(MouseEvent.MOUSE_UP, dragEndS);
          rd1.addEventListener(MouseEvent.MOUSE_DOWN, dragStartS);
          rd1.addEventListener(MouseEvent.MOUSE_UP, dragEndS);
          gauze1.addEventListener(MouseEvent.MOUSE_DOWN, dragStartS);
          gauze1.addEventListener(MouseEvent.MOUSE_UP, dragEndS);
          var sound1:Sound= new Sound();
          var sound2:Sound= new Sound();
          var sound3:Sound= new Sound();
          var sound4:Sound= new Sound();
          sound1.load(new URLRequest("music/Swerve_City.mp3"));
          sound2.load(new URLRequest("music/Poltergeist.mp3"));
          sound3.load(new URLRequest("music/Romantic_Dreams.mp3"));
          sound4.load(new URLRequest("music/Gauze.mp3"));
          var channel:SoundChannel= new SoundChannel();

          function dragStartS(e:MouseEvent){
              e.currentTarget.startDrag();
          }
          function dragEndS(e:MouseEvent){
              e.currentTarget.stopDrag();
              if (swerve1.hitTestObject(speaker1)){
                  channel.stop();
                  sound1.play(0);
              }
              else if (polt1.hitTestObject(speaker1)){
                  channel.stop();
                  sound2.play(0);
              }
              else if (rd1.hitTestObject(speaker1)){
                  channel.stop();
                  sound3.play(0);
              }
              else if (gauze1.hitTestObject(speaker1)){
                  channel.stop();
                  sound4.play(0);
              }
          }

      }
  }

Any help would me much appreciated.

Upvotes: 0

Views: 241

Answers (2)

Khalil Bhm
Khalil Bhm

Reputation: 398

You can use SoundChannel class for this, look in this link (Section 4 - Stopping a Sound)

Upvotes: 1

Marty
Marty

Reputation: 39456

You can use SoundMixer.stopAll()

Stops all sounds currently playing.

Upvotes: 1

Related Questions