[AS3 SNIPPET] Video loop class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package com.rblab.framework.video
{  
    import flash.events.AsyncErrorEvent;
    import flash.events.NetStatusEvent;
    import flash.media.Video;
    import flash.net.NetConnection;
    import flash.net.NetStream;

   
    public class VideoLoop extends Video
    {  
        //--------------------------------------------------------------------------
        //
        //  Private properties
        //
        //--------------------------------------------------------------------------

        private var _URL : String;  
        private var _connection : NetConnection;
        private var _stream : NetStream;
        private var _playedOnce : Boolean = false;  

        //--------------------------------------------------------------------------
        //
        //  Constructor
        //
        //--------------------------------------------------------------------------

        public function VideoLoop(w : int, h : int, videoPath : String, streamer: String) : void
        {
            super(w, h);
           
            _URL = videoPath;
           
            _connection = new NetConnection();
            _connection.addEventListener(NetStatusEvent.NET_STATUS, onStatus, false, 0, true);
            _connection.connect(streamer);
            _connection.client = this;
        }

        //--------------------------------------------------------------------------
        //
        //  Private methods
        //
        //--------------------------------------------------------------------------

        private function initVideo() : void
        {
            _stream = new NetStream(_connection);
            _stream.addEventListener(NetStatusEvent.NET_STATUS, onStatus, false, 0, true);
            _stream.addEventListener(AsyncErrorEvent.ASYNC_ERROR, asyncErrorHandler, false, 0, true);
            attachNetStream(_stream);
           
            var infoClient : Object = new Object();
            infoClient.onMetaData = onMetadata;
            infoClient.onBWDone = onBWDone;
            _stream.client = infoClient;
           
            loadVideo(_URL);
        }

        private function loadVideo(url : String) : void
        {
            _URL = url;
            _stream.play(_URL);
        }

        //--------------------------------------------------------------------------
        //
        //  Public methods
        //
        //--------------------------------------------------------------------------

        public function resume() : void
        {
            _stream.resume();
        }  

        public function pause() : void
        {
            _stream.pause();
        }

        //--------------------------------------------------------------------------
        //
        //  Event handlers
        //
        //--------------------------------------------------------------------------

        private function onStatus(nse : NetStatusEvent) : void
        {
            if (nse.info.code == "NetConnection.Connect.Success" && !_playedOnce)
            {
                _playedOnce = true;
                initVideo();
            }
            if (nse.info.code == "NetStream.Play.Stop" && _playedOnce)
            {
                _stream.seek(0);
            }
        }  

        private function asyncErrorHandler(event : AsyncErrorEvent) : void
        {
            trace(event.toString());
        }

        private function onMetadata(event : Object) : void
        {
        }

        public function onFCSubscribe(event : Object) : void
        {
        }
       
        public function onBWDone() : void
        {
        }
    }
}

