I have written simple example for playing mp3 sound with drawing sound FFT spectrum.
Here is the code.
Here is the button code used in above example. I simply referred - http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/SimpleButton.html#includeExamplesSummary
Here is the code.
package utils.sound
{
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.utils.ByteArray;
import flash.media.SoundMixer;
import flash.media.Sound;
import ui.Button;
import flash.net.URLRequest;
import flash.media.SoundChannel;
public class SoundPlayerTemp extends Sprite
{
private static const MAX_CHANEL :int = 256;
private var w:int = 1;
private var maxSize:int = 400;
private const OFFSET_Y:int = 20;
private var urlReq :URLRequest = new URLRequest("file://c:/temp/test.mp3");
// any button is fine....
private var startButton:Button = new Button(maxSize - 50, 100, OFFSET_Y);
private var snd:Sound = new Sound();
private var sndCh:SoundChannel;
private var playState:Boolean = false;
private var baSpect:ByteArray = new ByteArray();
public function SoundPlayerTemp ()
{
startButton.addEventListener(MouseEvent.CLICK, playButtonClickHandler);
addChild(startButton);
snd.load(urlReq)
}
protected function enterFrameHandler(evt:Event):void
{
SoundMixer.computeSpectrum(baSpect, true, 1);
graphics.clear();
graphics.lineStyle(0, 0x0000FF);
for (var i:int = 0; i < MAX_CHANEL; i++) {
graphics.moveTo(maxSize, 255-i + OFFSET_Y);
graphics.lineTo(maxSize-baSpect.readFloat()* maxSize, 255-i + OFFSET_Y);
}
for (i = 0; i < MAX_CHANEL; i++) {
graphics.moveTo(maxSize, 255-i + OFFSET_Y);
graphics.lineTo(maxSize+baSpect.readFloat()* maxSize, 255-i + OFFSET_Y);
}
}
protected function playButtonClickHandler(evt:MouseEvent):void
{
if (!playState)
{
playState = true;
sndCh = snd.play(0, 1000);
addEventListener(Event.ENTER_FRAME, enterFrameHandler);
}
else
{
playState = false;
sndCh.stop();
removeEventListener(Event.ENTER_FRAME, enterFrameHandler);
graphics.clear();
}
}
}
}
Here is the button code used in above example. I simply referred - http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/SimpleButton.html#includeExamplesSummary
package ui
{
import flash.display.SimpleButton;
import flash.display.Sprite;
public class Button extends SimpleButton
{
public function Button(x:int, w:int, h:int, lineColor:uint = 0x000000,
upColor:uint = 0xCCCCCC, hitColor:uint = 0xEEEEEE, downColor:uint=0xAAAAAA, overColor:uint=0xEEEEEE)
{
var upButton:Sprite = createButtonRect(x, w, h, lineColor, upColor);
var downButton:Sprite = createButtonRect(x, w, h, lineColor, downColor);
var hitButton:Sprite = createButtonRect(x, w, h, lineColor, hitColor);
var overButton:Sprite = createButtonRect(x, w, h, lineColor, upColor);
upState = upButton;
downState = downButton;
hitTestState = hitButton;
overState = overButton;
}
private function createButtonRect(x:int, w:int, h:int, lineColor:uint, backColor:uint):Sprite
{
var container:Sprite = new Sprite();
container.graphics.beginFill(lineColor,1.0);
container.graphics.drawRect(x,0,w,h);
container.graphics.beginFill(backColor,1.0);
container.graphics.drawRect(x+1,1, w-2,h-2);
container.graphics.endFill();
return container;
}
}
}

コメント