video.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. Namespace.register("U.Control.video"); //谢茵怡 吕坤城
  2. U.Control.video = (function () {
  3. var VIO, _VO = function (OPT) {
  4. return new VIO.init(OPT);
  5. }
  6. VIO = _VO.prototype = {
  7. // 初始化函数
  8. init: function (OPT) {
  9. this.config(OPT);
  10. return this.Lview();
  11. },
  12. // 配置文件
  13. config: function (OPT) {
  14. this.width = OPT.width || ""; this.height = OPT.height || ""; this.src = OPT.src || underfind; this.play = OPT.autoplay || false;
  15. this.tool = OPT.controls || false; this.pos = OPT.pos || $("body"); this.time = 1;
  16. },
  17. //播放器视图
  18. Lview: function () {
  19. var _this = this;
  20. var vstorage, vbox, vbar, vplay, vpause, vreplay, vprogem, vprogfp, vtime, vstime, vetime, vshu, vsound, vsoundpb, vsoundem, vsoundfp, vfull;
  21. var isMute;//判断是否静音的功能
  22. vbox = $$("div", { "className": "vbox" }, this.pos);
  23. this.elm = $$("video", { "src": this.src, "controls": this.tool, "height": "1px", "autoplay": this.play, "width": "1px" }, vbox);
  24. var o = this.Oapi();
  25. vbar = $$("div", { "className": "vbar" }, vbox);
  26. vplay = $$("div", { "className": "vicon vplay", "onclick": [[o.play]] }, vbar);
  27. vreplay = $$("div", { "className": "vicon vreplay", "onclick": [[o.replay]] }, vbar);
  28. this.vprog = $$("div", { "className": "vicon vprog" }, vbar);
  29. vprogem = $$("div", { "className": "vprogem" }, this.vprog);
  30. vprogfp = $$("div", { "className": "vicon vprogfp" }, vprogem);
  31. vtime = $$("div", { "className": "vtime" }, vbar);
  32. vstime = $$("span", { "className": "vstime", "innerHTML": "00:00" }, vtime);
  33. vshu = $$("span", { "className": "vshu", "innerHTML": "/" }, vtime);
  34. vetime = $$("span", { "className": "vetime", "innerHTML": "04:10" }, vtime);
  35. vsound = $$("div", { "className": "vicon vsound" }, vbar);
  36. vsoundpb = $$("div", { "className": "vsoundpb" }, vbar);
  37. vsoundem = $$("div", { "className": "vsoundem" }, vsoundpb);
  38. vsoundfp = $$("div", { "className": "vicon vsoundfp" }, vsoundem);
  39. vfull = $$("div", { "className": "vicon vfull" }, vbar);
  40. a = this.AFun();
  41. a.ratioVideo(this.elm, this.width, this.height, vbox, vfull); //视频加载
  42. o.metadata(vstime, vetime, vprogem); //加载视频状态
  43. o.scheduleRef(this.vprog, 1); //进度条功能
  44. isMute = o.loudspeakerMuteInit(vsound, "loudspeakerMute", vsoundpb); //静音功能
  45. o.scheduleRef(vsoundpb, 2,isMute); //声音功能
  46. return vbox;
  47. },
  48. AFun: function () {
  49. var _this = this;
  50. var time = null, time2 = null, canvas = null, inbox = 0, eX, eY, aX, aY, n = 0, m = 0;
  51. //绘制画布
  52. var drawVideo = function (ELMC, ELM, EW, EH) {
  53. var content = ELMC.getContext("2d");
  54. clearInterval(time);
  55. time = setInterval(function () { content.drawImage(ELM, 0, 0, EW, EH); }, 20);
  56. }
  57. //视频比例
  58. var ratioVideo = function (ELM, EWH, EHI, EVB, EFULL) {
  59. var cw = $("body").width(), bw = window.screen.width;
  60. var ch = $("body").height(), bh = window.screen.height;
  61. U.M.AddEvent("loadedmetadata", ELM, function () {
  62. var gw = 0, gh = 0, gv = 0;
  63. var w = _this.otime = this.videoWidth; //426
  64. var h = this.videoHeight; //240
  65. var p = h / w; //比例 0.56
  66. //如果只给了宽
  67. if (EWH && EHI == "") {
  68. EWH >= cw ? (gw = cw, gh = ch) : (gw = EWH, gh = gw * p);
  69. }
  70. //如果只给了高
  71. if (EHI && EWH == "") {
  72. EHI >= ch ? (gh = ch, gw = cw) : (gh = EHI, gw = gh / p);
  73. }
  74. //如果给了宽高
  75. if (EWH && EHI) {
  76. //如果宽大。。250 比例是 0.58
  77. //如果高大。。436 比例是 0.55
  78. gh = EHI; gw = gh / p;
  79. if (gh >= ch) {
  80. gh = ch;
  81. gw = gh / p;
  82. }
  83. if (gw >= cw) {
  84. gw = cw;
  85. gh = gw * p;
  86. }
  87. }
  88. if (EWH == "" && EHI == "") {
  89. gw = 300; gh = 300;
  90. }
  91. if (gw >= 1000) {
  92. gv = (gw - 1000) / 100;
  93. $(_this.vprog).css({ "width": 55 + gv + "%" });
  94. } else {
  95. gv = (1000 - gw) / 25;
  96. $(_this.vprog).css({ "width": 55 - gv + "%" });
  97. }
  98. $(EVB).css({ "width": gw + "px", "height": gh + "px" }); //大框
  99. if (!$("canvas")[0]) {
  100. canvas = $$("canvas", { "width": gw, "height": gh }, EVB); //canvas
  101. } else {
  102. canvas = $("canvas");
  103. }
  104. drawVideo(canvas, ELM, gw, gh);
  105. EFULL.onclick = function () {
  106. if (m % 2 == 0) {
  107. gv = (bw - 1000) / 100;
  108. $(_this.vprog).css({ "width": 55 + gv + "%" });
  109. $(EVB).css({ "width": bw + "px", "height": bh + "px" }); canvas.width = bw; canvas.height = bh;
  110. drawVideo(canvas, ELM, bw, bh);
  111. U.M.fullScreen(EVB);
  112. } else {
  113. gv = (gw - 1000) / 100;
  114. $(_this.vprog).css({ "width": 55 + gv + "%" });
  115. $(EVB).css({ "width": gw + "px", "height": gh + "px" }); canvas.width = gw; canvas.height = gh;
  116. drawVideo(canvas, ELM, gw, gh);
  117. document.webkitCancelFullScreen();
  118. }
  119. m++;
  120. }
  121. U.M.AddEvent("mousemove", EVB, function (e) {
  122. eX = e.clientX;
  123. eY = e.clientY;
  124. if (aX != eX && aY != eY) {
  125. inbox = 1;
  126. $(".vbar").show();
  127. }
  128. })
  129. U.M.AddEvent("mouseover", EVB, function (e) {
  130. time2 = setInterval(function () {
  131. aX = eX;
  132. aY = eY;
  133. if (inbox == 0) {
  134. $(".vbar").hide();
  135. }
  136. inbox = 0;
  137. }, 3000);
  138. })
  139. U.M.AddEvent("mouseout", EVB, function () {
  140. clearInterval(time2);
  141. })
  142. });
  143. }
  144. return {
  145. drawVideo: drawVideo, //绘制画布
  146. ratioVideo: ratioVideo
  147. }
  148. },
  149. //视频播放api
  150. Oapi: function () {
  151. var _this = this, n = 0;
  152. //可播放的类型
  153. var type = { "ogg": 'video/ogg; codecs="theora, vorbis"', "mp4": 'video/mp4; codecs="avc1.42E01E, mp4a.40.2"', "webm": 'audio/webm; codecs="vorbis"' };
  154. //转换分
  155. var timeStom = function (TIME) {
  156. var m = Math.floor(TIME / 60);
  157. m < 10 && (m = '0' + m);
  158. return m + ':' + (TIME % 60 / 100).toFixed(2).slice(-2);
  159. }
  160. //元数据事件
  161. var metadata = function (SVUl, EVUl, VEM) {
  162. U.M.AddEvent("timeupdate", _this.elm, function () {
  163. $(SVUl)[0].innerHTML = timeStom(this.currentTime);
  164. $(VEM).css('width', this.currentTime / this.duration * 100 + "%");
  165. });
  166. U.M.AddEvent("loadedmetadata", _this.elm, function () {
  167. $(EVUl)[0].innerHTML = timeStom(this.duration);
  168. this.volume = 0.5;
  169. });
  170. };
  171. //更改数据
  172. var updatabar = function (ELM, SHIF, snum) {
  173. var endtime = _this.elm.duration;
  174. var postion = SHIF - U.M.GetElementInfo(ELM).BCRL;
  175. var percent = postion / $(ELM).width() * 100;
  176. percent >= 100 ? percent = 100 : (percent <= 0 ? percent = 0 : percent = percent);
  177. $(ELM.childNodes).css("width", percent + "%");
  178. if (snum == 1) {
  179. _this.elm.currentTime = endtime * percent / 100;
  180. } else {
  181. _this.elm.volume = percent / 100;
  182. }
  183. }
  184. //进度调整
  185. var scheduleRef = function (ELM, SEL,isMute) {
  186. var state = false, snum;
  187. SEL == 1 ? snum = 1 : snum = 2;
  188. U.M.AddEvent("mousedown", ELM, function (e) {
  189. state = true; updatabar(ELM, e.pageX, snum);
  190. isMute&&isMute();
  191. });
  192. U.M.AddEvent("mouseup", document, function (e) {
  193. if (state) { state = false; updatabar(ELM, e.pageX, snum); }
  194. isMute&&isMute();
  195. });
  196. U.M.AddEvent("mousemove", document, function (e) {
  197. if (state) { updatabar(ELM, e.pageX, snum); }
  198. isMute&&isMute();
  199. });
  200. }
  201. //播放速度
  202. var backRate = function (UVE) {
  203. return _this.elm.playbackRate = UVE;
  204. }
  205. /* 是否可以播放
  206. * apply: probably and maybe
  207. * "" 空串表示不支持
  208. */
  209. var isPaly = function (UTF, UDOD) {
  210. return _this.elm.canPlayType(type[UTF.toLowerCase()]);
  211. }
  212. var play = function () {
  213. if (n % 2 == 0) {
  214. _this.elm.play();
  215. $(this).removeClass("vplay").addClass("vpause");
  216. } else {
  217. pause();
  218. $(this).removeClass("vpause").addClass("vplay");
  219. }
  220. n++;
  221. }
  222. var pause = function () {
  223. _this.elm.pause();
  224. }
  225. var load = function () {
  226. _this.elm.load();
  227. }
  228. var replay = function () {
  229. _this.elm.currentTime = 0;
  230. }
  231. var canPlayType = function () {
  232. if (_this.elm.canPlayType) { return true; } else { return false; }
  233. }
  234. //全屏
  235. var full = function (CLM) {
  236. U.M.fullScreen(CLM);
  237. }
  238. var exitfull = function (a) {
  239. U.M.fullScreen(true);
  240. }
  241. /*
  242. @function loudspeakerMuteInit 静音函数初始化
  243. @ele {htmlObj} 静音的图标
  244. @class {string} 改变的class
  245. @boolean {boolen} 是否静音
  246. @ELM {htmlObj} 静音的进度条
  247. */
  248. var loudspeakerMuteInit = function (ele, className, ELM) {
  249. var beforeVoice = 0.3;//记录之前的声音大小,默认0.3
  250. var isMute = function(){//判断是否改变图片
  251. _this.elm.volume == 0?
  252. $(ele).addClass(className):
  253. $(ele).removeClass(className);
  254. }
  255. $(ele).bind("click", function () {
  256. if (_this.elm.volume == 0) {
  257. _this.elm.volume = beforeVoice;
  258. } else {
  259. beforeVoice = _this.elm.volume;
  260. _this.elm.volume = 0;
  261. }
  262. isMute();
  263. $(ELM.childNodes).css("width", _this.elm.volume * 100 + "%");
  264. })
  265. return isMute;
  266. }
  267. return {
  268. play: play, //播放
  269. pause: pause, //暂停
  270. load: load, //重新加载视频元素
  271. replay: replay, //重播
  272. backRate: backRate, //速度
  273. canPlayType: canPlayType, //判断是否支持浏览器
  274. isPaly: isPaly, //判断是否可以播放
  275. full: full, //是否全屏
  276. exitfull: exitfull,
  277. metadata: metadata, //视频长度
  278. scheduleRef: scheduleRef, //调整进度
  279. loudspeakerMuteInit: loudspeakerMuteInit //静音初始化
  280. };
  281. }
  282. }
  283. VIO.init.prototype = VIO; return _VO;
  284. })();