Skip navigation
Currently Being Moderated

sound spectrum as3

Apr 3, 2012 11:49 AM

I have   the following  as3 code

 

var url:String = "song.mp3";

var request:URLRequest = new URLRequest(url);

var s:Sound = new Sound();

s.load(request);

 

var song:SoundChannel

var ba:ByteArray = new ByteArray();

 

var gr:Sprite = new Sprite();

gr.x = 20;

gr.y = 200;

addChild(gr);

 

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);

    }

    else

    {

song.stop();

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.clear();

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

    gr.graphics.beginFill(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

 

rakesh

 
Replies
  • Currently Being Moderated
    Apr 4, 2012 5:03 AM   in reply to rakesh74

    Two things. First change the line:

    var isPlaying:Boolean = false;

    to

    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.

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points