Common Uses of Byte Arrays : ByteArray « Development « Flash / Flex / ActionScript






Common Uses of Byte Arrays

 
package {

    import flash.display.Sprite;
    import flash.utils.ByteArray;
    import flash.media.Sound;
    import flash.media.SoundChannel;
    import flash.media.*;
    import flash.net.URLRequest;
    import flash.events.Event;
    import flash.system.Security;

    public class Main extends Sprite {

        private var _channel:SoundChannel;
        private var _graph:Sprite;

        public function Main() {
        Security.loadPolicyFile("http://java2s.com/crossdomain.xml");
            var sound:Sound = new Sound();
            sound.load(new URLRequest("http://java2s.com/Demo.mp3"), new SoundLoaderContext(1000, true));
            _channel = sound.play();
            _graph = new Sprite();
            _graph.y = 200;
            addChild(_graph);
            addEventListener(Event.ENTER_FRAME, enterFrameHandler);
        }

        private function enterFrameHandler(event:Event):void {
            var bytes:ByteArray = new ByteArray();
            SoundMixer.computeSpectrum(bytes);
            _graph.graphics.clear();
            _graph.graphics.lineStyle(0, 0, 1);
            var plotX:Number = 0;
            for(var i:Number = 0; i < 256; i++) {
                _graph.graphics.lineTo(plotX, bytes.readFloat() * stage.stageHeight / 2);
                plotX += stage.stageWidth / 256;
            }
        }
    }
}

        








Related examples in the same category

1.Creating a Byte Array
2.Write array to ByteArray
3.By default, private properties are not written when an object is serialized
4.Embedding Files as Binary Data
5.Embedding XML at compile time