• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

sound spectrum as3

New Here ,
Apr 03, 2012 Apr 03, 2012

Copy link to clipboard

Copied

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

TOPICS
ActionScript

Views

2.0K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 04, 2012 Apr 04, 2012

Copy link to clipboard

Copied

LATEST

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines