| 1 | package { |
|---|
| 2 | |
|---|
| 3 | import com.longtailvideo.jwplayer.events.*; |
|---|
| 4 | import com.longtailvideo.jwplayer.player.*; |
|---|
| 5 | import com.longtailvideo.jwplayer.plugins.*; |
|---|
| 6 | import com.longtailvideo.jwplayer.utils.*; |
|---|
| 7 | import com.longtailvideo.jwplayer.parsers.*; |
|---|
| 8 | |
|---|
| 9 | import flash.display.*; |
|---|
| 10 | import flash.events.*; |
|---|
| 11 | import flash.filters.DropShadowFilter; |
|---|
| 12 | import flash.net.*; |
|---|
| 13 | import flash.text.*; |
|---|
| 14 | import flash.utils.setTimeout; |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | /** This plugin displays an overlay with related videos. **/ |
|---|
| 18 | public class Related extends Sprite implements IPlugin { |
|---|
| 19 | |
|---|
| 20 | |
|---|
| 21 | /** Embedding the image assets. **/ |
|---|
| 22 | [Embed(source="../../assets/icon.png")] |
|---|
| 23 | private const DockIcon:Class; |
|---|
| 24 | [Embed(source="../../assets/sheet.png")] |
|---|
| 25 | private const BackSheet:Class; |
|---|
| 26 | [Embed(source="../../assets/replay.png")] |
|---|
| 27 | private const ReplayButton:Class; |
|---|
| 28 | [Embed(source="../../assets/close.png")] |
|---|
| 29 | private const CloseButton:Class; |
|---|
| 30 | |
|---|
| 31 | |
|---|
| 32 | /** Reference to the background sheet. **/ |
|---|
| 33 | private var _back:Sprite; |
|---|
| 34 | /** Reference to the dock button. **/ |
|---|
| 35 | private var _button:MovieClip; |
|---|
| 36 | /** The plugin configuration options.**/ |
|---|
| 37 | private var _config:Object; |
|---|
| 38 | /** Clip with all graphics. **/ |
|---|
| 39 | private var _container:MovieClip; |
|---|
| 40 | /** Default dimensions for the grid. **/ |
|---|
| 41 | private var _dimensions:Array; |
|---|
| 42 | /** Link to the mRSS file with related videos. **/ |
|---|
| 43 | private var _file:String; |
|---|
| 44 | /** The grid with all thumbs. **/ |
|---|
| 45 | private var _grid:Sprite; |
|---|
| 46 | /** The CTA text heading. **/ |
|---|
| 47 | private var _heading:TextField; |
|---|
| 48 | /** Reference to the dock icon. **/ |
|---|
| 49 | private var _icon:DisplayObject; |
|---|
| 50 | /** Component that loads the related videos. **/ |
|---|
| 51 | private var _loader:URLLoader; |
|---|
| 52 | /** Component that parses the related videos. **/ |
|---|
| 53 | private var _parser:RSSParser; |
|---|
| 54 | /** Reference to the player. **/ |
|---|
| 55 | private var _player:IPlayer; |
|---|
| 56 | /** Reference to the replay button. **/ |
|---|
| 57 | private var _replay:Sprite; |
|---|
| 58 | /** Reference to the close button. **/ |
|---|
| 59 | private var _close:Sprite; |
|---|
| 60 | |
|---|
| 61 | |
|---|
| 62 | /** The background screen was clicked. **/ |
|---|
| 63 | private function _backHandler(evt:MouseEvent):void { hide(); }; |
|---|
| 64 | |
|---|
| 65 | |
|---|
| 66 | /** Display the related items on complete. **/ |
|---|
| 67 | private function _completeHandler(evt:MediaEvent):void { |
|---|
| 68 | if(_config.oncomplete !== false) { |
|---|
| 69 | setTimeout(show,50); |
|---|
| 70 | } |
|---|
| 71 | }; |
|---|
| 72 | |
|---|
| 73 | |
|---|
| 74 | /** The controlbar/dock button was clicked. **/ |
|---|
| 75 | private function _dockHandler(evt:MouseEvent):void { show(); }; |
|---|
| 76 | |
|---|
| 77 | |
|---|
| 78 | /** Loading the RSS feed failed. **/ |
|---|
| 79 | private function _errorHandler(evt:ErrorEvent):void { |
|---|
| 80 | Logger.log(evt.text,id); |
|---|
| 81 | _file = undefined; |
|---|
| 82 | if(_icon) { |
|---|
| 83 | _icon.alpha = 0.5; |
|---|
| 84 | _button.field.alpha = 0.5; |
|---|
| 85 | _button.field.text = 'not set'; |
|---|
| 86 | } |
|---|
| 87 | }; |
|---|
| 88 | |
|---|
| 89 | |
|---|
| 90 | /** Hide the list with related videos. **/ |
|---|
| 91 | public function hide():void { |
|---|
| 92 | _back.alpha = 1; |
|---|
| 93 | new Animations(_container).fade(0,0.2); |
|---|
| 94 | // Only 5.7+... |
|---|
| 95 | try { |
|---|
| 96 | (_player.controls.display as Object).show(); |
|---|
| 97 | (_player.controls.dock as Object).show(); |
|---|
| 98 | } catch (error:Error) {} |
|---|
| 99 | }; |
|---|
| 100 | |
|---|
| 101 | |
|---|
| 102 | /** Returns the plugin name. **/ |
|---|
| 103 | public function get id():String { |
|---|
| 104 | return "related"; |
|---|
| 105 | }; |
|---|
| 106 | |
|---|
| 107 | |
|---|
| 108 | /** Called by the player to initialize; setup events and dock buttons. */ |
|---|
| 109 | public function initPlugin(player:IPlayer, config:PluginConfig):void { |
|---|
| 110 | _player = player; |
|---|
| 111 | _config = config; |
|---|
| 112 | _player.addEventListener(MediaEvent.JWPLAYER_MEDIA_COMPLETE, _completeHandler); |
|---|
| 113 | _player.addEventListener(PlaylistEvent.JWPLAYER_PLAYLIST_ITEM, _itemHandler); |
|---|
| 114 | _loader = new URLLoader(); |
|---|
| 115 | _loader.addEventListener(Event.COMPLETE,_loaderHandler); |
|---|
| 116 | _loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, _errorHandler); |
|---|
| 117 | _loader.addEventListener(IOErrorEvent.IO_ERROR, _errorHandler); |
|---|
| 118 | _parser = new RSSParser(); |
|---|
| 119 | if(_config.usedock !== false) { |
|---|
| 120 | _icon = new DockIcon() |
|---|
| 121 | _button = _player.controls.dock.addButton(_icon, "related", _dockHandler); |
|---|
| 122 | } |
|---|
| 123 | // Add the background. |
|---|
| 124 | _container = new MovieClip(); |
|---|
| 125 | _container.alpha = 0; |
|---|
| 126 | _container.visible = false; |
|---|
| 127 | addChild(_container); |
|---|
| 128 | _back = new Sprite(); |
|---|
| 129 | _back.buttonMode = true; |
|---|
| 130 | _back.addChild(new BackSheet()); |
|---|
| 131 | _back.addEventListener(MouseEvent.CLICK,_backHandler); |
|---|
| 132 | _container.addChild(_back); |
|---|
| 133 | // Add the replay and close buttons. |
|---|
| 134 | _replay = new Sprite(); |
|---|
| 135 | _replay.buttonMode = true; |
|---|
| 136 | _replay.addChild(new ReplayButton()); |
|---|
| 137 | _replay.addEventListener(MouseEvent.CLICK,_replayHandler); |
|---|
| 138 | _container.addChild(_replay); |
|---|
| 139 | _close = new Sprite(); |
|---|
| 140 | _close.buttonMode = true; |
|---|
| 141 | _close.addChild(new CloseButton()); |
|---|
| 142 | _close.addEventListener(MouseEvent.CLICK,_backHandler); |
|---|
| 143 | _container.addChild(_close); |
|---|
| 144 | // Add the text heading. |
|---|
| 145 | _heading = new TextField(); |
|---|
| 146 | _heading.height = 30; |
|---|
| 147 | _heading.defaultTextFormat = new TextFormat('Arial', 16, 0xFFFFFF); |
|---|
| 148 | _heading.autoSize = 'center'; |
|---|
| 149 | _heading.multiline = false; |
|---|
| 150 | _heading.selectable = false; |
|---|
| 151 | _heading.filters = new Array(new DropShadowFilter(1,45,0,1,1,1,1)); |
|---|
| 152 | if(_config.heading !== undefined) { |
|---|
| 153 | _heading.htmlText = _config.heading; |
|---|
| 154 | } else { |
|---|
| 155 | _heading.htmlText = "Watch related videos"; |
|---|
| 156 | } |
|---|
| 157 | _container.addChild(_heading); |
|---|
| 158 | // Add the grid for thumbs |
|---|
| 159 | _grid = new Sprite(); |
|---|
| 160 | _container.addChild(_grid); |
|---|
| 161 | }; |
|---|
| 162 | |
|---|
| 163 | |
|---|
| 164 | /** Find the related thumbs feed when an item loads. */ |
|---|
| 165 | public function _itemHandler(evt:PlaylistEvent):void { |
|---|
| 166 | // Reset old data |
|---|
| 167 | _file = undefined; |
|---|
| 168 | while(_grid.numChildren > 0) { |
|---|
| 169 | _grid.removeChildAt(0); |
|---|
| 170 | } |
|---|
| 171 | hide(); |
|---|
| 172 | // Check for new file |
|---|
| 173 | if(_player.playlist.currentItem['related.file']) { |
|---|
| 174 | _file = _player.playlist.currentItem['related.file']; |
|---|
| 175 | } else if (_config['file']) { |
|---|
| 176 | _file = _config['file']; |
|---|
| 177 | } |
|---|
| 178 | // Load the mRSS feed and set the dock icon |
|---|
| 179 | if(_file) { |
|---|
| 180 | _loader.load(new URLRequest(_file)); |
|---|
| 181 | if(_icon) { |
|---|
| 182 | _icon.alpha = 1; |
|---|
| 183 | _button.field.alpha = 1; |
|---|
| 184 | _button.field.text = 'related'; |
|---|
| 185 | } |
|---|
| 186 | } else { |
|---|
| 187 | _errorHandler(new ErrorEvent(ErrorEvent.ERROR,false,false, |
|---|
| 188 | "This playlist entry has no related videos.")); |
|---|
| 189 | } |
|---|
| 190 | }; |
|---|
| 191 | |
|---|
| 192 | |
|---|
| 193 | /** Loader has loaded the mRSS feed. **/ |
|---|
| 194 | private function _loaderHandler(evt:Event):void { |
|---|
| 195 | try { |
|---|
| 196 | var xml:XML = XML(evt.target.data); |
|---|
| 197 | var rss:Array = _parser.parse(xml); |
|---|
| 198 | } catch (error:Error) { |
|---|
| 199 | _errorHandler(new ErrorEvent(ErrorEvent.ERROR,false,false,"This feed is not valid XML and/or RSS.")); |
|---|
| 200 | return; |
|---|
| 201 | } |
|---|
| 202 | var related:Array = new Array(); |
|---|
| 203 | for (var i:Number = 0; i < rss.length; i++) { |
|---|
| 204 | if(rss[i].image && rss[i].link && rss[i].title) { |
|---|
| 205 | var entry:Object = { |
|---|
| 206 | image: rss[i].image, |
|---|
| 207 | link: rss[i].link |
|---|
| 208 | } |
|---|
| 209 | if(_config.titles !== false) { |
|---|
| 210 | entry.title = rss[i].title |
|---|
| 211 | } |
|---|
| 212 | related.push(entry); |
|---|
| 213 | } |
|---|
| 214 | } |
|---|
| 215 | if(related.length) { |
|---|
| 216 | var col:Number = 0; |
|---|
| 217 | var row:Number = 0; |
|---|
| 218 | for(var j:Number = 0; j < related.length; j++) { |
|---|
| 219 | var thumb:RelatedThumb = new RelatedThumb( |
|---|
| 220 | _dimensions[0], |
|---|
| 221 | _dimensions[1], |
|---|
| 222 | related[j].image, |
|---|
| 223 | related[j].link, |
|---|
| 224 | related[j].title |
|---|
| 225 | ); |
|---|
| 226 | thumb.x = (_dimensions[0]+10) * col; |
|---|
| 227 | thumb.y = (_dimensions[1]+10) * row; |
|---|
| 228 | _grid.addChild(thumb); |
|---|
| 229 | if(col == _dimensions[2]-1 || |
|---|
| 230 | (_dimensions[0]+10)*(col+2) > _dimensions[4]) { |
|---|
| 231 | if(row == _dimensions[3]-1 || |
|---|
| 232 | (_dimensions[1]+10)*(row+2) > _dimensions[5]-40) { |
|---|
| 233 | break; |
|---|
| 234 | } else { |
|---|
| 235 | row++; |
|---|
| 236 | col = 0; |
|---|
| 237 | } |
|---|
| 238 | } else { |
|---|
| 239 | col++; |
|---|
| 240 | } |
|---|
| 241 | } |
|---|
| 242 | resize(_dimensions[4],_dimensions[5]); |
|---|
| 243 | } else { |
|---|
| 244 | _errorHandler(new ErrorEvent(ErrorEvent.ERROR,false,false, |
|---|
| 245 | "RSS feed has 0 entries that contain title,link and image.")); |
|---|
| 246 | } |
|---|
| 247 | }; |
|---|
| 248 | |
|---|
| 249 | |
|---|
| 250 | /** The replay button was clicked. **/ |
|---|
| 251 | private function _replayHandler(evt:MouseEvent):void { |
|---|
| 252 | hide(); |
|---|
| 253 | _player.seek(0); |
|---|
| 254 | }; |
|---|
| 255 | |
|---|
| 256 | |
|---|
| 257 | /** Reposition the screens when the player resizes itself **/ |
|---|
| 258 | public function resize(width:Number, height:Number):void { |
|---|
| 259 | // Do regular resize stuff |
|---|
| 260 | _back.width = width; |
|---|
| 261 | _back.height = height; |
|---|
| 262 | _close.x = width - 50; |
|---|
| 263 | _grid.x = Math.round(width/2 - _grid.width/2); |
|---|
| 264 | _grid.y = Math.round(height/2 - _grid.height/2) + 15; |
|---|
| 265 | _heading.y = _grid.y - 30; |
|---|
| 266 | _heading.x = Math.round(width/2 - _heading.width/2); |
|---|
| 267 | // Store thumb dimensions on first resize. |
|---|
| 268 | _dimensions = [140,80,5,2,width,height]; |
|---|
| 269 | if(_config.dimensions) { |
|---|
| 270 | var dim:Array = _config.dimensions.split(','); |
|---|
| 271 | for(var i:Number=0; i<dim.length; i++) { |
|---|
| 272 | _dimensions[i] = Number(dim[i]); |
|---|
| 273 | } |
|---|
| 274 | } |
|---|
| 275 | }; |
|---|
| 276 | |
|---|
| 277 | |
|---|
| 278 | /** Show the list with related videos. **/ |
|---|
| 279 | public function show():void { |
|---|
| 280 | if(_file) { |
|---|
| 281 | new Animations(_container).fade(1,0.2); |
|---|
| 282 | _player.pause(); |
|---|
| 283 | // Only 5.7+... |
|---|
| 284 | try { |
|---|
| 285 | (_player.controls.display as Object).hide(); |
|---|
| 286 | (_player.controls.dock as Object).hide(); |
|---|
| 287 | } catch (error:Error) {} |
|---|
| 288 | } |
|---|
| 289 | }; |
|---|
| 290 | |
|---|
| 291 | |
|---|
| 292 | } |
|---|
| 293 | } |
|---|