[AS3 SNIPPET] Url unshortener class using http://untiny.me/ public service

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package com.rblab.framework.net
{
    import flash.events.Event;
    import flash.events.EventDispatcher;
    import flash.events.IOErrorEvent;
    import flash.net.URLLoader;
    import flash.net.URLRequest;
   
    /**
     * Dispatched when the instance has successfully unshortened an url.
     * @eventType flash.events.COMPLETE
     */

    [Event(name="complete", type="flash.events.Event")]
    /**
     * Dispatched when the instance has encountered an error while trying to unshorten your url.
     * @eventType flash.events.IOErrorEvent.IO_ERROR
     */

    [Event(name="ioError", type="flash.events.IOErrorEvent")]
   
    /**
     * URL Unshortener Class using http://untiny.me/ public service
     * For a list of supported services see <a href="http://untiny.me/api/1.0/services/" target="_blank">http://untiny.me/api/1.0/services/</a>
     * @author Sandro Ducceschi [swfjunkie.com, Switzerland]
     */

   
    public class URLUnshortener extends EventDispatcher
    {
        //--------------------------------------------------------------------------
        //
        //  Class variables
        //
        //--------------------------------------------------------------------------
        private static const UNTINY_ME_URL:String = "http://untiny.me/api/1.0/extract?format=text&url=";
        //--------------------------------------------------------------------------
        //
        //  Initialization
        //
        //--------------------------------------------------------------------------
        /**
         * Creates a new URLShortener Instance
         */

        public function URLUnshortener()
        {
            init();
        }
        /**
         * @private
         * Initializes the instance.
         */

        private function init():void
        {
            urlLoader = new URLLoader();
            urlRequest = new URLRequest();  
            urlLoader.addEventListener(Event.COMPLETE,handleComplete);
            urlLoader.addEventListener(IOErrorEvent.IO_ERROR,handleError);
        }
        //--------------------------------------------------------------------------
        //
        //  Variables
        //
        //--------------------------------------------------------------------------
        private var urlLoader:URLLoader;
        private var urlRequest:URLRequest;
        //--------------------------------------------------------------------------
        //
        //  Properties
        //
        //--------------------------------------------------------------------------
        /** @private */
        private var _url:String;
        /**
         * The Unshortened URL
         */

        public function get url():String
        {
            return _url;
        }
       
        //--------------------------------------------------------------------------
        //
        //  API
        //
        //--------------------------------------------------------------------------
       
        /**
         * Unshortens the given URL
         * @param url   Short URL that should be unshortened
         */
 
        public function unshorten(url:String):void
        {
            _url = null;
            urlRequest.url = UNTINY_ME_URL + url;
            urlLoader.load(urlRequest);
        }
       
       
        /**
         * Completely destroys the instance and frees all objects for the garbage
         * collector by setting their references to null.
         */

        public function destroy():void
        {
            _url = null;
            urlLoader = null;
            urlRequest = null;  
            urlLoader.removeEventListener(Event.COMPLETE,handleComplete);
            urlLoader.removeEventListener(IOErrorEvent.IO_ERROR,handleError);
        }
     
        //--------------------------------------------------------------------------
        //
        //  Eventhandling
        //
        //--------------------------------------------------------------------------
        /** @private */
        private function handleComplete(event:Event):void
        {
            if (String(urlLoader.data).indexOf("error") != -1)
                dispatchEvent(new IOErrorEvent(IOErrorEvent.IO_ERROR, false, false, urlLoader.data.toString()));
            else
            {
                _url = urlLoader.data;
                dispatchEvent(new Event(Event.COMPLETE));
            }
        }
       
        /** @private */
        private function handleError(event:IOErrorEvent):void
        {
            dispatchEvent(new IOErrorEvent(IOErrorEvent.IO_ERROR,false,false,"Unshortening failed"));
        }
    }
}

[AS3 SNIPPET] Get points around circumference

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/** Distribute the points clockwise around the circle. */
public static const CLOCKWISE : String = "clockwise";
/** Distribute the points counterclockwise around the circle. */
public static const COUNTERCLOCKWISE : String = "counterclockwise";

/**
 *  This method accepts arguments based on the size and position of your circle
 *  along with the amount of points to distribute around the circle,
 *  what angle to start the first point, which direction to plot the points,
 *  how much of the circumference to use for the distribution, and which direction
 *  around the circle to plot the points.
 *  @example example:
 *
 *  @param centerx The center x position of the circle to place the points around.
 *  @param centery The center y position of the circle to place the points around.
 *  @param radi The radius of the circle to distribute the points around.
 *  @param total The total amount of point to distribute around the circle.
 *  @param startangle [Optional] The starting angle of the first point. This is based on the 0-360 range.
 *  @param arc [Optional] The length of distribution around the circle to evenly distribute the points. This is based on 0-360.
 *  @param dir [Optional] This determines the direction that the points will distribute around the circle.
 *  @param evenDist [Optional] If set to true, AND if you're arc angle is less than 360, this will evently distribute the points around the circle.<br/>If set to true, the points are visually arranged in this manner POINT-SPACE-POINT-SPACE-POINT, if set to false, an extra space will be added after the last point: POINT-SPACE-POINT-SPACE-POINT-SPACE
 *
 *  @return Returns an Array containing Points.
 */

public static function getPointsAroundCircumference(centerx : Number, centery : Number, circleradius : Number, totalpoints : int, startangle : Number = 0, arc : int = 360, pointdirection : String = "clockwise", evendistribution : Boolean = true) : Array
{
    var mpi : Number = Math.PI / 180;
    var startRadians : Number = startangle * mpi;

    var incrementAngle : Number = arc / totalpoints;
    var incrementRadians : Number = incrementAngle * mpi;

    if(arc < 360)
    {
        // this spreads the points out evenly across the arc
        if(evendistribution)
        {
            incrementAngle = arc / (totalpoints - 1);
            incrementRadians = incrementAngle * mpi;
        }
        else
        {
            incrementAngle = arc / totalpoints;
            incrementRadians = incrementAngle * mpi;
        }
    }

    var pts : Array = [];

    while(totalpoints--)
    {
        var xp : Number = centerx + Math.sin(startRadians) * circleradius;
        var yp : Number = centery + Math.cos(startRadians) * circleradius;
        var pt : Object = new Object();
        pt.x = xp;
        pt.y = yp;
        pt.angle = startRadians;
        pts.push(pt);

        if(pointdirection == COUNTERCLOCKWISE)
        {
            startRadians += incrementRadians;
        }
        else
        {
            startRadians -= incrementRadians;
        }
    }

    return pts;
}