speed.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. mejs.i18n.en['mejs.speed-rate'] = 'Speed Rate';
  14. Object.assign(mejs.MepDefaults, {
  15. speeds: ['2.00', '1.50', '1.25', '1.00', '0.75'],
  16. defaultSpeed: '1.00',
  17. speedChar: 'x',
  18. speedText: null
  19. });
  20. Object.assign(MediaElementPlayer.prototype, {
  21. buildspeed: function buildspeed(player, controls, layers, media) {
  22. var t = this,
  23. isNative = t.media.rendererName !== null && /(native|html5)/i.test(t.media.rendererName);
  24. if (!isNative) {
  25. return;
  26. }
  27. var speeds = [],
  28. speedTitle = mejs.Utils.isString(t.options.speedText) ? t.options.speedText : mejs.i18n.t('mejs.speed-rate'),
  29. getSpeedNameFromValue = function getSpeedNameFromValue(value) {
  30. for (var i = 0, total = speeds.length; i < total; i++) {
  31. if (speeds[i].value === value) {
  32. return speeds[i].name;
  33. }
  34. }
  35. };
  36. var playbackSpeed = void 0,
  37. defaultInArray = false;
  38. for (var i = 0, total = t.options.speeds.length; i < total; i++) {
  39. var s = t.options.speeds[i];
  40. if (typeof s === 'string') {
  41. speeds.push({
  42. name: '' + s + t.options.speedChar,
  43. value: s
  44. });
  45. if (s === t.options.defaultSpeed) {
  46. defaultInArray = true;
  47. }
  48. } else {
  49. speeds.push(s);
  50. if (s.value === t.options.defaultSpeed) {
  51. defaultInArray = true;
  52. }
  53. }
  54. }
  55. if (!defaultInArray) {
  56. speeds.push({
  57. name: t.options.defaultSpeed + t.options.speedChar,
  58. value: t.options.defaultSpeed
  59. });
  60. }
  61. speeds.sort(function (a, b) {
  62. return parseFloat(b.value) - parseFloat(a.value);
  63. });
  64. t.cleanspeed(player);
  65. player.speedButton = document.createElement('div');
  66. player.speedButton.className = t.options.classPrefix + 'button ' + t.options.classPrefix + 'speed-button';
  67. player.speedButton.innerHTML = '<button type="button" aria-controls="' + t.id + '" title="' + speedTitle + '" ' + ('aria-label="' + speedTitle + '" tabindex="0">' + getSpeedNameFromValue(t.options.defaultSpeed) + '</button>') + ('<div class="' + t.options.classPrefix + 'speed-selector ' + t.options.classPrefix + 'offscreen">') + ('<ul class="' + t.options.classPrefix + 'speed-selector-list"></ul>') + '</div>';
  68. t.addControlElement(player.speedButton, 'speed');
  69. for (var _i = 0, _total = speeds.length; _i < _total; _i++) {
  70. var inputId = t.id + '-speed-' + speeds[_i].value;
  71. player.speedButton.querySelector('ul').innerHTML += '<li class="' + t.options.classPrefix + 'speed-selector-list-item">' + ('<input class="' + t.options.classPrefix + 'speed-selector-input" type="radio" name="' + t.id + '_speed"') + ('disabled="disabled" value="' + speeds[_i].value + '" id="' + inputId + '" ') + ((speeds[_i].value === t.options.defaultSpeed ? ' checked="checked"' : '') + '/>') + ('<label for="' + inputId + '" class="' + t.options.classPrefix + 'speed-selector-label') + ((speeds[_i].value === t.options.defaultSpeed ? ' ' + t.options.classPrefix + 'speed-selected' : '') + '">') + (speeds[_i].name + '</label>') + '</li>';
  72. }
  73. playbackSpeed = t.options.defaultSpeed;
  74. player.speedSelector = player.speedButton.querySelector('.' + t.options.classPrefix + 'speed-selector');
  75. var inEvents = ['mouseenter', 'focusin'],
  76. outEvents = ['mouseleave', 'focusout'],
  77. radios = player.speedButton.querySelectorAll('input[type="radio"]'),
  78. labels = player.speedButton.querySelectorAll('.' + t.options.classPrefix + 'speed-selector-label');
  79. for (var _i2 = 0, _total2 = inEvents.length; _i2 < _total2; _i2++) {
  80. player.speedButton.addEventListener(inEvents[_i2], function () {
  81. mejs.Utils.removeClass(player.speedSelector, t.options.classPrefix + 'offscreen');
  82. player.speedSelector.style.height = player.speedSelector.querySelector('ul').offsetHeight;
  83. player.speedSelector.style.top = -1 * parseFloat(player.speedSelector.offsetHeight) + 'px';
  84. });
  85. }
  86. for (var _i3 = 0, _total3 = outEvents.length; _i3 < _total3; _i3++) {
  87. player.speedSelector.addEventListener(outEvents[_i3], function () {
  88. mejs.Utils.addClass(this, t.options.classPrefix + 'offscreen');
  89. });
  90. }
  91. for (var _i4 = 0, _total4 = radios.length; _i4 < _total4; _i4++) {
  92. var radio = radios[_i4];
  93. radio.disabled = false;
  94. radio.addEventListener('click', function () {
  95. var self = this,
  96. newSpeed = self.value;
  97. playbackSpeed = newSpeed;
  98. media.playbackRate = parseFloat(newSpeed);
  99. player.speedButton.querySelector('button').innerHTML = getSpeedNameFromValue(newSpeed);
  100. var selected = player.speedButton.querySelectorAll('.' + t.options.classPrefix + 'speed-selected');
  101. for (var _i5 = 0, _total5 = selected.length; _i5 < _total5; _i5++) {
  102. mejs.Utils.removeClass(selected[_i5], t.options.classPrefix + 'speed-selected');
  103. }
  104. self.checked = true;
  105. var siblings = mejs.Utils.siblings(self, function (el) {
  106. return mejs.Utils.hasClass(el, t.options.classPrefix + 'speed-selector-label');
  107. });
  108. for (var j = 0, _total6 = siblings.length; j < _total6; j++) {
  109. mejs.Utils.addClass(siblings[j], t.options.classPrefix + 'speed-selected');
  110. }
  111. });
  112. }
  113. for (var _i6 = 0, _total7 = labels.length; _i6 < _total7; _i6++) {
  114. labels[_i6].addEventListener('click', function () {
  115. var radio = mejs.Utils.siblings(this, function (el) {
  116. return el.tagName === 'INPUT';
  117. })[0],
  118. event = mejs.Utils.createEvent('click', radio);
  119. radio.dispatchEvent(event);
  120. });
  121. }
  122. player.speedSelector.addEventListener('keydown', function (e) {
  123. e.stopPropagation();
  124. });
  125. media.addEventListener('loadedmetadata', function () {
  126. if (playbackSpeed) {
  127. media.playbackRate = parseFloat(playbackSpeed);
  128. }
  129. });
  130. },
  131. cleanspeed: function cleanspeed(player) {
  132. if (player) {
  133. if (player.speedButton) {
  134. player.speedButton.parentNode.removeChild(player.speedButton);
  135. }
  136. if (player.speedSelector) {
  137. player.speedSelector.parentNode.removeChild(player.speedSelector);
  138. }
  139. }
  140. }
  141. });
  142. },{}]},{},[1]);