vimeo.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. /*!
  2. * MediaElement.js
  3. * http://www.mediaelementjs.com/
  4. *
  5. * Wrapper that mimics native HTML5 MediaElement (audio and video)
  6. * using a variety of technologies (pure JavaScript, Flash, iframe)
  7. *
  8. * Copyright 2010-2017, John Dyer (http://j.hn/)
  9. * License: MIT
  10. *
  11. */(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(_dereq_,module,exports){
  12. 'use strict';
  13. var VimeoApi = {
  14. promise: null,
  15. load: function load(settings) {
  16. if (typeof Vimeo !== 'undefined') {
  17. VimeoApi._createPlayer(settings);
  18. } else {
  19. VimeoApi.promise = VimeoApi.promise || mejs.Utils.loadScript('https://player.vimeo.com/api/player.js');
  20. VimeoApi.promise.then(function () {
  21. VimeoApi._createPlayer(settings);
  22. });
  23. }
  24. },
  25. _createPlayer: function _createPlayer(settings) {
  26. var player = new Vimeo.Player(settings.iframe);
  27. window['__ready__' + settings.id](player);
  28. },
  29. getVimeoId: function getVimeoId(url) {
  30. if (url === undefined || url === null) {
  31. return null;
  32. }
  33. var parts = url.split('?');
  34. url = parts[0];
  35. return parseInt(url.substring(url.lastIndexOf('/') + 1), 10);
  36. }
  37. };
  38. var vimeoIframeRenderer = {
  39. name: 'vimeo_iframe',
  40. options: {
  41. prefix: 'vimeo_iframe'
  42. },
  43. canPlayType: function canPlayType(type) {
  44. return ~['video/vimeo', 'video/x-vimeo'].indexOf(type.toLowerCase());
  45. },
  46. create: function create(mediaElement, options, mediaFiles) {
  47. var apiStack = [],
  48. vimeo = {},
  49. readyState = 4;
  50. var paused = true,
  51. volume = 1,
  52. oldVolume = volume,
  53. currentTime = 0,
  54. bufferedTime = 0,
  55. ended = false,
  56. duration = 0,
  57. vimeoPlayer = null,
  58. url = '';
  59. vimeo.options = options;
  60. vimeo.id = mediaElement.id + '_' + options.prefix;
  61. vimeo.mediaElement = mediaElement;
  62. var errorHandler = function errorHandler(error) {
  63. mediaElement.generateError('Code ' + error.name + ': ' + error.message, mediaFiles);
  64. };
  65. var props = mejs.html5media.properties,
  66. assignGettersSetters = function assignGettersSetters(propName) {
  67. var capName = '' + propName.substring(0, 1).toUpperCase() + propName.substring(1);
  68. vimeo['get' + capName] = function () {
  69. if (vimeoPlayer !== null) {
  70. var value = null;
  71. switch (propName) {
  72. case 'currentTime':
  73. return currentTime;
  74. case 'duration':
  75. return duration;
  76. case 'volume':
  77. return volume;
  78. case 'muted':
  79. return volume === 0;
  80. case 'paused':
  81. return paused;
  82. case 'ended':
  83. return ended;
  84. case 'src':
  85. vimeoPlayer.getVideoUrl().then(function (_url) {
  86. url = _url;
  87. }).catch(function (error) {
  88. return errorHandler(error);
  89. });
  90. return url;
  91. case 'buffered':
  92. return {
  93. start: function start() {
  94. return 0;
  95. },
  96. end: function end() {
  97. return bufferedTime * duration;
  98. },
  99. length: 1
  100. };
  101. case 'readyState':
  102. return readyState;
  103. }
  104. return value;
  105. } else {
  106. return null;
  107. }
  108. };
  109. vimeo['set' + capName] = function (value) {
  110. if (vimeoPlayer !== null) {
  111. switch (propName) {
  112. case 'src':
  113. var _url2 = typeof value === 'string' ? value : value[0].src,
  114. videoId = VimeoApi.getVimeoId(_url2);
  115. vimeoPlayer.loadVideo(videoId).then(function () {
  116. if (mediaElement.originalNode.autoplay) {
  117. vimeoPlayer.play();
  118. }
  119. }).catch(function (error) {
  120. return errorHandler(error);
  121. });
  122. break;
  123. case 'currentTime':
  124. vimeoPlayer.setCurrentTime(value).then(function () {
  125. currentTime = value;
  126. setTimeout(function () {
  127. var event = mejs.Utils.createEvent('timeupdate', vimeo);
  128. mediaElement.dispatchEvent(event);
  129. }, 50);
  130. }).catch(function (error) {
  131. return errorHandler(error);
  132. });
  133. break;
  134. case 'volume':
  135. vimeoPlayer.setVolume(value).then(function () {
  136. volume = value;
  137. oldVolume = volume;
  138. setTimeout(function () {
  139. var event = mejs.Utils.createEvent('volumechange', vimeo);
  140. mediaElement.dispatchEvent(event);
  141. }, 50);
  142. }).catch(function (error) {
  143. return errorHandler(error);
  144. });
  145. break;
  146. case 'loop':
  147. vimeoPlayer.setLoop(value).catch(function (error) {
  148. return errorHandler(error);
  149. });
  150. break;
  151. case 'muted':
  152. if (value) {
  153. vimeoPlayer.setVolume(0).then(function () {
  154. volume = 0;
  155. setTimeout(function () {
  156. var event = mejs.Utils.createEvent('volumechange', vimeo);
  157. mediaElement.dispatchEvent(event);
  158. }, 50);
  159. }).catch(function (error) {
  160. return errorHandler(error);
  161. });
  162. } else {
  163. vimeoPlayer.setVolume(oldVolume).then(function () {
  164. volume = oldVolume;
  165. setTimeout(function () {
  166. var event = mejs.Utils.createEvent('volumechange', vimeo);
  167. mediaElement.dispatchEvent(event);
  168. }, 50);
  169. }).catch(function (error) {
  170. return errorHandler(error);
  171. });
  172. }
  173. break;
  174. case 'readyState':
  175. var event = mejs.Utils.createEvent('canplay', vimeo);
  176. mediaElement.dispatchEvent(event);
  177. break;
  178. default:
  179. break;
  180. }
  181. } else {
  182. apiStack.push({ type: 'set', propName: propName, value: value });
  183. }
  184. };
  185. };
  186. for (var i = 0, total = props.length; i < total; i++) {
  187. assignGettersSetters(props[i]);
  188. }
  189. var methods = mejs.html5media.methods,
  190. assignMethods = function assignMethods(methodName) {
  191. vimeo[methodName] = function () {
  192. if (vimeoPlayer !== null) {
  193. switch (methodName) {
  194. case 'play':
  195. paused = false;
  196. return vimeoPlayer.play();
  197. case 'pause':
  198. paused = true;
  199. return vimeoPlayer.pause();
  200. case 'load':
  201. return null;
  202. }
  203. } else {
  204. apiStack.push({ type: 'call', methodName: methodName });
  205. }
  206. };
  207. };
  208. for (var _i = 0, _total = methods.length; _i < _total; _i++) {
  209. assignMethods(methods[_i]);
  210. }
  211. window['__ready__' + vimeo.id] = function (_vimeoPlayer) {
  212. mediaElement.vimeoPlayer = vimeoPlayer = _vimeoPlayer;
  213. if (apiStack.length) {
  214. for (var _i2 = 0, _total2 = apiStack.length; _i2 < _total2; _i2++) {
  215. var stackItem = apiStack[_i2];
  216. if (stackItem.type === 'set') {
  217. var propName = stackItem.propName,
  218. capName = '' + propName.substring(0, 1).toUpperCase() + propName.substring(1);
  219. vimeo['set' + capName](stackItem.value);
  220. } else if (stackItem.type === 'call') {
  221. vimeo[stackItem.methodName]();
  222. }
  223. }
  224. }
  225. if (mediaElement.originalNode.muted) {
  226. vimeoPlayer.setVolume(0);
  227. volume = 0;
  228. }
  229. var vimeoIframe = document.getElementById(vimeo.id);
  230. var events = void 0;
  231. events = ['mouseover', 'mouseout'];
  232. var assignEvents = function assignEvents(e) {
  233. var event = mejs.Utils.createEvent(e.type, vimeo);
  234. mediaElement.dispatchEvent(event);
  235. };
  236. for (var _i3 = 0, _total3 = events.length; _i3 < _total3; _i3++) {
  237. vimeoIframe.addEventListener(events[_i3], assignEvents, false);
  238. }
  239. vimeoPlayer.on('loaded', function () {
  240. vimeoPlayer.getDuration().then(function (loadProgress) {
  241. duration = loadProgress;
  242. if (duration > 0) {
  243. bufferedTime = duration * loadProgress;
  244. if (mediaElement.originalNode.autoplay) {
  245. paused = false;
  246. ended = false;
  247. var event = mejs.Utils.createEvent('play', vimeo);
  248. mediaElement.dispatchEvent(event);
  249. }
  250. }
  251. }).catch(function (error) {
  252. errorHandler(error, vimeo);
  253. });
  254. });
  255. vimeoPlayer.on('progress', function () {
  256. vimeoPlayer.getDuration().then(function (loadProgress) {
  257. duration = loadProgress;
  258. if (duration > 0) {
  259. bufferedTime = duration * loadProgress;
  260. if (mediaElement.originalNode.autoplay) {
  261. var initEvent = mejs.Utils.createEvent('play', vimeo);
  262. mediaElement.dispatchEvent(initEvent);
  263. var playingEvent = mejs.Utils.createEvent('playing', vimeo);
  264. mediaElement.dispatchEvent(playingEvent);
  265. }
  266. }
  267. var event = mejs.Utils.createEvent('progress', vimeo);
  268. mediaElement.dispatchEvent(event);
  269. }).catch(function (error) {
  270. return errorHandler(error);
  271. });
  272. });
  273. vimeoPlayer.on('timeupdate', function () {
  274. vimeoPlayer.getCurrentTime().then(function (seconds) {
  275. currentTime = seconds;
  276. var event = mejs.Utils.createEvent('timeupdate', vimeo);
  277. mediaElement.dispatchEvent(event);
  278. }).catch(function (error) {
  279. return errorHandler(error);
  280. });
  281. });
  282. vimeoPlayer.on('play', function () {
  283. paused = false;
  284. ended = false;
  285. var event = mejs.Utils.createEvent('play', vimeo);
  286. mediaElement.dispatchEvent(event);
  287. var playingEvent = mejs.Utils.createEvent('playing', vimeo);
  288. mediaElement.dispatchEvent(playingEvent);
  289. });
  290. vimeoPlayer.on('pause', function () {
  291. paused = true;
  292. ended = false;
  293. var event = mejs.Utils.createEvent('pause', vimeo);
  294. mediaElement.dispatchEvent(event);
  295. });
  296. vimeoPlayer.on('ended', function () {
  297. paused = false;
  298. ended = true;
  299. var event = mejs.Utils.createEvent('ended', vimeo);
  300. mediaElement.dispatchEvent(event);
  301. });
  302. events = ['rendererready', 'loadedmetadata', 'loadeddata', 'canplay'];
  303. for (var _i4 = 0, _total4 = events.length; _i4 < _total4; _i4++) {
  304. var event = mejs.Utils.createEvent(events[_i4], vimeo);
  305. mediaElement.dispatchEvent(event);
  306. }
  307. };
  308. var height = mediaElement.originalNode.height,
  309. width = mediaElement.originalNode.width,
  310. vimeoContainer = document.createElement('iframe'),
  311. standardUrl = 'https://player.vimeo.com/video/' + VimeoApi.getVimeoId(mediaFiles[0].src);
  312. var queryArgs = ~mediaFiles[0].src.indexOf('?') ? '?' + mediaFiles[0].src.slice(mediaFiles[0].src.indexOf('?') + 1) : '';
  313. if (queryArgs && mediaElement.originalNode.autoplay && queryArgs.indexOf('autoplay') === -1) {
  314. queryArgs += '&autoplay=1';
  315. }
  316. if (queryArgs && mediaElement.originalNode.loop && queryArgs.indexOf('loop') === -1) {
  317. queryArgs += '&loop=1';
  318. }
  319. vimeoContainer.setAttribute('id', vimeo.id);
  320. vimeoContainer.setAttribute('width', width);
  321. vimeoContainer.setAttribute('height', height);
  322. vimeoContainer.setAttribute('frameBorder', '0');
  323. vimeoContainer.setAttribute('src', '' + standardUrl + queryArgs);
  324. vimeoContainer.setAttribute('webkitallowfullscreen', 'true');
  325. vimeoContainer.setAttribute('mozallowfullscreen', 'true');
  326. vimeoContainer.setAttribute('allowfullscreen', 'true');
  327. mediaElement.originalNode.parentNode.insertBefore(vimeoContainer, mediaElement.originalNode);
  328. mediaElement.originalNode.style.display = 'none';
  329. VimeoApi.load({
  330. iframe: vimeoContainer,
  331. id: vimeo.id
  332. });
  333. vimeo.hide = function () {
  334. vimeo.pause();
  335. if (vimeoPlayer) {
  336. vimeoContainer.style.display = 'none';
  337. }
  338. };
  339. vimeo.setSize = function (width, height) {
  340. vimeoContainer.setAttribute('width', width);
  341. vimeoContainer.setAttribute('height', height);
  342. };
  343. vimeo.show = function () {
  344. if (vimeoPlayer) {
  345. vimeoContainer.style.display = '';
  346. }
  347. };
  348. vimeo.destroy = function () {};
  349. return vimeo;
  350. }
  351. };
  352. mejs.Utils.typeChecks.push(function (url) {
  353. return (/(\/\/player\.vimeo|vimeo\.com)/i.test(url) ? 'video/x-vimeo' : null
  354. );
  355. });
  356. mejs.Renderers.add(vimeoIframeRenderer);
  357. },{}]},{},[1]);