1 Reply Latest reply: Apr 4, 2012 5:03 AM by Rob Dillon RSS

    sound spectrum as3


      I have   the following  as3 code


      var url:String = "song.mp3";

      var request:URLRequest = new URLRequest(url);

      var s:Sound = new Sound();



      var song:SoundChannel

      var ba:ByteArray = new ByteArray();


      var gr:Sprite = new Sprite();

      gr.x = 20;

      gr.y = 200;



      song = s.play();

      var isPlaying:Boolean = false;

      gr.addEventListener(MouseEvent.CLICK, togglePlay);

      function togglePlay(e:MouseEvent):void


          if (!isPlaying)


      song = s.play();

      addEventListener(Event.ENTER_FRAME, loop);





      removeEventListener(Event.ENTER_FRAME, loop);



          isPlaying = !isPlaying;



      addEventListener(Event.ENTER_FRAME, loop);

      function loop(e:Event):void {

          SoundMixer.computeSpectrum(ba, true);

          var i:int;


          gr.graphics.lineStyle(0, 0xFF0003);


          gr.graphics.moveTo(0, 0);

          var w:uint = 2;

          for (i=0; i<30; i+=w) {

              var t:Number = ba.readFloat();

              var n:Number = (t * 10);

              gr.graphics.drawRect(i, 0, w, -n);




      when  i  execute the  code. the  spectrum  stops but song  continues to  play. What i want is when i click on the spectrum  ,both the spectrum and the song stops .When i  click again on the spectrum both song and  spectrum resumes to  play.


      please help



        • 1. Re: sound spectrum as3
          Rob Dillon MVP

          Two things. First change the line:

          var isPlaying:Boolean = false;


          var isPlaying:Boolean = true;

          Second, you may want to add another sprite to the gr sprite to act as a target so that it is easier to click on the spectrum. Alternately, make the spectrum sprite larger so that it is easier to click on.