In case Flash no longer exists; a copy of this site is included in the Flashpoint archive's "ultimate" collection.

Dead Code Preservation :: Archived AS3 works from wonderfl.net

forked from: forked from: SimpleMP3Player

Get Adobe Flash player
by jokehn9 08 Jul 2012
    Embed
/**
 * Copyright jokehn9 ( http://wonderfl.net/user/jokehn9 )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/mM37
 */

// forked from jokehn9's forked from: SimpleMP3Player
package 
{
    
    import flash.display.*;
    import flash.events.*;
  
    import flash.geom.ColorTransform;
    import flash.utils.ByteArray;
    
    import flash.media.Sound;
    import flash.net.URLRequest;
    import flash.media.SoundLoaderContext;
    import flash.media.SoundChannel;
    import flash.media.SoundMixer;
    
   
    
    
    import flash.net.FileReference;
    import flash.net.FileFilter;
   

    import flash.text.TextField;
    import flash.text.TextFormat;
    import flash.text.TextFormatAlign;
    
    public class Main extends Sprite 
    {
        public static const WIDTH:int = 465;
        public static const HEIGHT:int = 465;
        private var _progressbar:ProgressBar;
        private var _player:SimpleMP3Player;
        private var _canvas:Sprite;
        private var s:Sound
        private var channel:SoundChannel;
        private const PARTICLE_NUM:int = 490;
        private var cx:int = 228;
        private var cy:int = 228;
        private var bytes:ByteArray = new ByteArray()
        private var txt:TextField = new TextField();
        private var arr:Array = []; 
        private var speed:Number = 0;
        private var d:Number = 0;
        private var bd:BitmapData = new BitmapData(465,465,true,0x0);
        private var g:Shape = new Shape();
        private var backGround:Shape = new Shape();
        private var tempx:Number = 0;
        private var tempy:Number = 0;
        private var rota:Number = 0.1;
        private var colorFixed:uint  = 0x0;
        
        public function Main():void 
        {
            if (stage) init();
            else addEventListener(Event.ADDED_TO_STAGE, init);
        }
        
        private function init(e:Event = null):void 
        {
            removeEventListener(Event.ADDED_TO_STAGE, init);
            // entry point
            var cou:int = 0;
            for(cou = 0; cou < PARTICLE_NUM;cou++) {
                arr[cou] = new particle;
                arr[cou].x = Math.random() * 465;
                arr[cou].y = Math.random() * 465;
                arr[cou].color = 0x6fffff;
                if(arr[cou].x < 306 && arr[cou].x > 150) {
                    if(arr[cou].y < 306 && arr[cou].y > 150) {
                        arr[cou].x = Math.random() * 465;
                        arr[cou].y = Math.random() * 465;
                       
                    }

                }
            }
            graphics.beginFill(0x0,0.9);
            graphics.drawRect(0, 0, WIDTH, HEIGHT);
            graphics.endFill();
            
            _canvas = new Sprite();
           
            addChild(new Bitmap(bd));
            _player = new SimpleMP3Player( LoadStart, LoadEnd );
            _player.x = WIDTH / 2;
            _player.y = 0;
            addChild( _player );
            
            addEventListener( Event.ENTER_FRAME, EnterFrameHandler );
        }
                
        private function LoadStart() : void {
            LoadEnd();
            _progressbar = new ProgressBar();
            _progressbar.x = WIDTH / 2;
            _progressbar.y = HEIGHT / 2;
            addChild( _progressbar );
        }
        
        private function LoadEnd() : void {
            if( _progressbar ){
                removeChild( _progressbar );
                _progressbar = null;
            }
        }
        
        private function EnterFrameHandler( e:Event ) : void {
           var n:Number = 0;
           var b:Number = 0;
            SoundMixer.computeSpectrum(bytes, false, 0);
            

             _canvas.graphics.clear();
             n = bytes.readFloat()
             /*_canvas.graphics.clear();
             _canvas.graphics.beginFill(0x0);
             _canvas.graphics.drawRect(0,0,456,456);
             _canvas.graphics.endFill();*/
             for(var i:int = 0; i<PARTICLE_NUM; i++) {
                 if(arr[i].x > 465 || arr[i].x <0) {
                     if(arr[i].y >465 || arr[i].y < 0) {
                         arr[i].x = cx+Math.random()*10 - 5;
                         arr[i].y = cy+Math.random()*10 -5 ;
                         arr[i].vx *= 0.5
                         arr[i].vy *= 0.5
                     }

                 }
                
                 b = bytes.readFloat();   
                 if(Math.abs(n) > 0.4) {
                     colorFixed = (Math.random() * 0xffffff)|0xaaaaaa;
                     for(i=0;i<PARTICLE_NUM; i++) {
                         arr[i].color = colorFixed;
                     }

                 }

             
                 _canvas.graphics.lineStyle(1, arr[i].color);
                 d = Math.sqrt(Math.pow(arr[i].x - cx , 2) + Math.pow(cy - arr[i].y,2)); 
                 if(b != 0) {
                     arr[i].vx += 2*(cx - arr[i].x)/b*6;
                     arr[i].vy += 2*( cy - arr[i].y)/b*6;
                 }
                 else {
                     arr[i].vx += 2*(cx - arr[i].x)/d;
                     arr[i].vy += 2*( cy - arr[i].y)/d;
                 }

                 
                 arr[i].vx *= 0.95;
                 arr[i].vy *= 0.95;
                if(n > 0.05 ) {
                     if(arr[i].x - cx > 0) {
                         arr[i].vx += Math.abs(Math.cos(i)*b*12);
                     }
                     else {
                         arr[i].vx -= Math.abs(Math.cos(i)*b*12);
                     }
                     if(arr[i].y - cy > 0) {
                         arr[i].vy += Math.abs(Math.sin(i)*b*12);
                     }
                     else {
                         arr[i].vy -= Math.abs(Math.sin(i)*b*12);
                     }
              }
                 /*else if(n >= 0.3) {
                     if(arr[i].x - cx > 0) {
                         arr[i].vx += Math.abs(n*12);
                     }
                     else {
                         arr[i].vx -= Math.abs(n*12);
                     }
                     if(arr[i].y - cy > 0) {
                         arr[i].vy += Math.abs(b*12);
                     }
                     else {
                         arr[i].vy -= Math.abs(b*12);
                     }
                 }
*/
                
                 _canvas.graphics.moveTo(arr[i].x, arr[i].y);
                 arr[i].x += arr[i].vx;
                 arr[i].y += arr[i].vy;
                 tempx = arr[i].x;
                 tempy = arr[i].y;
                 arr[i].x = Math.cos(n)*(tempx - cx) - Math.sin(n)*(tempy - cy) + cx;
                 arr[i].y = Math.cos(n)*(tempy - cy) + Math.sin(n)*(tempx - cx) + cy
                 _canvas.graphics.lineTo(arr[i].x, arr[i].y);    
                          
                
             }
             
             bd.draw( _canvas);
             bd.colorTransform(bd.rect, new ColorTransform(1,1,1,0.9,0,0,0,0));;
            
            
            
        }
        
    }
    
}

    import flash.display.*;
    import flash.events.*;
  
    import flash.geom.ColorTransform;
    import flash.utils.ByteArray;
    
    import flash.media.Sound;
    import flash.net.URLRequest;
    import flash.media.SoundLoaderContext;
    import flash.media.SoundChannel;
    import flash.media.SoundMixer;
    
   
    
    
    import flash.net.FileReference;
    import flash.net.FileFilter;
   

    import flash.text.TextField;
    import flash.text.TextFormat;
    import flash.text.TextFormatAlign;
    

    /**
     * ...
     * @author okoi
     */
    class ProgressBar extends Sprite
    {
        private var _mask:Shape;
        private var text:TextField;
        private var ct:int;
        
        private static const PARTICLE_LIFE:int = 60;
        private var particleList:/*Object*/Array;
        
        private var _rate:Number = 0;
        
        public function ProgressBar() 
        {
            ct = 0;
            particleList = [];
            
            var format:TextFormat = new TextFormat();
            format.font = "Arial";
            format.bold = true;
            format.color = 0xFFFFFF;
            
            text = new TextField();
            text.defaultTextFormat = format;
            text.text = "LOADING";
            text.selectable = false;
            text.x = -text.textWidth / 2;
            text.y = -text.textHeight / 2;
            
            addChild( text );
            
            _mask = new Shape();
            _mask.graphics.beginFill(0);
            _mask.graphics.drawRect( -100, -25, 200, 50 );
            _mask.graphics.endFill();
            this.mask = _mask;
            addChild( _mask );
            
            addEventListener( Event.ENTER_FRAME, EnterFrameHandler );
        }
        
        public function Destroy() : void {
            removeEventListener( Event.ENTER_FRAME, EnterFrameHandler );
        }
        
        private function EnterFrameHandler( e:Event ) : void {
             
            
            
            
            
            
            
         
            
            
        }
        
        
        public function set rate( val:Number ) : void { _rate = val;    }
    }

    /**
     * ...
     * @author okoi
     */
    class SimpleMP3Player extends Sprite
    {
        
        
        private var _bytes:ByteArray;
        private var _fr:FileReference;
        private var _sound:Sound = null;
        private var _soundChannel:SoundChannel = null;
        private var _pausePosition:Number;
        
        private var _loadStartCallback:Function/*():void*/;
        private var _loadEndCallback:Function/*():void*/;
        
        private var _btn_play:Button;
        private var _flg_play:Boolean = false;
        
        private var _btn_load:Button;
        private var _flg_loading:Boolean;
        
        private var _titleBoard:TitleBoard;
        
        public function SimpleMP3Player( loadstartcallback:Function, loadendcallback:Function ) 
        {
            _fr = new FileReference();
            _fr.addEventListener( Event.SELECT, FileSelectHandler );
            _fr.addEventListener( ProgressEvent.PROGRESS, ProgressHandler );
            _fr.addEventListener( Event.COMPLETE, FileLoadCompleteHandler );
            _fr.addEventListener( IOErrorEvent.IO_ERROR, FileLoadIOErrorHandler );
            _fr.addEventListener( SecurityErrorEvent.SECURITY_ERROR, FileLoadSecurityError );
            _flg_loading = false;
            
            _bytes = new ByteArray();
            
            _loadStartCallback = loadstartcallback;
            _loadEndCallback = loadendcallback;
            
            CreateUI();
        }
        
        private function OpenFileSelectDialog() : void {

            var filter:FileFilter = new FileFilter("mp3 file", "*.mp3");
            _fr.browse([filter]);
        }
                
        private function FileSelectHandler( e:Event ) : void {
            _fr.load();
            _flg_loading = true;
            _btn_play.Selectable( false );
            _titleBoard.SetTitle("");
            
            if( _loadStartCallback != null )    _loadStartCallback();
        }
        
        private function ProgressHandler( e:ProgressEvent ) : void {
            
        }
        
        private function FileLoadCompleteHandler( e:Event ) : void {
            
            _flg_loading = false;
            _pausePosition = 0;
            
            _sound = new Sound();
            _sound.loadCompressedDataFromByteArray( _fr.data, _fr.data.length );
            
            SetTitleBoardText( _fr.name );
            
            resetSoundChannel();
            
            _btn_play.Selectable( true );
            
            _flg_play = true;
            onClickPlayButton();
            
            if ( _loadEndCallback != null ) _loadEndCallback();
        }
        
        private function FileLoadIOErrorHandler( e:IOErrorEvent ) : void {
            _flg_loading = false;
            _sound = null;
            changePlayButton( false );
            _btn_play.Selectable( false );
        }
        private function FileLoadSecurityError( e:SecurityErrorEvent ) : void {
            _flg_loading = false;
            _sound = null;
            changePlayButton( false );
            _btn_play.Selectable( false );
        }
        
        private function resetSoundChannel() : void {
            if ( _soundChannel != null ) {
                _soundChannel.stop();
                _soundChannel.removeEventListener( Event.SOUND_COMPLETE, SoundCompleteHandler );
                _soundChannel = null;
            }
            _titleBoard.SetScrollFlag( false );
        }
        
        /**
         * サウンドの再生、再開を行う
         */
        private function play() : void {
            if ( _sound ) {
                _soundChannel = _sound.play( _pausePosition );
                _soundChannel.addEventListener( Event.SOUND_COMPLETE, SoundCompleteHandler );
                _pausePosition = 0;
                
                _titleBoard.SetScrollFlag( true );
            }
        }
        /**
         * サウンドの一時停止を行う
         */
        private function pause() : void {
            if ( _soundChannel ) {
                _pausePosition = _soundChannel.position;
                resetSoundChannel();
            }
        }        
        
        private function SoundCompleteHandler( e:Event ) : void {
            _pausePosition = 0;
            resetSoundChannel();
            play();
        }
        
       
        
        //////////////////////////////////////////////////////////////////////
        //////////////////////////////////////////////////////////////////////
        private function CreateUI() : void {
            
            var g:Graphics;
            
            g = graphics;
            
            //    背景
            
            

            CreatePlayButton();
            CreateLoadButton();
            CreateTitleBoard();
        }
        
        private function CreatePlayButton() : void {
            
            _btn_play = new Button( 58, 18, "play", onClickPlayButton );
            _btn_play.x = -35;
            _btn_play.y = 20;
            _btn_play.Selectable( false );
            addChild( _btn_play );
        }
        
        /**
         * 再生ボタンが押された
         */
        private function onClickPlayButton() : void {
            
            _flg_play = ( _flg_play ) ? false : true;
            changePlayButton( _flg_play );
            if ( _flg_play ) {
                play();
            }else {
                pause();
            }
        }
        private function changePlayButton( flg:Boolean ) : void {
            if ( flg ) {
                _btn_play.setLabelText( "stop" );
            }else {
                _btn_play.setLabelText( "play" );
            }
        }
        
        /**
         * 曲ロードボタンを作成
         */
        private function CreateLoadButton() : void {
            _btn_load = new Button( 58, 18, "load", onClickLoadButton );
            _btn_load.x = 35;
            _btn_load.y = 20;
            addChild( _btn_load );
        }
        /**
         * 曲ロードボタンが押された
         */
        private function onClickLoadButton() : void {
            
            if ( _flg_loading )    return;
            
            OpenFileSelectDialog();
        }
        
        
        private function CreateTitleBoard() : void {
            
            _titleBoard = new TitleBoard();
            _titleBoard.x = -TitleBoard.WIDTH / 2;
            _titleBoard.y = 40;
            addChild( _titleBoard );
        }
        
        private function SetTitleBoardText( text:String ) : void {
            _titleBoard.SetTitle(text);
        }
        
        
    }


    class Button extends Sprite {
        
        private var label:TextField;
        private var _onclick:Function/*():void*/ = null;
        private var _width:Number;
        private var _height:Number;
        private var _selectable:Boolean;
        
        public function Button( w:Number, h:Number, text:String, onclick:Function ) {
            
            graphics.lineStyle( 1, 0x474a4d );
            graphics.beginFill( 0x2b2b2b );
            graphics.drawRect( -w/2, -h/2, w, h );
            graphics.endFill();
            
            _width = w;
            _height = h;
            
            var tf:TextFormat = new TextFormat();
            tf.font = "Arial";
            tf.size = 12;
            tf.color = 0xFFFFFF;
            tf.align = TextFormatAlign.CENTER;
            
            label = new TextField();
            label.mouseEnabled = false;
            label.autoSize = "center";
            label.multiline = false;
            label.defaultTextFormat = tf;
            label.selectable = false;
            label.text = text;
            label.x = -w/2;
            label.y = -h/2;
            label.width = w;
            label.height = h;
            addChild( label );
            
            this._onclick = onclick;
            
            this.buttonMode = true;
            this.addEventListener( MouseEvent.MOUSE_DOWN, MouseDownHandler );
        }
        
        private function MouseDownHandler( e:MouseEvent ) : void {
        
            this.removeEventListener( MouseEvent.MOUSE_DOWN, MouseDownHandler );
            
            this.addEventListener( MouseEvent.MOUSE_UP, MouseUpHandler );
            this.addEventListener( MouseEvent.ROLL_OUT, RollOutHandler );
            
           
        }
        private function MouseUpHandler( e:MouseEvent ) : void {
            
            this.removeEventListener( MouseEvent.MOUSE_UP, MouseUpHandler );
            this.removeEventListener( MouseEvent.ROLL_OUT, RollOutHandler );
            
            this.addEventListener( MouseEvent.MOUSE_DOWN, MouseDownHandler );
            
            
            if ( _onclick != null ) {
                _onclick();
            }
        }
        private function RollOutHandler( e:MouseEvent ) : void {
            
            this.removeEventListener( MouseEvent.MOUSE_UP, MouseUpHandler );
            this.removeEventListener( MouseEvent.ROLL_OUT, RollOutHandler );
            
            this.addEventListener( MouseEvent.MOUSE_DOWN, MouseDownHandler );
            
            
        }
        
        
        
        public function setLabelText( text:String ) : void {
            label.text = text;
        }
        
        public function Selectable( flg:Boolean ) : void {
            _selectable = flg;
            
            this.mouseChildren = flg;
            this.mouseEnabled = flg;
            if ( !flg ) {
                
                this.alpha = 0.5;
            }else {
                this.alpha = 1;
            }
        }
    }

    class TitleBoard extends Sprite {
        
        public static const WIDTH:int = 300;
        public static const HEIGHT:int = 20;
        
        private var text:TextField;
        private var _mask:Shape;
        private var _flg_scroll:Boolean;
        
        public function TitleBoard() {
            
            this.alpha = 0.5;
            
            graphics.lineStyle( 1, 0x474a4d );
            graphics.beginFill( 0xc0c6c9 );
            graphics.drawRect( 0, 0, WIDTH, HEIGHT );
            graphics.endFill();

            var tf:TextFormat = new TextFormat();
            tf.font = "Arial";
            tf.size = 14;
            tf.color = 0xFFFFFF;
            tf.align = TextFormatAlign.CENTER;
            
            text = new TextField();
            text.autoSize = "left";
            text.selectable = false;
            text.defaultTextFormat = tf;
            text.x = WIDTH / 2;
            addChild( text );
            
            _mask = new Shape();
            _mask.graphics.beginFill(0);
            _mask.graphics.drawRect(0, 0, WIDTH, HEIGHT);
            _mask.graphics.endFill();
            this.mask = _mask;
            addChild( _mask );
            
            _flg_scroll = false;
            
            addEventListener( Event.ENTER_FRAME, EnterFrameHandler );
        }
        
        public function SetTitle( title:String ) : void {
            text.text = title;
            text.x = WIDTH / 2 - text.textWidth / 2;
        }
        
        private function EnterFrameHandler( e:Event ) : void {
            
            //if ( text.textWidth >= WIDTH ) {
            if( _flg_scroll ) {
                text.x -= 1;
                if ( text.x < -text.textWidth ) {
                    text.x = WIDTH;
                }
            }
            
        }
        
        public function SetScrollFlag( flg:Boolean ) : void {
            _flg_scroll = flg;
        }
        
    }
    class particle{
    public var x:Number = 0;
    public var y:Number = 0;
    public var vx:Number = 0;
    public var vy:Number = 0;
    public var color:uint = 0xff;
}