FLVideo.js 8.1 KB

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