Ignore:
Timestamp:
10/21/09 16:22:59 (4 years ago)
Author:
zach
Message:

Fixing YouTube seeking

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/fl5/src/com/longtailvideo/jwplayer/media/YouTubeMediaProvider.as

    r501 r511  
    2626                /** connection from the YT proxy. **/ 
    2727                private var inbound:LocalConnection; 
    28                 /** Save that the meta has been sent. **/ 
    29                 private var metasent:Boolean; 
    3028                /** Save that a load call has been sent. **/ 
    3129                private var loading:Boolean; 
    3230                /** Save the connection state. **/ 
    3331                private var connected:Boolean; 
    34                 /** URL of a custom youtube swf. **/ 
    35                 private var location:String; 
    3632                 
    3733                 
    3834                /** Setup YouTube connections and load proxy. **/ 
    39                 public function YouTubeMediaProvider(){ 
    40                 } 
     35                public function YouTubeMediaProvider() { 
     36                } 
     37                 
    4138                 
    4239                public override function initializeMediaProvider(cfg:PlayerConfig):void { 
     
    4441                        _provider = 'youtube'; 
    4542                        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                         } 
    5543                        outgoing = new LocalConnection(); 
    5644                        outgoing.allowDomain('*'); 
     
    6149                        inbound.allowInsecureDomain('*'); 
    6250                        inbound.addEventListener(StatusEvent.STATUS, onLocalConnectionStatusChange); 
    63                         //inbound.addEventListener(AsyncErrorEvent.ASYNC_ERROR,errorHandler); 
    6451                        inbound.client = this; 
    6552                        loader = new Loader(); 
     
    9481                 
    9582                 
     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                 
    9699                /** Load the YouTube movie. **/ 
    97100                override public function load(itm:PlaylistItem):void { 
     
    102105                                if (outgoing) { 
    103106                                        var gid:String = getID(_item.file); 
     107                                        outgoing.send('AS3_' + unique, "loadVideoById", gid, _item.start); 
    104108                                        resize(_config.width, _config.width / 4 * 3); 
    105                                         outgoing.send('AS3_' + unique, "loadVideoById", gid, _item.start); 
    106109                                        media = loader; 
    107110                                } 
    108111                        } else { 
     112                                loader.load(new URLRequest(getLocation())); 
    109113                                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); 
     114                        } 
    115115                } 
    116116                 
     
    132132                /** SWF loaded; add it to the tree **/ 
    133133                public function onSwfLoadComplete():void { 
    134                         _config.mute == true ? setVolume(0) : setVolume(_config.volume); 
    135134                        connected = true; 
    136135                        if (loading) { 
     
    147146                 
    148147                /** 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}); 
     148                public function onError(erc:Number):void { 
    151149                        stop(); 
     150                        var msg:String = 'Video not found or deleted: ' + getID(item['file']); 
     151                        if (erc == 101 || erc == 150) { 
     152                                msg = 'Embedding this video is disabled by its owner.'; 
     153                        } 
     154                        sendMediaEvent(MediaEvent.JWPLAYER_MEDIA_ERROR, {message: msg}); 
    152155                } 
    153156                 
     
    173176                                case 3: 
    174177                                        setState(PlayerState.BUFFERING); 
    175                                         sendMediaEvent(MediaEvent.JWPLAYER_MEDIA_BUFFER, {percentage: 0}); 
    176178                                        break; 
    177179                        } 
     
    181183                /** Catch Youtube load changes **/ 
    182184                public function onLoadChange(ldd:Number, ttl:Number, off:Number):void { 
    183                         sendMediaEvent(MediaEvent.JWPLAYER_MEDIA_LOADED, {loaded: ldd, total: ttl, offset: off}); 
     185                        //sendMediaEvent(MediaEvent.JWPLAYER_MEDIA_LOADED, {loaded: ldd, total: ttl, offset: off}); 
     186                        sendMediaEvent(MediaEvent.JWPLAYER_MEDIA_LOADED); 
     187                        _config.mute == true ? setVolume(0) : setVolume(_config.volume); 
     188                        setState(PlayerState.BUFFERING); 
     189                        sendBufferEvent(0); 
    184190                } 
    185191                 
     
    187193                /** Catch Youtube _position changes **/ 
    188194                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; 
     195                        if (state != PlayerState.PLAYING) { 
     196                                setState(PlayerState.PLAYING); 
     197                        } 
     198                        sendMediaEvent(MediaEvent.JWPLAYER_MEDIA_TIME, {position: pos, duration: dur}); 
     199                        if (item.duration <= 0) { 
     200                                item.duration = dur; 
    193201                        } 
    194202                } 
     
    210218                /** Destroy the youtube video. **/ 
    211219                override public function stop():void { 
    212                         metasent = false; 
    213                         outgoing.send('AS3_' + unique, "stopVideo"); 
     220                        if (connected) { 
     221                                outgoing.send('AS3_' + unique, "stopVideo"); 
     222                        } else { 
     223                                loading = false; 
     224                        } 
    214225                        super.stop(); 
    215226                } 
Note: See TracChangeset for help on using the changeset viewer.