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

Revision 519, 6.2 KB checked in by zach, 4 years ago (diff)
  • Updating SoundMediaProvider to handle null channels
  • Updating VideoMediaProvider play / pause bug
  • Fixing YouTube buffering issue
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 a load call has been sent. **/
29                private var loading:Boolean;
30                /** Save the connection state. **/
31                private var connected:Boolean;
32               
33               
34                /** Setup YouTube connections and load proxy. **/
35                public function YouTubeMediaProvider() {
36                }
37               
38               
39                public override function initializeMediaProvider(cfg:PlayerConfig):void {
40                        super.initializeMediaProvider(cfg);
41                        _provider = 'youtube';
42                        Security.allowDomain('*');
43                        outgoing = new LocalConnection();
44                        outgoing.allowDomain('*');
45                        outgoing.allowInsecureDomain('*');
46                        outgoing.addEventListener(StatusEvent.STATUS, onLocalConnectionStatusChange);
47                        inbound = new LocalConnection();
48                        inbound.allowDomain('*');
49                        inbound.allowInsecureDomain('*');
50                        inbound.addEventListener(StatusEvent.STATUS, onLocalConnectionStatusChange);
51                        inbound.client = this;
52                        loader = new Loader();
53                        loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, errorHandler);
54                }
55               
56               
57                /** Catch load errors. **/
58                private function errorHandler(evt:ErrorEvent):void {
59                        stop();
60                        sendMediaEvent(MediaEvent.JWPLAYER_MEDIA_ERROR, {message: evt.text});
61                }
62               
63               
64                /** xtract the current ID from a youtube URL **/
65                private function getID(url:String):String {
66                        var arr:Array = url.split('?');
67                        var str:String = '';
68                        for (var i:String in arr) {
69                                if (arr[i].substr(0, 2) == 'v=') {
70                                        str = arr[i].substr(2);
71                                }
72                        }
73                        if (str == '') {
74                                str = url.substr(url.indexOf('/v/') + 3);
75                        }
76                        if (str.indexOf('&') > -1) {
77                                str = str.substr(0, str.indexOf('&'));
78                        }
79                        return str;
80                }
81               
82               
83                /** Get the location of yt.swf. **/
84                private function getLocation():String {
85                        var loc:String;
86                        var url:String = RootReference.stage.loaderInfo.url;
87                        if (url.indexOf('http://') == 0) {
88                                unique = Math.random().toString().substr(2);
89                                loc = url.substr(0, url.indexOf('.swf'));
90                                loc = loc.substr(0, loc.lastIndexOf('/') + 1) + 'yt.swf?unique=' + unique;
91                        } else {
92                                unique = '1';
93                                loc = 'yt.swf';
94                        }
95                        return loc;
96                }
97               
98               
99                /** Load the YouTube movie. **/
100                override public function load(itm:PlaylistItem):void {
101                        _item = itm;
102                        _position = 0;
103                        loading = true;
104                        if (connected) {
105                                if (outgoing) {
106                                        var gid:String = getID(_item.file);
107                                        outgoing.send('AS3_' + unique, "cueVideoById", gid, _item.start);
108                                        resize(_config.width, _config.width / 4 * 3);
109                                        media = loader;
110                                        sendMediaEvent(MediaEvent.JWPLAYER_MEDIA_LOADED);
111                                        _config.mute == true ? setVolume(0) : setVolume(_config.volume);
112                                        setState(PlayerState.BUFFERING);
113                                        sendBufferEvent(0);
114                                        sendMediaEvent(MediaEvent.JWPLAYER_MEDIA_BUFFER_FULL);
115                                }
116                        } else {
117                                loader.load(new URLRequest(getLocation()));
118                                inbound.connect('AS2_' + unique);
119                        }
120                }
121               
122               
123                /** Pause the YouTube movie. **/
124                override public function pause():void {
125                        outgoing.send('AS3_' + unique, "pauseVideo");
126                        super.pause();
127                }
128               
129               
130                /** Play or pause the video. **/
131                override public function play():void {
132                        outgoing.send('AS3_' + unique, "playVideo");
133                        super.play();
134                }
135               
136               
137                /** SWF loaded; add it to the tree **/
138                public function onSwfLoadComplete():void {
139                        connected = true;
140                        if (loading) {
141                                load(_item);
142                        }
143                }
144               
145               
146                /** error was thrown without this handler **/
147                public function onLocalConnectionStatusChange(evt:StatusEvent):void {
148                        // sendMediaEvent(MediaEvent.JWPLAYER_MEDIA_META,{status:evt.code});
149                }
150               
151               
152                /** Catch youtube errors. **/
153                public function onError(erc:Number):void {
154                        stop();
155                        var msg:String = 'Video not found or deleted: ' + getID(item['file']);
156                        if (erc == 101 || erc == 150) {
157                                msg = 'Embedding this video is disabled by its owner.';
158                        }
159                        sendMediaEvent(MediaEvent.JWPLAYER_MEDIA_ERROR, {message: msg});
160                }
161               
162               
163                /** Catch youtube state changes. **/
164                public function onStateChange(stt:Number):void {
165                        switch (Number(stt)) {
166                                case -1:
167                                        // setState(PlayerState.IDLE);
168                                        break;
169                                case 0:
170                                        if (_config.state != PlayerState.BUFFERING && _config.state != PlayerState.IDLE) {
171                                                setState(PlayerState.IDLE);
172                                                sendMediaEvent(MediaEvent.JWPLAYER_MEDIA_COMPLETE);
173                                        }
174                                        break;
175                                case 1:
176                                        super.play();
177                                        break;
178                                case 2:
179                                        super.pause();
180                                        break;
181                                case 3:
182                                        setState(PlayerState.BUFFERING);
183                                        break;
184                        }
185                }
186               
187               
188                /** Catch Youtube load changes **/
189                public function onLoadChange(ldd:Number, ttl:Number, off:Number):void {
190                        sendBufferEvent(ldd / ttl * 100);
191                }
192               
193               
194                /** Catch Youtube _position changes **/
195                public function onTimeChange(pos:Number, dur:Number):void {
196                        sendMediaEvent(MediaEvent.JWPLAYER_MEDIA_TIME, {position: pos, duration: dur});
197                        if (item.duration < 0) {
198                                item.duration = dur;
199                        }
200                }
201               
202               
203                /** Resize the YT player. **/
204                public override function resize(wid:Number, hei:Number):void {
205                        outgoing.send('AS3_' + unique, "setSize", wid, hei);
206                }
207               
208               
209                /** Seek to _position. **/
210                override public function seek(pos:Number):void {
211                        outgoing.send('AS3_' + unique, "seekTo", pos);
212                        play();
213                }
214               
215               
216                /** Destroy the youtube video. **/
217                override public function stop():void {
218                        if (connected) {
219                                outgoing.send('AS3_' + unique, "stopVideo");
220                        } else {
221                                loading = false;
222                        }
223                        super.stop();
224                }
225               
226               
227                /** Set the volume level. **/
228                override public function setVolume(pct:Number):void {
229                        outgoing.send('AS3_' + unique, "setVolume", pct);
230                }
231        }
232}
Note: See TracBrowser for help on using the repository browser.