| 1 | /** |
|---|
| 2 | * Wrapper for load and playback of Youtube videos. |
|---|
| 3 | **/ |
|---|
| 4 | package com.jeroenwijering.models { |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | import com.jeroenwijering.events.*; |
|---|
| 8 | import com.jeroenwijering.models.ModelInterface; |
|---|
| 9 | import com.jeroenwijering.player.Model; |
|---|
| 10 | import flash.display.Sprite; |
|---|
| 11 | import flash.display.Loader; |
|---|
| 12 | import flash.net.URLRequest; |
|---|
| 13 | import flash.events.*; |
|---|
| 14 | import flash.net.LocalConnection; |
|---|
| 15 | import flash.utils.setInterval; |
|---|
| 16 | import flash.utils.setTimeout; |
|---|
| 17 | import flash.utils.clearInterval; |
|---|
| 18 | |
|---|
| 19 | |
|---|
| 20 | public class YoutubeModel implements ModelInterface { |
|---|
| 21 | |
|---|
| 22 | |
|---|
| 23 | /** Reference to the Model **/ |
|---|
| 24 | private var model:Model; |
|---|
| 25 | /** Location of the YT proxy **/ |
|---|
| 26 | private var proxy:String = "http://www.jeroenwijering.com/embed/yt.swf"; |
|---|
| 27 | /** Loader for loading the YouTube proxy **/ |
|---|
| 28 | private var loader:Loader; |
|---|
| 29 | /** Connection towards the YT proxy. **/ |
|---|
| 30 | private var outgoing:LocalConnection; |
|---|
| 31 | /** connection from the YT proxy. **/ |
|---|
| 32 | private var inbound:LocalConnection; |
|---|
| 33 | /** Save that the meta has been sent. **/ |
|---|
| 34 | private var metasent:Boolean; |
|---|
| 35 | /** Save that a load call has been sent. **/ |
|---|
| 36 | private var loading:Boolean; |
|---|
| 37 | |
|---|
| 38 | |
|---|
| 39 | /** Setup YouTube connections and load proxy. **/ |
|---|
| 40 | public function YoutubeModel(mod:Model) { |
|---|
| 41 | model = mod; |
|---|
| 42 | outgoing = new LocalConnection(); |
|---|
| 43 | outgoing.allowDomain('*'); |
|---|
| 44 | outgoing.allowInsecureDomain('*'); |
|---|
| 45 | outgoing.addEventListener(StatusEvent.STATUS,onLocalConnectionStatusChange); |
|---|
| 46 | inbound = new LocalConnection(); |
|---|
| 47 | inbound.allowDomain('*'); |
|---|
| 48 | inbound.allowInsecureDomain('*'); |
|---|
| 49 | inbound.addEventListener(StatusEvent.STATUS,onLocalConnectionStatusChange); |
|---|
| 50 | inbound.client = this; |
|---|
| 51 | inbound.connect("_AS2_to_AS3"); |
|---|
| 52 | loader = new Loader(); |
|---|
| 53 | loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,errorHandler); |
|---|
| 54 | loader.load(new URLRequest(proxy)); |
|---|
| 55 | }; |
|---|
| 56 | |
|---|
| 57 | |
|---|
| 58 | /** Catch load errors. **/ |
|---|
| 59 | private function errorHandler(evt:ErrorEvent) { |
|---|
| 60 | model.sendEvent(ModelEvent.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 = url.split('?'); |
|---|
| 67 | var str = ''; |
|---|
| 68 | for (var i in arr) { |
|---|
| 69 | if(arr[i].substr(0,2) == 'v=') { |
|---|
| 70 | str = arr[i].substr(2); |
|---|
| 71 | } |
|---|
| 72 | } |
|---|
| 73 | if(str == '') { str = url.substr(url.indexOf('/v/')+3); } |
|---|
| 74 | return str; |
|---|
| 75 | }; |
|---|
| 76 | |
|---|
| 77 | |
|---|
| 78 | /** Load the YouTube movie. **/ |
|---|
| 79 | public function load() { |
|---|
| 80 | model.sendEvent(ModelEvent.STATE,{newstate:ModelStates.BUFFERING}); |
|---|
| 81 | loading = true; |
|---|
| 82 | if(outgoing) { |
|---|
| 83 | var gid = getID(model.playlist[model.config['item']]['file']); |
|---|
| 84 | var stt = model.playlist[model.config['item']]['start']; |
|---|
| 85 | outgoing.send("_AS3_to_AS2","loadVideoById",gid,stt); |
|---|
| 86 | model.mediaHandler(loader); |
|---|
| 87 | } |
|---|
| 88 | }; |
|---|
| 89 | |
|---|
| 90 | |
|---|
| 91 | /** Pause the YouTube movie. **/ |
|---|
| 92 | public function pause() { |
|---|
| 93 | outgoing.send("_AS3_to_AS2","pauseVideo"); |
|---|
| 94 | }; |
|---|
| 95 | |
|---|
| 96 | |
|---|
| 97 | |
|---|
| 98 | /** Play or pause the video. **/ |
|---|
| 99 | public function play() { |
|---|
| 100 | outgoing.send("_AS3_to_AS2","playVideo"); |
|---|
| 101 | }; |
|---|
| 102 | |
|---|
| 103 | |
|---|
| 104 | /** SWF loaded; add it to the tree **/ |
|---|
| 105 | public function onSwfLoadComplete() { |
|---|
| 106 | outgoing.send("_AS3_to_AS2","setSize",320,240); |
|---|
| 107 | model.config['mute'] == true ? volume(0): volume(model.config['volume']); |
|---|
| 108 | if(loading) { load(); } |
|---|
| 109 | }; |
|---|
| 110 | |
|---|
| 111 | |
|---|
| 112 | /** error was thrown without this handler **/ |
|---|
| 113 | public function onLocalConnectionStatusChange(evt:StatusEvent) { |
|---|
| 114 | // model.sendEvent(ModelEvent.META,{status:evt.code}); |
|---|
| 115 | }; |
|---|
| 116 | |
|---|
| 117 | |
|---|
| 118 | /** Catch youtube errors. **/ |
|---|
| 119 | public function onError(erc:String) { |
|---|
| 120 | var fil = model.playlist[model.config['item']]['file']; |
|---|
| 121 | model.sendEvent(ModelEvent.ERROR,{message:"YouTube error (video not found?):\n"+fil}); |
|---|
| 122 | stop(); |
|---|
| 123 | }; |
|---|
| 124 | |
|---|
| 125 | |
|---|
| 126 | /** Catch youtube state changes. **/ |
|---|
| 127 | public function onStateChange(stt:Number) { |
|---|
| 128 | switch(Number(stt)) { |
|---|
| 129 | case -1: |
|---|
| 130 | // model.sendEvent(ModelEvent.STATE,{newstate:ModelStates.IDLE}); |
|---|
| 131 | break; |
|---|
| 132 | case 0: |
|---|
| 133 | if(model.config['state'] != ModelStates.BUFFERING && model.config['state'] != ModelStates.IDLE) { |
|---|
| 134 | model.sendEvent(ModelEvent.STATE,{newstate:ModelStates.COMPLETED}); |
|---|
| 135 | } |
|---|
| 136 | break; |
|---|
| 137 | case 1: |
|---|
| 138 | model.sendEvent(ModelEvent.STATE,{newstate:ModelStates.PLAYING}); |
|---|
| 139 | break; |
|---|
| 140 | case 2: |
|---|
| 141 | model.sendEvent(ModelEvent.STATE,{newstate:ModelStates.PAUSED}); |
|---|
| 142 | break; |
|---|
| 143 | case 3: |
|---|
| 144 | model.sendEvent(ModelEvent.STATE,{newstate:ModelStates.BUFFERING}); |
|---|
| 145 | model.sendEvent(ModelEvent.BUFFER,{percentage:0}); |
|---|
| 146 | break; |
|---|
| 147 | } |
|---|
| 148 | }; |
|---|
| 149 | |
|---|
| 150 | |
|---|
| 151 | /** Catch Youtube load changes **/ |
|---|
| 152 | public function onLoadChange(ldd:Number,ttl:Number,off:Number) { |
|---|
| 153 | model.sendEvent(ModelEvent.LOADED,{loaded:ldd,total:ttl,offset:off}); |
|---|
| 154 | }; |
|---|
| 155 | |
|---|
| 156 | |
|---|
| 157 | /** Catch Youtube position changes **/ |
|---|
| 158 | public function onTimeChange(pos:Number,dur:Number) { |
|---|
| 159 | model.sendEvent(ModelEvent.TIME,{position:pos,duration:dur}); |
|---|
| 160 | if(!metasent) { |
|---|
| 161 | model.sendEvent(ModelEvent.META,{width:320,height:240,duration:dur}); |
|---|
| 162 | metasent = true; |
|---|
| 163 | } |
|---|
| 164 | }; |
|---|
| 165 | |
|---|
| 166 | |
|---|
| 167 | /** Toggle quality (perhaps access the H264 versions later?). **/ |
|---|
| 168 | public function quality(stt:Boolean) {}; |
|---|
| 169 | |
|---|
| 170 | |
|---|
| 171 | /** Seek to position. **/ |
|---|
| 172 | public function seek(pos:Number) { |
|---|
| 173 | outgoing.send("_AS3_to_AS2","seekTo",pos); |
|---|
| 174 | play(); |
|---|
| 175 | }; |
|---|
| 176 | |
|---|
| 177 | |
|---|
| 178 | /** Destroy the youtube video. **/ |
|---|
| 179 | public function stop() { |
|---|
| 180 | outgoing.send("_AS3_to_AS2","stopVideo"); |
|---|
| 181 | }; |
|---|
| 182 | |
|---|
| 183 | |
|---|
| 184 | |
|---|
| 185 | /** Set the volume level. **/ |
|---|
| 186 | public function volume(pct:Number) { |
|---|
| 187 | outgoing.send("_AS3_to_AS2","setVolume",pct); |
|---|
| 188 | }; |
|---|
| 189 | |
|---|
| 190 | |
|---|
| 191 | } |
|---|
| 192 | |
|---|
| 193 | |
|---|
| 194 | } |
|---|