flash on 2012-5-25
♥0 |
Line 106 |
Modified 2012-07-14 07:11:28 |
MIT License
archived:2017-03-20 09:54:18
ActionScript3 source code
/**
* Copyright bpyser ( http://wonderfl.net/user/bpyser )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/jeiu
*/
package {
import flash.display.AVM1Movie;
import flash.display.Sprite;
import flash.display.Graphics;
import flash.events.Event;
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.media.SoundMixer;
import flash.net.URLRequest;
import flash.utils.ByteArray;
import flash.text.TextField;
import flash.external.ExternalInterface;
import flash.system.Security;
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.media.SoundLoaderContext;
import fl.controls.Button;
import flash.events.MouseEvent;
public class SoundMixer_compute extends Sprite {
private var text:TextField = new TextField();
private var snd:Sound;
private var aButton:Button = new Button();
private var playing:Boolean = false;
private var started:Boolean = false;
private var sndCh:SoundChannel;
private var pos:Number = 0;
public function SoundMixer_compute(){
Security.loadPolicyFile("http://hycro.crz.jp/crossdomain.xml");
addChild(aButton);
aButton.label = "Play";
aButton.toggle =true;
aButton.move(50, 50);
aButton.addEventListener(MouseEvent.CLICK, buttonClickHandler);
addEventListener(Event.ENTER_FRAME, onEnterFrame);
ExternalInterface.addCallback("getSoundMixerData", getSoundMixerData);
}
private function buttonClickHandler(event:MouseEvent):void {
if(!started){
playSound('http://www.takasumi-nagai.com/soundfiles/01.mp3');
playing = true;
started = true;
aButton.label = "Pause";
}else if(playing){
pos = sndCh.position;
sndCh.stop();
playing = false;
aButton.label = "Play";
}else{
aButton.label = "Pause";
sndCh = snd.play(pos);
playing = true;
}
}
public function pauseSound():void {
}
private function playSound(url:String):void
{
snd = new Sound();
var context:SoundLoaderContext = new SoundLoaderContext(0, true);
var sndReq:URLRequest = new URLRequest(url);
snd.load(sndReq,context);
sndCh = new SoundChannel();
sndCh = snd.play(0, 5);
}
public function getSoundMixerData (callback:String ):void {
try{
var bytes:ByteArray = new ByteArray();
const CHANNEL_LENGTH:int = 256;
SoundMixer.computeSpectrum(bytes, true, 0);
var data:String = "" + bytes.readFloat();
for(var i:int = 1; i < CHANNEL_LENGTH*2;i++){
data += "," + bytes.readFloat();
}
ExternalInterface.call(callback,data);
}catch(ex:Error ){
//ExternalInterface.call(callback,ex.message);
}
//ExternalInterface.addCallback("getSoundMixerData", getSoundMixerData);
}
private function onEnterFrame(event:Event):void {
var bytes:ByteArray = new ByteArray();
const PLOT_HEIGHT:int = 200;
const CHANNEL_LENGTH:int = 256;
SoundMixer.computeSpectrum(bytes, false, 0);
var g:Graphics = this.graphics;
g.clear();
g.lineStyle(0, 0x6600CC);
g.beginFill(0x6600CC);
g.moveTo(0, PLOT_HEIGHT);
var n:Number = 0;
for (var i:int = 0; i < CHANNEL_LENGTH; i++) {
n = (bytes.readFloat() * PLOT_HEIGHT);
g.lineTo(i * 2, PLOT_HEIGHT - n);
}
g.lineTo(CHANNEL_LENGTH * 2, PLOT_HEIGHT);
g.endFill();
g.lineStyle(0, 0xCC0066);
g.beginFill(0xCC0066, 0.5);
g.moveTo(CHANNEL_LENGTH * 2, PLOT_HEIGHT);
for (i = CHANNEL_LENGTH; i > 0; i--) {
n = (bytes.readFloat() * PLOT_HEIGHT);
g.lineTo(i * 2, PLOT_HEIGHT - n);
}
g.lineTo(0, PLOT_HEIGHT);
g.endFill();
}
}
}