FLVideo.js 8.4 KB

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