source: trunk/fl5/src/com/longtailvideo/jwplayer/media/YouTubeMediaProvider.as @ 501

Revision 501, 6.4 KB checked in by zach, 4 years ago (diff)
  • Updating default skin controlbar to be 20px tall
  • Adding mute initialization to RTMP
  • Repairing numerous problems with SoundMediaProvider
  • Repairing numerous problems with VideoMediaProvider
  • Adding mute initialization to YouTube
  • Adding PlayerVersion code
  • Fixing controlbar idle state handling
  • Adding Color class until Pablo replaces
Line 
1/**
2 * Wrapper for load and playback of Youtube videos through their API.
3 **/
4package com.longtailvideo.jwplayer.media {
5        import com.jeroenwijering.events.*;
6        import com.longtailvideo.jwplayer.events.MediaEvent;
7        import com.longtailvideo.jwplayer.model.PlayerConfig;
8        import com.longtailvideo.jwplayer.model.PlaylistItem;
9        import com.longtailvideo.jwplayer.player.PlayerState;
10        import com.longtailvideo.jwplayer.utils.RootReference;
11       
12        import flash.display.Loader;
13        import flash.events.*;
14        import flash.net.LocalConnection;
15        import flash.net.URLRequest;
16        import flash.system.Security;
17       
18       
19        public class YouTubeMediaProvider extends MediaProvider {
20                /** Loader for loading the YouTube proxy **/
21                private var loader:Loader;
22                /** 'Unique' string to use for proxy connection. **/
23                private var unique:String;
24                /** Connection towards the YT proxy. **/
25                private var outgoing:LocalConnection;
26                /** connection from the YT proxy. **/
27                private var inbound:LocalConnection;
28                /** Save that the meta has been sent. **/
29                private var metasent:Boolean;
30                /** Save that a load call has been sent. **/
31                private var loading:Boolean;
32                /** Save the connection state. **/
33                private var connected:Boolean;
34                /** URL of a custom youtube swf. **/
35                private var location:String;
36               
37               
38                /** Setup YouTube connections and load proxy. **/
39                public function YouTubeMediaProvider(){
40                }
41               
42                public override function initializeMediaProvider(cfg:PlayerConfig):void {
43                        super.initializeMediaProvider(cfg);
44                        _provider = 'youtube';
45                        Security.allowDomain('*');
46                        var url:String = RootReference.root.loaderInfo.url;
47                        if (url.indexOf('http://') == 0) {
48                                unique = Math.random().toString().substr(2);
49                                var str:String = url.substr(0, url.indexOf('.swf'));
50                                location = str.substr(0, str.lastIndexOf('/') + 1) + 'yt.swf?unique=' + unique;
51                        } else {
52                                unique = '1';
53                                location = 'yt.swf';
54                        }
55                        outgoing = new LocalConnection();
56                        outgoing.allowDomain('*');
57                        outgoing.allowInsecureDomain('*');
58                        outgoing.addEventListener(StatusEvent.STATUS, onLocalConnectionStatusChange);
59                        inbound = new LocalConnection();
60                        inbound.allowDomain('*');
61                        inbound.allowInsecureDomain('*');
62                        inbound.addEventListener(StatusEvent.STATUS, onLocalConnectionStatusChange);
63                        //inbound.addEventListener(AsyncErrorEvent.ASYNC_ERROR,errorHandler);
64                        inbound.client = this;
65                        loader = new Loader();
66                        loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, errorHandler);
67                }
68               
69               
70                /** Catch load errors. **/
71                private function errorHandler(evt:ErrorEvent):void {
72                        stop();
73                        sendMediaEvent(MediaEvent.JWPLAYER_MEDIA_ERROR, {message: evt.text});
74                }
75               
76               
77                /** xtract the current ID from a youtube URL **/
78                private function getID(url:String):String {
79                        var arr:Array = url.split('?');
80                        var str:String = '';
81                        for (var i:String in arr) {
82                                if (arr[i].substr(0, 2) == 'v=') {
83                                        str = arr[i].substr(2);
84                                }
85                        }
86                        if (str == '') {
87                                str = url.substr(url.indexOf('/v/') + 3);
88                        }
89                        if (str.indexOf('&') > -1) {
90                                str = str.substr(0, str.indexOf('&'));
91                        }
92                        return str;
93                }
94               
95               
96                /** Load the YouTube movie. **/
97                override public function load(itm:PlaylistItem):void {
98                        _item = itm;
99                        _position = 0;
100                        loading = true;
101                        if (connected) {
102                                if (outgoing) {
103                                        var gid:String = getID(_item.file);
104                                        resize(_config.width, _config.width / 4 * 3);
105                                        outgoing.send('AS3_' + unique, "loadVideoById", gid, _item.start);
106                                        media = loader;
107                                }
108                        } else {
109                                inbound.connect('AS2_' + unique);
110                                loader.load(new URLRequest(location));
111                        }
112                        _config.mute == true ? setVolume(0) : setVolume(_config.volume);
113                        sendMediaEvent(MediaEvent.JWPLAYER_MEDIA_BUFFER, {percentage: 0});
114                        setState(PlayerState.BUFFERING);
115                }
116               
117               
118                /** Pause the YouTube movie. **/
119                override public function pause():void {
120                        outgoing.send('AS3_' + unique, "pauseVideo");
121                        super.pause();
122                }
123               
124               
125                /** Play or pause the video. **/
126                override public function play():void {
127                        outgoing.send('AS3_' + unique, "playVideo");
128                        super.play();
129                }
130               
131               
132                /** SWF loaded; add it to the tree **/
133                public function onSwfLoadComplete():void {
134                        _config.mute == true ? setVolume(0) : setVolume(_config.volume);
135                        connected = true;
136                        if (loading) {
137                                load(_item);
138                        }
139                }
140               
141               
142                /** error was thrown without this handler **/
143                public function onLocalConnectionStatusChange(evt:StatusEvent):void {
144                        // sendMediaEvent(MediaEvent.JWPLAYER_MEDIA_META,{status:evt.code});
145                }
146               
147               
148                /** Catch youtube errors. **/
149                public function onError(erc:String):void {
150                        sendMediaEvent(MediaEvent.JWPLAYER_MEDIA_ERROR, {message: "YouTube error (video not found?):\n" + _item.file});
151                        stop();
152                }
153               
154               
155                /** Catch youtube state changes. **/
156                public function onStateChange(stt:Number):void {
157                        switch (Number(stt)) {
158                                case -1:
159                                        // setState(PlayerState.IDLE);
160                                        break;
161                                case 0:
162                                        if (_config.state != PlayerState.BUFFERING && _config.state != PlayerState.IDLE) {
163                                                setState(PlayerState.IDLE);
164                                                sendMediaEvent(MediaEvent.JWPLAYER_MEDIA_COMPLETE);
165                                        }
166                                        break;
167                                case 1:
168                                        super.play();
169                                        break;
170                                case 2:
171                                        super.pause();
172                                        break;
173                                case 3:
174                                        setState(PlayerState.BUFFERING);
175                                        sendMediaEvent(MediaEvent.JWPLAYER_MEDIA_BUFFER, {percentage: 0});
176                                        break;
177                        }
178                }
179               
180               
181                /** Catch Youtube load changes **/
182                public function onLoadChange(ldd:Number, ttl:Number, off:Number):void {
183                        sendMediaEvent(MediaEvent.JWPLAYER_MEDIA_LOADED, {loaded: ldd, total: ttl, offset: off});
184                }
185               
186               
187                /** Catch Youtube _position changes **/
188                public function onTimeChange(pos:Number, dur:Number):void {
189                        sendMediaEvent(MediaEvent.JWPLAYER_MEDIA_TIME, {_position: pos, duration: dur});
190                        if (!metasent) {
191                                sendMediaEvent(MediaEvent.JWPLAYER_MEDIA_META, {width: 320, height: 240, duration: dur});
192                                metasent = true;
193                        }
194                }
195               
196               
197                /** Resize the YT player. **/
198                public override function resize(wid:Number, hei:Number):void {
199                        outgoing.send('AS3_' + unique, "setSize", wid, hei);
200                }
201               
202               
203                /** Seek to _position. **/
204                override public function seek(pos:Number):void {
205                        outgoing.send('AS3_' + unique, "seekTo", pos);
206                        play();
207                }
208               
209               
210                /** Destroy the youtube video. **/
211                override public function stop():void {
212                        metasent = false;
213                        outgoing.send('AS3_' + unique, "stopVideo");
214                        super.stop();
215                }
216               
217               
218                /** Set the volume level. **/
219                override public function setVolume(pct:Number):void {
220                        outgoing.send('AS3_' + unique, "setVolume", pct);
221                }
222        }
223}
Note: See TracBrowser for help on using the repository browser.