Changeset 851


Ignore:
Timestamp:
02/24/10 15:09:55 (3 years ago)
Author:
pablo
Message:

Updating plugin for 5.0 and 5.1 compatibility

Location:
branches/plugins/hd
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/plugins/hd/src/com/longtailvideo/plugins/hd/HD.as

    r830 r851  
    11 
    22package com.longtailvideo.plugins.hd { 
    3  
     3         
    44        import com.longtailvideo.jwplayer.events.MediaEvent; 
    55        import com.longtailvideo.jwplayer.events.PlaylistEvent; 
     6        import com.longtailvideo.jwplayer.model.IPlaylist; 
     7        import com.longtailvideo.jwplayer.model.PlaylistItem; 
    68        import com.longtailvideo.jwplayer.player.IPlayer; 
    79        import com.longtailvideo.jwplayer.player.PlayerState; 
     
    911        import com.longtailvideo.jwplayer.plugins.PluginConfig; 
    1012        import com.longtailvideo.jwplayer.utils.Configger; 
    11         import com.longtailvideo.jwplayer.utils.Logger; 
    1213         
    1314        import flash.display.DisplayObject; 
    1415        import flash.display.MovieClip; 
    1516        import flash.events.Event; 
    16  
    17  
     17         
     18         
    1819        /** 
    1920         * HD Plugin; implements an HD toggle. 
    2021         **/ 
    2122        public class HD extends MovieClip implements IPlugin { 
    22  
     23                 
    2324                [Embed(source="../../../../../assets/controlbar.png")] 
    2425                private const ControlbarIcon:Class; 
    2526                [Embed(source="../../../../../assets/dock.png")] 
    2627                private const DockIcon:Class; 
    27  
     28                 
    2829                /** Reference to the player. **/ 
    2930                private var _player:IPlayer; 
     
    3435                /** Reference to the clip on stage. **/ 
    3536                private var _icon:DisplayObject; 
    36                 /** reference to the original file. **/ 
    37                 private var _original:String; 
    3837                /** The current playlist item **/ 
    39                 private var _currentIndex:Number; 
     38                private var _currentItem:PlaylistItem; 
    4039                /** The current position inside a video. **/ 
    4140                private var _position:Number = 0; 
    42  
    43  
     41                 
     42                 
    4443                /** Constructor; nothing going on. **/ 
    4544                public function HD():void { 
    4645                } 
    47  
    48  
     46                 
     47                 
    4948                /** The initialize call is invoked by the player. **/ 
    5049                public function initPlugin(player:IPlayer, cfg:PluginConfig):void { 
     
    5756                        } 
    5857                        _player.addEventListener(MediaEvent.JWPLAYER_MEDIA_TIME, timeHandler); 
    59                         _player.addEventListener(PlaylistEvent.JWPLAYER_PLAYLIST_ITEM, itemLocker); 
     58                        _player.addEventListener(PlaylistEvent.JWPLAYER_PLAYLIST_ITEM, itemHandler); 
    6059                        if (config.state) { 
    6160                                config.file = config.file; 
     
    7877                        setUI(); 
    7978                } 
    80  
    81  
     79                 
     80                 
    8281                /** Upon resize, check for fullscreen switches. Switch the state if so. **/ 
    8382                public function resize(width:Number, height:Number):void { 
    84                         if (config.fullscreen && (_player.fullscreen != _player.state && _player.state != PlayerState.IDLE)) { 
     83                        if (config.fullscreen && (_player.fullscreen != config.state && _player.state != PlayerState.IDLE)) { 
    8584                                clickHandler(); 
    8685                        } 
    8786                } 
    88  
     87                 
    8988                 
    9089                public function get config():PluginConfig { 
     
    9291                } 
    9392                 
    94  
     93                 
    9594                public function get id():String { 
    9695                        return _config.id; 
    9796                } 
    98  
    99  
     97                 
     98                 
    10099                /** Save the position inside a video. **/ 
    101100                private function timeHandler(evt:MediaEvent):void { 
    102101                        _position = evt.position; 
    103102                } 
    104  
    105  
     103                 
    106104                /** HD _button is clicked, so change the video. **/ 
    107105                private function clickHandler(evt:Event=null):void { 
     
    111109                        setUI(); 
    112110                } 
    113  
    114  
    115                 /** Locks the player and updates the current item **/ 
    116                 private function itemLocker(evt:PlaylistEvent):void { 
    117                         if (_currentIndex != _player.playlist.currentIndex) { 
    118                                 if (_original) { 
    119                                         _player.playlist.getItemAt(_currentIndex).file = _original; 
    120                                 } 
    121                                 _currentIndex = _player.playlist.currentIndex; 
    122                                 _original = _player.playlist.getItemAt(_currentIndex).file; 
    123                                 _position = 0; 
    124                                 _player.lock(this, itemHandler); 
    125                         } 
    126                 } 
    127  
    128  
    129                 /** Updates the playlist with either the HD or default video. **/ 
    130                 private function itemHandler():void { 
    131                         var updated:Boolean; 
    132  
    133                         if (config.state == false && _player.playlist.currentItem.file != _original) { 
    134                                 _player.playlist.currentItem.file = _original; 
    135                                 updated = true; 
    136                                 Logger.log("Switching to original"); 
    137                         } else if (config.state == true && _player.playlist.currentItem.file != config.file) { 
    138                                 _player.playlist.currentItem.file = config.file; 
    139                                 updated = true; 
    140                                 Logger.log("Switching to HD"); 
    141                         } 
    142                          
    143                         if (updated) { 
    144                                 _player.load(_player.playlist.currentItem); 
    145                                 _player.play(); 
    146                                 _player.seek(_position); 
    147                         } 
    148                          
    149                         if (_player.locked) { 
    150                                 _player.unlock(this); 
    151                         }  
    152                 } 
    153  
    154  
     111                 
    155112                /** Set the HD _button state. **/ 
    156113                private function setUI():void { 
    157                         if (config.state == false) { 
     114                        if (config.file || (_currentItem && _currentItem.hasOwnProperty('hd.file'))) { 
     115                                _button.field.alpha = 1; 
     116                                if (config.state == false) { 
     117                                        if (_button) { 
     118                                                _button.field.text = 'is off'; 
     119                                                _icon.alpha = 1; 
     120                                        } else { 
     121                                                _icon.alpha = 0.3; 
     122                                        } 
     123                                } else { 
     124                                        _icon.alpha = 1; 
     125                                        if (_button) { 
     126                                                _button.field.text = 'is on'; 
     127                                        } 
     128                                } 
     129                        } else { 
     130                                _icon.alpha = 0.5; 
    158131                                if (_button) { 
    159                                         _button.field.text = 'is off'; 
    160                                 } else { 
    161                                         _icon.alpha = 0.3; 
    162                                 } 
    163                         } else { 
    164                                 if (_button) { 
    165                                         _button.field.text = 'is on'; 
    166                                 } else { 
    167                                         _icon.alpha = 1; 
    168                                 } 
    169                         } 
     132                                        _button.field.text = 'not avail'; 
     133                                        _button.field.alpha = 0.5; 
     134                                } 
     135                        } 
     136                } 
     137 
     138                /** Updates the playlist with either the HD or default video. **/ 
     139                private function itemHandler(evt:Event=null):void { 
     140                        if (_currentItem != _player.playlist.currentItem) { 
     141                                _currentItem = _player.playlist.currentItem; 
     142                        }  
     143 
     144                        var swapFile:String = stateFile(); 
     145                        if (swapFile) { 
     146                                if (_currentItem.file != swapFile) { 
     147                                        swap(swapFile); 
     148                                } 
     149                        } 
     150                        setUI(); 
     151                         
     152                } 
     153                 
     154 
     155                /** Switch the currently playing file with a new one. **/ 
     156                private function swap(newFile:String):void { 
     157                        var newList:Array = playlistToArray(_player.playlist); 
     158                        var newItem:PlaylistItem = newList[_player.playlist.currentIndex] as PlaylistItem; 
     159                        if (!newItem.hasOwnProperty('hd.originalfile')) { 
     160                                newItem['hd.originalfile'] = newItem.file;       
     161                        }  
     162                        newItem.file = newFile; 
     163                         
     164                        // Load a new playlist instead of changing the item's file property. 
     165                        // This is necessary due to a bug in 5.0 that doesn't load a new file if the PlaylistItem object doesn't change. 
     166                        _player.load(newList); 
     167                        _player.play(); 
     168                        _player.seek(_position); 
     169                } 
     170                 
     171                /** Which file to play, based on the current state **/ 
     172                private function stateFile():String { 
     173                        if (config.state) { 
     174                                if (_currentItem.hasOwnProperty('hd.file')) { 
     175                                        return _currentItem['hd.file']; 
     176                                } else if (config.hasOwnProperty('file')) {  
     177                                        return config['file']; 
     178                                } else { 
     179                                        return ''; 
     180                                } 
     181                        } else { 
     182                                if (_currentItem.hasOwnProperty('hd.originalfile')) { 
     183                                        return _currentItem['hd.originalfile']; 
     184                                } else if (_currentItem.hasOwnProperty('hd.file') || config.hasOwnProperty('file')) { 
     185                                        return _currentItem.file; 
     186                                } else { 
     187                                        return ''; 
     188                                } 
     189                        } 
     190                } 
     191                 
     192                /** Clone a playlist's items and put everything in an array **/ 
     193                private function playlistToArray(source:IPlaylist):Array { 
     194                        var playlistContents:Array = []; 
     195                        for (var i:Number = 0; i < source.length; i++) { 
     196                                playlistContents.push(clonePlaylistItem(source.getItemAt(i))); 
     197                        } 
     198                        return playlistContents; 
     199                } 
     200                 
     201                /** Create a copy of a PlaylistItem **/ 
     202                private function clonePlaylistItem(source:PlaylistItem):PlaylistItem { 
     203                        var newItem:PlaylistItem = new PlaylistItem(source); 
     204                        newItem.author = source.author; 
     205                        newItem.date = source.date; 
     206                        newItem.description = source.description; 
     207                        newItem.duration = source.duration; 
     208                        newItem.file = source.file; 
     209                        newItem.image = source.image; 
     210                        newItem.link = source.link; 
     211                        newItem.mediaid = source.mediaid; 
     212                        newItem.provider = source.provider; 
     213                        newItem.start = source.start; 
     214                        newItem.tags = source.tags; 
     215                        newItem.title = source.title; 
     216                        return newItem; 
    170217                } 
    171218        } 
Note: See TracChangeset for help on using the changeset viewer.