[483] | 1 | define(['dojo', 'dijit', 'dijit/_Widget', 'dojox/embed/Flash', 'dojox/av/_Media'], |
---|
| 2 | function(dojo, dijit, _Widget, Flash, _Media){ |
---|
| 3 | |
---|
| 4 | dojo.experimental("dojox.av.FLVideo"); |
---|
| 5 | |
---|
| 6 | return dojo.declare("dojox.av.FLVideo", [_Widget, _Media], { |
---|
| 7 | |
---|
| 8 | // summary: |
---|
| 9 | // Inserts a Flash FLV video into the HTML page and provides methods |
---|
| 10 | // and events for controlling the video. Also plays the H264/M4V codec |
---|
| 11 | // with a little trickery: change the '.M4V' extension to '.flv'. |
---|
| 12 | // example: |
---|
| 13 | // markup: |
---|
| 14 | // | <div id="vid" initialVolume=".7", |
---|
| 15 | // | mediaUrl="../resources/Grog.flv" |
---|
| 16 | // | dojoType="dojox.av.FLVideo"></div> |
---|
| 17 | // example: |
---|
| 18 | // programmatic: |
---|
| 19 | // | new dojox.av.FLVideo({ |
---|
| 20 | // | initialVolume:.7, |
---|
| 21 | // | mediaUrl:"../resources/Grog.flv" |
---|
| 22 | // | }, "vid"); |
---|
| 23 | |
---|
| 24 | // mediaUrl: String |
---|
| 25 | // REQUIRED: The Url of the video file that will be played. |
---|
| 26 | // NOTE: Must be either an absolute URL or relative to the HTML file. |
---|
| 27 | // Relative paths will be converted to absolute paths |
---|
| 28 | |
---|
| 29 | // _swfPath: Uri |
---|
| 30 | // The path to the video player SWF resource |
---|
| 31 | _swfPath: dojo.moduleUrl("dojox.av", "resources/video.swf"), |
---|
| 32 | |
---|
| 33 | constructor: function(/*Object*/options){ |
---|
| 34 | // Provide this function for the SWF to ensure that the it is playing |
---|
| 35 | // in HTML. |
---|
| 36 | dojo.global.swfIsInHTML = function(){ return true; } |
---|
| 37 | }, |
---|
| 38 | |
---|
| 39 | postCreate: function(){ |
---|
| 40 | // summary: |
---|
| 41 | // Initialize the media. |
---|
| 42 | |
---|
| 43 | this._subs = []; |
---|
| 44 | this._cons = []; |
---|
| 45 | this.mediaUrl = this._normalizeUrl(this.mediaUrl); |
---|
| 46 | this.initialVolume = this._normalizeVolume(this.initialVolume); |
---|
| 47 | |
---|
| 48 | var args = { |
---|
| 49 | path:this._swfPath, |
---|
| 50 | width:"100%", |
---|
| 51 | height:"100%", |
---|
| 52 | minimumVersion:9, |
---|
| 53 | expressInstall:true, |
---|
| 54 | params:{ |
---|
| 55 | allowFullScreen: this.allowFullScreen, |
---|
| 56 | wmode:this.wmode, |
---|
| 57 | allowScriptAccess:this.allowScriptAccess, |
---|
| 58 | allowNetworking:this.allowNetworking |
---|
| 59 | }, |
---|
| 60 | // only pass in simple variables - no deep objects |
---|
| 61 | vars:{ |
---|
| 62 | videoUrl:this.mediaUrl, |
---|
| 63 | id:this.id, |
---|
| 64 | autoPlay:this.autoPlay, |
---|
| 65 | volume:this.initialVolume, |
---|
| 66 | isDebug:this.isDebug |
---|
| 67 | } |
---|
| 68 | }; |
---|
| 69 | |
---|
| 70 | // Setting up dojo.subscribes that listens to events |
---|
| 71 | // from the player |
---|
| 72 | this._sub("stageClick", "onClick"); |
---|
| 73 | this._sub("stageSized", "onSwfSized"); |
---|
| 74 | this._sub("mediaStatus", "onPlayerStatus"); |
---|
| 75 | this._sub("mediaMeta", "onMetaData"); |
---|
| 76 | this._sub("mediaError", "onError"); |
---|
| 77 | this._sub("mediaStart", "onStart"); |
---|
| 78 | this._sub("mediaEnd", "onEnd"); |
---|
| 79 | |
---|
| 80 | this._flashObject = new dojox.embed.Flash(args, this.domNode); |
---|
| 81 | this._flashObject.onError = function(err){ |
---|
| 82 | console.error("Flash Error:", err); |
---|
| 83 | }; |
---|
| 84 | this._flashObject.onLoad = dojo.hitch(this, function(mov){ |
---|
| 85 | this.flashMedia = mov; |
---|
| 86 | this.isPlaying = this.autoPlay; |
---|
| 87 | this.isStopped = !this.autoPlay; |
---|
| 88 | this.onLoad(this.flashMedia); |
---|
| 89 | this._initStatus(); |
---|
| 90 | this._update(); |
---|
| 91 | }); |
---|
| 92 | this.inherited(arguments); |
---|
| 93 | }, |
---|
| 94 | |
---|
| 95 | // ============================= // |
---|
| 96 | // Methods to control the player // |
---|
| 97 | // ============================= // |
---|
| 98 | |
---|
| 99 | play: function(/* String? */newUrl){ |
---|
| 100 | // summary: |
---|
| 101 | // Plays the video. If an url is passed in, plays the new link. |
---|
| 102 | this.isPlaying = true; |
---|
| 103 | this.isStopped = false; |
---|
| 104 | this.flashMedia.doPlay(this._normalizeUrl(newUrl)); |
---|
| 105 | }, |
---|
| 106 | |
---|
| 107 | pause: function(){ |
---|
| 108 | // summary: |
---|
| 109 | // Pauses the video |
---|
| 110 | this.isPlaying = false; |
---|
| 111 | this.isStopped = false; |
---|
| 112 | if(this.onPaused){ |
---|
| 113 | this.onPaused(); |
---|
| 114 | } |
---|
| 115 | this.flashMedia.pause(); |
---|
| 116 | }, |
---|
| 117 | |
---|
| 118 | seek: function(/* Float */ time ){ |
---|
| 119 | // summary: |
---|
| 120 | // Goes to the time passed in the argument |
---|
| 121 | this.flashMedia.seek(time); |
---|
| 122 | }, |
---|
| 123 | |
---|
| 124 | |
---|
| 125 | // ===================== // |
---|
| 126 | // Player Getter/Setters // |
---|
| 127 | // ===================== // |
---|
| 128 | |
---|
| 129 | volume: function(/* Float */ vol){ |
---|
| 130 | // summary: |
---|
| 131 | // Sets the volume of the video to the time in the |
---|
| 132 | // vol: |
---|
| 133 | // between 0 - 1. |
---|
| 134 | if(vol){ |
---|
| 135 | if(!this.flashMedia) { |
---|
| 136 | this.initialVolume = vol; |
---|
| 137 | } |
---|
| 138 | this.flashMedia.setVolume(this._normalizeVolume(vol)); |
---|
| 139 | } |
---|
| 140 | if(!this.flashMedia || !this.flashMedia.doGetVolume) { |
---|
| 141 | return this.initialVolume; |
---|
| 142 | } |
---|
| 143 | return this.flashMedia.getVolume(); // Float |
---|
| 144 | }, |
---|
| 145 | |
---|
| 146 | // ============= // |
---|
| 147 | // Player Events // |
---|
| 148 | // ============= // |
---|
| 149 | |
---|
| 150 | /*===== |
---|
| 151 | onLoad: function(mov){ |
---|
| 152 | // summary: |
---|
| 153 | // Fired when the SWF player has loaded |
---|
| 154 | // NOT when the video has loaded |
---|
| 155 | }, |
---|
| 156 | |
---|
| 157 | onDownloaded: function(percent){ |
---|
| 158 | // summary: |
---|
| 159 | // Fires the amount of that the media has been |
---|
| 160 | // downloaded. Number, 0-100 |
---|
| 161 | }, |
---|
| 162 | |
---|
| 163 | onClick: function(evt){ |
---|
| 164 | // summary: |
---|
| 165 | // Fires when the player is clicked |
---|
| 166 | // Could be used to toggle play/pause, or |
---|
| 167 | // do an external activity, like opening a new |
---|
| 168 | // window. |
---|
| 169 | }, |
---|
| 170 | |
---|
| 171 | onSwfSized: function(data){ |
---|
| 172 | // summary: |
---|
| 173 | // Fired on SWF resize, or when its |
---|
| 174 | // toggled between fullscreen. |
---|
| 175 | }, |
---|
| 176 | |
---|
| 177 | onMetaData: function(data, evt){ |
---|
| 178 | // summary: |
---|
| 179 | // The video properties. Width, height, duration, etc. |
---|
| 180 | // |
---|
| 181 | // NOTE: if data is empty, this is an older FLV with no meta data. |
---|
| 182 | // Duration cannot be determined. In original FLVs, duration |
---|
| 183 | // could only be obtained with Flash Media Server. |
---|
| 184 | // |
---|
| 185 | // NOTE: Older FLVs can still return width and height |
---|
| 186 | // and will do so on a second event call |
---|
| 187 | }, |
---|
| 188 | |
---|
| 189 | onPosition: function( time){ |
---|
| 190 | // summary: |
---|
| 191 | // The position of the playhead in seconds |
---|
| 192 | }, |
---|
| 193 | |
---|
| 194 | onStart: function( data){ |
---|
| 195 | // summary: |
---|
| 196 | // Fires when video starts |
---|
| 197 | // Good for setting the play button to pause |
---|
| 198 | // during an autoPlay for example |
---|
| 199 | }, |
---|
| 200 | |
---|
| 201 | onPlay: function(data){ |
---|
| 202 | // summary: |
---|
| 203 | // Fires when video starts and resumes |
---|
| 204 | }, |
---|
| 205 | |
---|
| 206 | onPause: function(data){ |
---|
| 207 | // summary: |
---|
| 208 | // Fires when the pause button is clicked |
---|
| 209 | }, |
---|
| 210 | |
---|
| 211 | onEnd: function(data){ |
---|
| 212 | // summary: |
---|
| 213 | // Fires when video ends |
---|
| 214 | // Could be used to change pause button to play |
---|
| 215 | // or show a post video graphic, like YouTube |
---|
| 216 | }, |
---|
| 217 | |
---|
| 218 | onStop: function(){ |
---|
| 219 | // summary: |
---|
| 220 | // Fire when the Stop button is clicked |
---|
| 221 | |
---|
| 222 | // TODO: This is not hooked up yet and shouldn't |
---|
| 223 | // fire. |
---|
| 224 | }, |
---|
| 225 | |
---|
| 226 | onBuffer: function(isBuffering){ |
---|
| 227 | // summary: |
---|
| 228 | // Fires a boolean to tell if media |
---|
| 229 | // is paused for buffering or if buffering |
---|
| 230 | // has finished |
---|
| 231 | this.isBuffering = isBuffering; |
---|
| 232 | }, |
---|
| 233 | |
---|
| 234 | onError: function(data, url){ |
---|
| 235 | // summary: |
---|
| 236 | // Fired when the player encounters an error |
---|
| 237 | // example: |
---|
| 238 | // | console.warn("ERROR-"+data.type.toUpperCase()+":", |
---|
| 239 | // | data.info.code, " - URL:", url); |
---|
| 240 | }, |
---|
| 241 | |
---|
| 242 | onStatus: function(data){ |
---|
| 243 | // summary: |
---|
| 244 | // Simple status |
---|
| 245 | }, |
---|
| 246 | |
---|
| 247 | onPlayerStatus: function(data){ |
---|
| 248 | // summary: |
---|
| 249 | // The status of the video from the SWF |
---|
| 250 | // playing, stopped, bufering, etc. |
---|
| 251 | }, |
---|
| 252 | |
---|
| 253 | onResize: function(){ |
---|
| 254 | // summary: |
---|
| 255 | // Fired on page resize |
---|
| 256 | }, |
---|
| 257 | =====*/ |
---|
| 258 | |
---|
| 259 | // =============== // |
---|
| 260 | // Private Methods // |
---|
| 261 | // =============== // |
---|
| 262 | |
---|
| 263 | _checkBuffer: function(/* Float */time, /* Float */bufferLength){ |
---|
| 264 | // summary: |
---|
| 265 | // Checks that there is a proper buffer time between |
---|
| 266 | // current playhead time and the amount of data loaded. |
---|
| 267 | // Works only on FLVs with a duration (not older). Pauses |
---|
| 268 | // the video while continuing download. |
---|
| 269 | |
---|
| 270 | if(this.percentDownloaded == 100){ |
---|
| 271 | if(this.isBuffering){ |
---|
| 272 | this.onBuffer(false); |
---|
| 273 | this.flashMedia.doPlay(); |
---|
| 274 | } |
---|
| 275 | return; |
---|
| 276 | } |
---|
| 277 | |
---|
| 278 | if(!this.isBuffering && bufferLength<.1){ |
---|
| 279 | this.onBuffer(true); |
---|
| 280 | this.flashMedia.pause(); |
---|
| 281 | return; |
---|
| 282 | } |
---|
| 283 | |
---|
| 284 | var timePercentLoad = this.percentDownloaded*.01*this.duration; |
---|
| 285 | |
---|
| 286 | // check if start buffer needed |
---|
| 287 | if(!this.isBuffering && time+this.minBufferTime*.001>timePercentLoad){ |
---|
| 288 | this.onBuffer(true); |
---|
| 289 | this.flashMedia.pause(); |
---|
| 290 | |
---|
| 291 | // check if end buffer needed |
---|
| 292 | }else if(this.isBuffering && time+this.bufferTime*.001<=timePercentLoad){ |
---|
| 293 | this.onBuffer(false); |
---|
| 294 | this.flashMedia.doPlay(); |
---|
| 295 | } |
---|
| 296 | |
---|
| 297 | }, |
---|
| 298 | _update: function(){ |
---|
| 299 | // summary: |
---|
| 300 | // Helper function to fire onPosition, check download progress, |
---|
| 301 | // and check buffer. |
---|
| 302 | var time = Math.min(this.getTime() || 0, this.duration); |
---|
| 303 | |
---|
| 304 | var dObj = this.flashMedia.getLoaded(); |
---|
| 305 | this.percentDownloaded = Math.ceil(dObj.bytesLoaded/dObj.bytesTotal*100); |
---|
| 306 | this.onDownloaded(this.percentDownloaded); |
---|
| 307 | this.onPosition(time); |
---|
| 308 | if(this.duration){ |
---|
| 309 | this._checkBuffer(time, dObj.buffer); |
---|
| 310 | } |
---|
| 311 | // FIXME: need to remove this on destroy |
---|
| 312 | this._updateHandle = setTimeout(dojo.hitch(this, "_update"), this.updateTime); |
---|
| 313 | }, |
---|
| 314 | |
---|
| 315 | destroy: function(){ |
---|
| 316 | clearTimeout(this._updateHandle); |
---|
| 317 | dojo.disconnect(this._positionHandle); |
---|
| 318 | this.inherited(arguments); |
---|
| 319 | } |
---|
| 320 | }); |
---|
| 321 | |
---|
| 322 | }); |
---|