soundcloud.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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 SoundCloudApi = {
  14. promise: null,
  15. load: function load(settings) {
  16. if (typeof SC !== 'undefined') {
  17. SoundCloudApi._createPlayer(settings);
  18. } else {
  19. SoundCloudApi.promise = SoundCloudApi.promise || mejs.Utils.loadScript('https://w.soundcloud.com/player/api.js');
  20. SoundCloudApi.promise.then(function () {
  21. SoundCloudApi._createPlayer(settings);
  22. });
  23. }
  24. },
  25. _createPlayer: function _createPlayer(settings) {
  26. var player = SC.Widget(settings.iframe);
  27. window['__ready__' + settings.id](player);
  28. }
  29. };
  30. var SoundCloudIframeRenderer = {
  31. name: 'soundcloud_iframe',
  32. options: {
  33. prefix: 'soundcloud_iframe'
  34. },
  35. canPlayType: function canPlayType(type) {
  36. return ~['video/soundcloud', 'video/x-soundcloud'].indexOf(type.toLowerCase());
  37. },
  38. create: function create(mediaElement, options, mediaFiles) {
  39. var sc = {},
  40. apiStack = [],
  41. readyState = 4,
  42. autoplay = mediaElement.originalNode.autoplay,
  43. isVideo = mediaElement.originalNode !== null && mediaElement.originalNode.tagName.toLowerCase() === 'video';
  44. var duration = 0,
  45. currentTime = 0,
  46. bufferedTime = 0,
  47. volume = 1,
  48. muted = false,
  49. paused = true,
  50. ended = false,
  51. scPlayer = null,
  52. scIframe = null;
  53. sc.options = options;
  54. sc.id = mediaElement.id + '_' + options.prefix;
  55. sc.mediaElement = mediaElement;
  56. var props = mejs.html5media.properties,
  57. assignGettersSetters = function assignGettersSetters(propName) {
  58. var capName = '' + propName.substring(0, 1).toUpperCase() + propName.substring(1);
  59. sc['get' + capName] = function () {
  60. if (scPlayer !== null) {
  61. var value = null;
  62. switch (propName) {
  63. case 'currentTime':
  64. return currentTime;
  65. case 'duration':
  66. return duration;
  67. case 'volume':
  68. return volume;
  69. case 'paused':
  70. return paused;
  71. case 'ended':
  72. return ended;
  73. case 'muted':
  74. return muted;
  75. case 'buffered':
  76. return {
  77. start: function start() {
  78. return 0;
  79. },
  80. end: function end() {
  81. return bufferedTime * duration;
  82. },
  83. length: 1
  84. };
  85. case 'src':
  86. return scIframe ? scIframe.src : '';
  87. case 'readyState':
  88. return readyState;
  89. }
  90. return value;
  91. } else {
  92. return null;
  93. }
  94. };
  95. sc['set' + capName] = function (value) {
  96. if (scPlayer !== null) {
  97. switch (propName) {
  98. case 'src':
  99. var url = typeof value === 'string' ? value : value[0].src;
  100. scPlayer.load(url);
  101. if (autoplay) {
  102. scPlayer.play();
  103. }
  104. break;
  105. case 'currentTime':
  106. scPlayer.seekTo(value * 1000);
  107. break;
  108. case 'muted':
  109. if (value) {
  110. scPlayer.setVolume(0);
  111. } else {
  112. scPlayer.setVolume(1);
  113. }
  114. setTimeout(function () {
  115. var event = mejs.Utils.createEvent('volumechange', sc);
  116. mediaElement.dispatchEvent(event);
  117. }, 50);
  118. break;
  119. case 'volume':
  120. scPlayer.setVolume(value);
  121. setTimeout(function () {
  122. var event = mejs.Utils.createEvent('volumechange', sc);
  123. mediaElement.dispatchEvent(event);
  124. }, 50);
  125. break;
  126. case 'readyState':
  127. var event = mejs.Utils.createEvent('canplay', sc);
  128. mediaElement.dispatchEvent(event);
  129. break;
  130. default:
  131. break;
  132. }
  133. } else {
  134. apiStack.push({ type: 'set', propName: propName, value: value });
  135. }
  136. };
  137. };
  138. for (var i = 0, total = props.length; i < total; i++) {
  139. assignGettersSetters(props[i]);
  140. }
  141. var methods = mejs.html5media.methods,
  142. assignMethods = function assignMethods(methodName) {
  143. sc[methodName] = function () {
  144. if (scPlayer !== null) {
  145. switch (methodName) {
  146. case 'play':
  147. return scPlayer.play();
  148. case 'pause':
  149. return scPlayer.pause();
  150. case 'load':
  151. return null;
  152. }
  153. } else {
  154. apiStack.push({ type: 'call', methodName: methodName });
  155. }
  156. };
  157. };
  158. for (var _i = 0, _total = methods.length; _i < _total; _i++) {
  159. assignMethods(methods[_i]);
  160. }
  161. window['__ready__' + sc.id] = function (_scPlayer) {
  162. mediaElement.scPlayer = scPlayer = _scPlayer;
  163. if (autoplay) {
  164. scPlayer.play();
  165. }
  166. if (apiStack.length) {
  167. for (var _i2 = 0, _total2 = apiStack.length; _i2 < _total2; _i2++) {
  168. var stackItem = apiStack[_i2];
  169. if (stackItem.type === 'set') {
  170. var propName = stackItem.propName,
  171. capName = '' + propName.substring(0, 1).toUpperCase() + propName.substring(1);
  172. sc['set' + capName](stackItem.value);
  173. } else if (stackItem.type === 'call') {
  174. sc[stackItem.methodName]();
  175. }
  176. }
  177. }
  178. scPlayer.bind(SC.Widget.Events.PLAY_PROGRESS, function () {
  179. paused = false;
  180. ended = false;
  181. scPlayer.getPosition(function (_currentTime) {
  182. currentTime = _currentTime / 1000;
  183. var event = mejs.Utils.createEvent('timeupdate', sc);
  184. mediaElement.dispatchEvent(event);
  185. });
  186. });
  187. scPlayer.bind(SC.Widget.Events.PAUSE, function () {
  188. paused = true;
  189. var event = mejs.Utils.createEvent('pause', sc);
  190. mediaElement.dispatchEvent(event);
  191. });
  192. scPlayer.bind(SC.Widget.Events.PLAY, function () {
  193. paused = false;
  194. ended = false;
  195. var event = mejs.Utils.createEvent('play', sc);
  196. mediaElement.dispatchEvent(event);
  197. });
  198. scPlayer.bind(SC.Widget.Events.FINISHED, function () {
  199. paused = false;
  200. ended = true;
  201. var event = mejs.Utils.createEvent('ended', sc);
  202. mediaElement.dispatchEvent(event);
  203. });
  204. scPlayer.bind(SC.Widget.Events.READY, function () {
  205. scPlayer.getDuration(function (_duration) {
  206. duration = _duration / 1000;
  207. var event = mejs.Utils.createEvent('loadedmetadata', sc);
  208. mediaElement.dispatchEvent(event);
  209. });
  210. });
  211. scPlayer.bind(SC.Widget.Events.LOAD_PROGRESS, function () {
  212. scPlayer.getDuration(function (loadProgress) {
  213. if (duration > 0) {
  214. bufferedTime = duration * loadProgress;
  215. var event = mejs.Utils.createEvent('progress', sc);
  216. mediaElement.dispatchEvent(event);
  217. }
  218. });
  219. scPlayer.getDuration(function (_duration) {
  220. duration = _duration;
  221. var event = mejs.Utils.createEvent('loadedmetadata', sc);
  222. mediaElement.dispatchEvent(event);
  223. });
  224. });
  225. var initEvents = ['rendererready', 'loadeddata', 'loadedmetadata', 'canplay'];
  226. for (var _i3 = 0, _total3 = initEvents.length; _i3 < _total3; _i3++) {
  227. var event = mejs.Utils.createEvent(initEvents[_i3], sc);
  228. mediaElement.dispatchEvent(event);
  229. }
  230. };
  231. scIframe = document.createElement('iframe');
  232. scIframe.id = sc.id;
  233. scIframe.width = isVideo ? '100%' : 1;
  234. scIframe.height = isVideo ? '100%' : 1;
  235. scIframe.frameBorder = 0;
  236. scIframe.style.visibility = isVideo ? 'visible' : 'hidden';
  237. scIframe.src = mediaFiles[0].src;
  238. scIframe.scrolling = 'no';
  239. mediaElement.appendChild(scIframe);
  240. mediaElement.originalNode.style.display = 'none';
  241. var scSettings = {
  242. iframe: scIframe,
  243. id: sc.id
  244. };
  245. SoundCloudApi.load(scSettings);
  246. sc.setSize = function () {};
  247. sc.hide = function () {
  248. sc.pause();
  249. if (scIframe) {
  250. scIframe.style.display = 'none';
  251. }
  252. };
  253. sc.show = function () {
  254. if (scIframe) {
  255. scIframe.style.display = '';
  256. }
  257. };
  258. sc.destroy = function () {
  259. scPlayer.destroy();
  260. };
  261. return sc;
  262. }
  263. };
  264. mejs.Utils.typeChecks.push(function (url) {
  265. return (/\/\/(w\.)?soundcloud.com/i.test(url) ? 'video/x-soundcloud' : null
  266. );
  267. });
  268. mejs.Renderers.add(SoundCloudIframeRenderer);
  269. },{}]},{},[1]);