| 1 | /** |
|---|
| 2 | * Youtube model class of the players MCV pattern. |
|---|
| 3 | * Integrates Youtube playback component. |
|---|
| 4 | * |
|---|
| 5 | * @author Jeroen Wijering |
|---|
| 6 | * @version 1.0 |
|---|
| 7 | **/ |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | import com.jeroenwijering.players.*; |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | class com.jeroenwijering.players.YoutubeModel extends AbstractModel { |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | /** Array with extensions used by this model. **/ |
|---|
| 17 | private var mediatypes:Array = new Array("youtube"); |
|---|
| 18 | /** Clip the YouTube blob is loaded into **/ |
|---|
| 19 | private var ytplayer:MovieClip; |
|---|
| 20 | /** Location of the YouTube blob. **/ |
|---|
| 21 | private var url:String = "http://gdata.youtube.com/apiplayer"; |
|---|
| 22 | /** Developer key for the YouTube player **/ |
|---|
| 23 | private var key:String = "AI39si4CHS3-oQa0cHIhANstFbCLE71-qK6CB3mNe0lEx2h1mwXsOz6n1fkPo0yTKpZgYH4jsLgSX1Qg4jXNrYhJYKfMQiPlzw"; |
|---|
| 24 | /** Reference to the loader **/ |
|---|
| 25 | private var loader:MovieClipLoader; |
|---|
| 26 | /** ID of the current clip to play **/ |
|---|
| 27 | private var currentURL:String; |
|---|
| 28 | /** interval ID of the buffer update function **/ |
|---|
| 29 | private var loadedInterval:Number; |
|---|
| 30 | /** current percentage of the video that's loaded **/ |
|---|
| 31 | private var currentLoaded:Number = 0; |
|---|
| 32 | /** interval ID of the position update function **/ |
|---|
| 33 | private var positionInterval:Number; |
|---|
| 34 | /** current position of the video that is playing **/ |
|---|
| 35 | private var currentPosition:Number; |
|---|
| 36 | /** Current volume **/ |
|---|
| 37 | private var currentVolume:Number; |
|---|
| 38 | /** static referer to the model **/ |
|---|
| 39 | private static var instance; |
|---|
| 40 | /** pause-seeking **/ |
|---|
| 41 | private var pauseseek:Boolean; |
|---|
| 42 | /** has the update already been sent? **/ |
|---|
| 43 | private var updateSent:Boolean; |
|---|
| 44 | |
|---|
| 45 | |
|---|
| 46 | /** Constructor **/ |
|---|
| 47 | function YoutubeModel(vws:Array,ctr:AbstractController,cfg:Object,fed:Object,fcl:MovieClip) { |
|---|
| 48 | super(vws,ctr,cfg,fed); |
|---|
| 49 | ytplayer = fcl; |
|---|
| 50 | System.security.allowDomain("gdata.youtube.com"); |
|---|
| 51 | System.security.allowInsecureDomain("gdata.youtube.com"); |
|---|
| 52 | YoutubeModel.instance = this; |
|---|
| 53 | }; |
|---|
| 54 | |
|---|
| 55 | /** wait for the player to load with a loop, then remove the loop. **/ |
|---|
| 56 | public function onLoadInit() { |
|---|
| 57 | var ref = this; |
|---|
| 58 | ytplayer.onEnterFrame = function() { |
|---|
| 59 | if (this.isPlayerLoaded()) { |
|---|
| 60 | ref.pauseseek = false; |
|---|
| 61 | this.addEventListener("onStateChange",ref.onPlayerStateChange); |
|---|
| 62 | this.addEventListener("onError",ref.onError); |
|---|
| 63 | ref.onPlayerStateChange(this.getPlayerState()); |
|---|
| 64 | ref.setStart(ref.feeder.feed[ref.currentItem]['duration']); |
|---|
| 65 | delete this.onEnterFrame; |
|---|
| 66 | this.setSize(ref.config['displaywidth'],ref.config['displayheight']); |
|---|
| 67 | } |
|---|
| 68 | } |
|---|
| 69 | }; |
|---|
| 70 | |
|---|
| 71 | |
|---|
| 72 | /** Start a specific video **/ |
|---|
| 73 | private function setStart(pos:Number) { |
|---|
| 74 | clearInterval(positionInterval); |
|---|
| 75 | clearInterval(loadedInterval); |
|---|
| 76 | if(feeder.feed[currentItem]["file"] != currentURL) { |
|---|
| 77 | if(!loader) { |
|---|
| 78 | loader = new MovieClipLoader(); |
|---|
| 79 | loader.addListener(this); |
|---|
| 80 | loader.loadClip(url+'?key='+key,ytplayer); |
|---|
| 81 | } else { |
|---|
| 82 | currentURL = feeder.feed[currentItem]["file"]; |
|---|
| 83 | ytplayer.loadVideoById(getID(currentURL),pos); |
|---|
| 84 | ytplayer.setVolume(config['volume']); |
|---|
| 85 | sendUpdate("load",0); |
|---|
| 86 | sendUpdate("size",320,240); |
|---|
| 87 | } |
|---|
| 88 | } else if(!isNaN(pos)) { |
|---|
| 89 | ytplayer.seekTo(pos,true); |
|---|
| 90 | } else { |
|---|
| 91 | ytplayer.playVideo(); |
|---|
| 92 | } |
|---|
| 93 | positionInterval = setInterval(this,"updatePosition",100); |
|---|
| 94 | ytplayer._visible = true; |
|---|
| 95 | trace('true!!'); |
|---|
| 96 | }; |
|---|
| 97 | |
|---|
| 98 | |
|---|
| 99 | /** xtract the current ID from a youtube URL **/ |
|---|
| 100 | private function getID(url:String):String { |
|---|
| 101 | var arr = url.split('?'); |
|---|
| 102 | for (var i in arr) { |
|---|
| 103 | if(arr[i].substr(0,2) == 'v=') { |
|---|
| 104 | return arr[i].substr(2); |
|---|
| 105 | } |
|---|
| 106 | } |
|---|
| 107 | return ''; |
|---|
| 108 | }; |
|---|
| 109 | |
|---|
| 110 | |
|---|
| 111 | /** Listens for the player's onStateChange event **/ |
|---|
| 112 | public function onPlayerStateChange(stt:Number) { |
|---|
| 113 | var ref = YoutubeModel.instance; |
|---|
| 114 | if(ref.currentURL == undefined) { return; } |
|---|
| 115 | switch(Number(stt)) { |
|---|
| 116 | case 0: |
|---|
| 117 | if(!ref.updateSent) { |
|---|
| 118 | ref.sendUpdate("state",3); |
|---|
| 119 | ref.sendCompleteEvent(); |
|---|
| 120 | ref.sendUpdate("time",0,ref.feeder.feed[ref.currentItem]['duration']); |
|---|
| 121 | } |
|---|
| 122 | break; |
|---|
| 123 | case 1: |
|---|
| 124 | delete ref.updateSent; |
|---|
| 125 | if(ref.pauseseek == true) { |
|---|
| 126 | ref.pauseseek = false; |
|---|
| 127 | ref.updatePosition(); |
|---|
| 128 | ref.ytplayer.pauseVideo(); |
|---|
| 129 | } else { |
|---|
| 130 | ref.sendUpdate("load",100); |
|---|
| 131 | ref.sendUpdate("state",2); |
|---|
| 132 | } |
|---|
| 133 | break; |
|---|
| 134 | case 3: |
|---|
| 135 | ref.sendUpdate("state",1); |
|---|
| 136 | break; |
|---|
| 137 | default: |
|---|
| 138 | ref.sendUpdate("state",0); |
|---|
| 139 | break; |
|---|
| 140 | } |
|---|
| 141 | }; |
|---|
| 142 | |
|---|
| 143 | |
|---|
| 144 | /** Error received, let's move to the next video **/ |
|---|
| 145 | public function onError() { |
|---|
| 146 | var ref = YoutubeModel.instance; |
|---|
| 147 | ref.sendUpdate("state",3); |
|---|
| 148 | ref.sendCompleteEvent(); |
|---|
| 149 | ref.sendUpdate("time",0,0); |
|---|
| 150 | }; |
|---|
| 151 | |
|---|
| 152 | |
|---|
| 153 | /** Read and broadcast the current position of the song **/ |
|---|
| 154 | private function updatePosition() { |
|---|
| 155 | var pos = ytplayer.getCurrentTime(); |
|---|
| 156 | var dur = ytplayer.getDuration(); |
|---|
| 157 | if(isNaN(dur)) { |
|---|
| 158 | pos = 0; |
|---|
| 159 | dur = feeder.feed[currentItem]['duration']; |
|---|
| 160 | } else { |
|---|
| 161 | feeder.feed[currentItem]['duration'] = dur; |
|---|
| 162 | } |
|---|
| 163 | sendUpdate("time",pos,dur-pos); |
|---|
| 164 | currentPosition = pos; |
|---|
| 165 | }; |
|---|
| 166 | |
|---|
| 167 | |
|---|
| 168 | /** Pause the video that's currently playing. **/ |
|---|
| 169 | private function setPause(pos:Number) { |
|---|
| 170 | clearInterval(positionInterval); |
|---|
| 171 | ytplayer.pauseVideo(); |
|---|
| 172 | if(pos > 0) { |
|---|
| 173 | pauseseek = true; |
|---|
| 174 | ytplayer.seekTo(pos,true); |
|---|
| 175 | updatePosition(); |
|---|
| 176 | } |
|---|
| 177 | sendUpdate("state",0); |
|---|
| 178 | }; |
|---|
| 179 | |
|---|
| 180 | |
|---|
| 181 | /** Stop video and clear data. **/ |
|---|
| 182 | private function setStop(pos:Number) { |
|---|
| 183 | updateSent = true; |
|---|
| 184 | delete currentURL; |
|---|
| 185 | ytplayer.stopVideo(); |
|---|
| 186 | ytplayer._visible = false; |
|---|
| 187 | clearInterval(loadedInterval); |
|---|
| 188 | clearInterval(positionInterval); |
|---|
| 189 | }; |
|---|
| 190 | |
|---|
| 191 | |
|---|
| 192 | /** Set volume of the sound object. **/ |
|---|
| 193 | private function setVolume(vol:Number) { |
|---|
| 194 | super.setVolume(vol); |
|---|
| 195 | currentVolume = vol; |
|---|
| 196 | ytplayer.setVolume(vol); |
|---|
| 197 | }; |
|---|
| 198 | |
|---|
| 199 | |
|---|
| 200 | |
|---|
| 201 | |
|---|
| 202 | |
|---|
| 203 | } |
|---|