PageRenderTime 33ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 1ms

/files/jplayer/2.3.0/popcorn/popcorn.jplayer.js

https://gitlab.com/Mirros/jsdelivr
JavaScript | 559 lines | 412 code | 58 blank | 89 comment | 96 complexity | a53c807c2e0c32dd6d81c94e2a4b06c9 MD5 | raw file
  1. /*
  2. * jPlayer Player Plugin for Popcorn JavaScript Library
  3. * http://www.jplayer.org
  4. *
  5. * Copyright (c) 2013 Happyworm Ltd
  6. * Dual licensed under the MIT and GPL licenses.
  7. * - http://www.opensource.org/licenses/mit-license.php
  8. * - http://www.gnu.org/copyleft/gpl.html
  9. *
  10. * Author: Mark J Panaghiston
  11. * Version: 1.1.0
  12. * Date: 20th April 2013
  13. *
  14. * For Popcorn Version: 1.3
  15. * For jPlayer Version: 2.3.0
  16. * Requires: jQuery 1.3.2+
  17. * Note: jQuery dependancy cannot be removed since jPlayer 2 is a jQuery plugin. Use of jQuery will be kept to a minimum.
  18. */
  19. /* Code verified using http://www.jshint.com/ */
  20. /*jshint asi:false, bitwise:false, boss:false, browser:true, curly:false, debug:false, eqeqeq:true, eqnull:false, evil:false, forin:false, immed:false, jquery:true, laxbreak:false, newcap:true, noarg:true, noempty:true, nonew:true, onevar:false, passfail:false, plusplus:false, regexp:false, undef:true, sub:false, strict:false, white:false, smarttabs:true */
  21. /*global Popcorn:false, console:false */
  22. (function(Popcorn) {
  23. var JQUERY_SCRIPT = 'http://ajax.googleapis.com/ajax/libs/jquery/1.8/jquery.min.js', // Used if jQuery not already present.
  24. JPLAYER_SCRIPT = 'http://www.jplayer.org/2.3.0/js/jquery.jplayer.min.js', // Used if jPlayer not already present.
  25. JPLAYER_SWFPATH = 'http://www.jplayer.org/2.3.0/js/Jplayer.swf', // Used if not specified in jPlayer options via SRC Object.
  26. SOLUTION = 'html,flash', // The default solution option.
  27. DEBUG = false, // Decided to leave the debugging option and console output in for the time being. Overhead is trivial.
  28. jQueryDownloading = false, // Flag to stop multiple instances from each pulling in jQuery, thus corrupting it.
  29. jPlayerDownloading = false, // Flag to stop multiple instances from each pulling in jPlayer, thus corrupting it.
  30. format = { // Duplicate of jPlayer 2.3.0 object, to avoid always requiring jQuery and jPlayer to be loaded before performing the _canPlayType() test.
  31. mp3: {
  32. codec: 'audio/mpeg; codecs="mp3"',
  33. flashCanPlay: true,
  34. media: 'audio'
  35. },
  36. m4a: { // AAC / MP4
  37. codec: 'audio/mp4; codecs="mp4a.40.2"',
  38. flashCanPlay: true,
  39. media: 'audio'
  40. },
  41. oga: { // OGG
  42. codec: 'audio/ogg; codecs="vorbis"',
  43. flashCanPlay: false,
  44. media: 'audio'
  45. },
  46. wav: { // PCM
  47. codec: 'audio/wav; codecs="1"',
  48. flashCanPlay: false,
  49. media: 'audio'
  50. },
  51. webma: { // WEBM
  52. codec: 'audio/webm; codecs="vorbis"',
  53. flashCanPlay: false,
  54. media: 'audio'
  55. },
  56. fla: { // FLV / F4A
  57. codec: 'audio/x-flv',
  58. flashCanPlay: true,
  59. media: 'audio'
  60. },
  61. rtmpa: { // RTMP AUDIO
  62. codec: 'audio/rtmp; codecs="rtmp"',
  63. flashCanPlay: true,
  64. media: 'audio'
  65. },
  66. m4v: { // H.264 / MP4
  67. codec: 'video/mp4; codecs="avc1.42E01E, mp4a.40.2"',
  68. flashCanPlay: true,
  69. media: 'video'
  70. },
  71. ogv: { // OGG
  72. codec: 'video/ogg; codecs="theora, vorbis"',
  73. flashCanPlay: false,
  74. media: 'video'
  75. },
  76. webmv: { // WEBM
  77. codec: 'video/webm; codecs="vorbis, vp8"',
  78. flashCanPlay: false,
  79. media: 'video'
  80. },
  81. flv: { // FLV / F4V
  82. codec: 'video/x-flv',
  83. flashCanPlay: true,
  84. media: 'video'
  85. },
  86. rtmpv: { // RTMP VIDEO
  87. codec: 'video/rtmp; codecs="rtmp"',
  88. flashCanPlay: true,
  89. media: 'video'
  90. }
  91. },
  92. isObject = function(val) { // Basic check for Object
  93. if(val && typeof val === 'object' && val.hasOwnProperty) {
  94. return true;
  95. } else {
  96. return false;
  97. }
  98. },
  99. getMediaType = function(url) { // Function to gleam the media type from the URL
  100. var mediaType = false;
  101. if(/\.mp3$/i.test(url)) {
  102. mediaType = 'mp3';
  103. } else if(/\.mp4$/i.test(url) || /\.m4v$/i.test(url)) {
  104. mediaType = 'm4v';
  105. } else if(/\.m4a$/i.test(url)) {
  106. mediaType = 'm4a';
  107. } else if(/\.ogg$/i.test(url) || /\.oga$/i.test(url)) {
  108. mediaType = 'oga';
  109. } else if(/\.ogv$/i.test(url)) {
  110. mediaType = 'ogv';
  111. } else if(/\.webm$/i.test(url)) {
  112. mediaType = 'webmv';
  113. }
  114. return mediaType;
  115. },
  116. getSupplied = function(url) { // Function to generate a supplied option from an src object. ie., When supplied not specified.
  117. var supplied = '',
  118. separator = '';
  119. if(isObject(url)) {
  120. // Generate supplied option from object's properties. Non-format properties would be ignored by jPlayer. Order is unpredictable.
  121. for(var prop in url) {
  122. if(url.hasOwnProperty(prop)) {
  123. supplied += separator + prop;
  124. separator = ',';
  125. }
  126. }
  127. }
  128. if(DEBUG) console.log('getSupplied(): Generated: supplied = "' + supplied + '"');
  129. return supplied;
  130. };
  131. Popcorn.player( 'jplayer', {
  132. _canPlayType: function( containerType, url ) {
  133. // url : Either a String or an Object structured similar a jPlayer media object. ie., As used by setMedia in jPlayer.
  134. // The url object may also contain a solution and supplied property.
  135. // Define the src object structure here!
  136. var cType = containerType.toLowerCase(),
  137. srcObj = {
  138. media:{},
  139. options:{}
  140. },
  141. rVal = false, // Only a boolean false means it is not supported.
  142. mediaType;
  143. if(cType !== 'video' && cType !== 'audio') {
  144. if(typeof url === 'string') {
  145. // Check it starts with http, so the URL is absolute... Well, it is not a perfect check.
  146. if(/^http.*/i.test(url)) {
  147. mediaType = getMediaType(url);
  148. if(mediaType) {
  149. srcObj.media[mediaType] = url;
  150. srcObj.options.solution = SOLUTION;
  151. srcObj.options.supplied = mediaType;
  152. }
  153. }
  154. } else {
  155. srcObj = url; // Assume the url is an src object.
  156. }
  157. // Check for Object and appropriate minimum data structure.
  158. if(isObject(srcObj) && isObject(srcObj.media)) {
  159. if(!isObject(srcObj.options)) {
  160. srcObj.options = {};
  161. }
  162. if(!srcObj.options.solution) {
  163. srcObj.options.solution = SOLUTION;
  164. }
  165. if(!srcObj.options.supplied) {
  166. srcObj.options.supplied = getSupplied(srcObj.media);
  167. }
  168. // Figure out how jPlayer will play it.
  169. // This may not work properly when both audio and video is supplied. ie., A media player. But it should return truethy and jPlayer can figure it out.
  170. var solution = srcObj.options.solution.toLowerCase().split(","), // Create the solution array, with prority based on the order of the solution string.
  171. supplied = srcObj.options.supplied.toLowerCase().split(","); // Create the supplied formats array, with prority based on the order of the supplied formats string.
  172. for(var sol = 0; sol < solution.length; sol++) {
  173. var solutionType = solution[sol].replace(/^\s+|\s+$/g, ""), //trim
  174. checkingHtml = solutionType === 'html',
  175. checkingFlash = solutionType === 'flash',
  176. mediaElem;
  177. for(var fmt = 0; fmt < supplied.length; fmt++) {
  178. mediaType = supplied[fmt].replace(/^\s+|\s+$/g, ""); //trim
  179. if(format[mediaType]) { // Check format is valid.
  180. // Create an HTML5 media element for the type of media.
  181. if(!mediaElem && checkingHtml) {
  182. mediaElem = document.createElement(format[mediaType].media);
  183. }
  184. // See if the HTML5 media element can play the MIME / Codec type.
  185. // Flash also returns the object if the format is playable, so it is truethy, but that html property is false.
  186. // This assumes Flash is available, but that should be dealt with by jPlayer if that happens.
  187. var htmlCanPlay = !!(mediaElem && mediaElem.canPlayType && mediaElem.canPlayType(format[mediaType].codec)),
  188. htmlWillPlay = htmlCanPlay && checkingHtml,
  189. flashWillPlay = format[mediaType].flashCanPlay && checkingFlash;
  190. // The first one found will match what jPlayer uses.
  191. if(htmlWillPlay || flashWillPlay) {
  192. rVal = {
  193. html: htmlWillPlay,
  194. type: mediaType
  195. };
  196. sol = solution.length; // Exit solution loop
  197. fmt = supplied.length; // Exit supplied loop
  198. }
  199. }
  200. }
  201. }
  202. }
  203. }
  204. return rVal;
  205. },
  206. // _setup: function( options ) { // Warning: options is deprecated.
  207. _setup: function() {
  208. var media = this,
  209. myPlayer, // The jQuery selector of the jPlayer element. Usually a <div>
  210. jPlayerObj, // The jPlayer data instance. For performance and DRY code.
  211. mediaType = 'unknown',
  212. jpMedia = {},
  213. jpOptions = {},
  214. ready = false, // Used during init to override the annoying duration dependance in the track event padding during Popcorn's isReady(). ie., We is ready after loadeddata and duration can then be set real value at leisure.
  215. duration = 0, // For the durationchange event with both HTML5 and Flash solutions. Used with 'ready' to keep control during the Popcorn isReady() via loadeddata event. (Duration=0 is bad.)
  216. durationchangeId = null, // A timeout ID used with delayed durationchange event. (Because of the duration=NaN fudge to avoid Popcorn track event corruption.)
  217. canplaythrough = false,
  218. error = null, // The MediaError object.
  219. dispatchDurationChange = function() {
  220. if(ready) {
  221. if(DEBUG) console.log('Dispatched event : durationchange : ' + duration);
  222. media.dispatchEvent('durationchange');
  223. } else {
  224. if(DEBUG) console.log('DELAYED EVENT (!ready) : durationchange : ' + duration);
  225. clearTimeout(durationchangeId); // Stop multiple triggers causing multiple timeouts running in parallel.
  226. durationchangeId = setTimeout(dispatchDurationChange, 250);
  227. }
  228. },
  229. jPlayerFlashEventsPatch = function() {
  230. /* Events already supported by jPlayer Flash:
  231. * loadstart
  232. * loadedmetadata (M4A, M4V)
  233. * progress
  234. * play
  235. * pause
  236. * seeking
  237. * seeked
  238. * timeupdate
  239. * ended
  240. * volumechange
  241. * error <- See the custom handler in jPlayerInit()
  242. */
  243. /* Events patched:
  244. * loadeddata
  245. * durationchange
  246. * canplaythrough
  247. * playing
  248. */
  249. /* Events NOT patched:
  250. * suspend
  251. * abort
  252. * emptied
  253. * stalled
  254. * loadedmetadata (MP3)
  255. * waiting
  256. * canplay
  257. * ratechange
  258. */
  259. // Triggering patched events through the jPlayer Object so the events are homogeneous. ie., The contain the event.jPlayer data structure.
  260. var checkDuration = function(event) {
  261. if(event.jPlayer.status.duration !== duration) {
  262. duration = event.jPlayer.status.duration;
  263. dispatchDurationChange();
  264. }
  265. },
  266. checkCanPlayThrough = function(event) {
  267. if(!canplaythrough && event.jPlayer.status.seekPercent === 100) {
  268. canplaythrough = true;
  269. setTimeout(function() {
  270. if(DEBUG) console.log('Trigger : canplaythrough');
  271. jPlayerObj._trigger($.jPlayer.event.canplaythrough);
  272. }, 0);
  273. }
  274. };
  275. myPlayer.bind($.jPlayer.event.loadstart, function() {
  276. setTimeout(function() {
  277. if(DEBUG) console.log('Trigger : loadeddata');
  278. jPlayerObj._trigger($.jPlayer.event.loadeddata);
  279. }, 0);
  280. })
  281. .bind($.jPlayer.event.progress, function(event) {
  282. checkDuration(event);
  283. checkCanPlayThrough(event);
  284. })
  285. .bind($.jPlayer.event.timeupdate, function(event) {
  286. checkDuration(event);
  287. checkCanPlayThrough(event);
  288. })
  289. .bind($.jPlayer.event.play, function() {
  290. setTimeout(function() {
  291. if(DEBUG) console.log('Trigger : playing');
  292. jPlayerObj._trigger($.jPlayer.event.playing);
  293. }, 0);
  294. });
  295. if(DEBUG) console.log('Created CUSTOM event handlers for FLASH');
  296. },
  297. jPlayerInit = function() {
  298. (function($) {
  299. myPlayer = $('#' + media.id);
  300. if(typeof media.src === 'string') {
  301. mediaType = getMediaType(media.src);
  302. jpMedia[mediaType] = media.src;
  303. jpOptions.supplied = mediaType;
  304. jpOptions.solution = SOLUTION;
  305. } else if(isObject(media.src)) {
  306. jpMedia = isObject(media.src.media) ? media.src.media : {};
  307. jpOptions = isObject(media.src.options) ? media.src.options : {};
  308. jpOptions.solution = jpOptions.solution || SOLUTION;
  309. jpOptions.supplied = jpOptions.supplied || getSupplied(media.src.media);
  310. }
  311. // Allow the swfPath to be set to local server. ie., If the jPlayer Plugin is local and already on the page, then you can also use the local SWF.
  312. jpOptions.swfPath = jpOptions.swfPath || JPLAYER_SWFPATH;
  313. myPlayer.bind($.jPlayer.event.ready, function(event) {
  314. if(event.jPlayer.flash.used) {
  315. jPlayerFlashEventsPatch();
  316. }
  317. // Set the media andd load it, so that the Flash solution behaves similar to HTML5 solution.
  318. // This also allows the loadstart event to be used to know jPlayer is ready.
  319. $(this).jPlayer('setMedia', jpMedia).jPlayer('load');
  320. });
  321. // Do not auto-bubble the reserved events, nor the loadeddata and durationchange event, since the duration must be carefully handled when loadeddata event occurs.
  322. // See the duration property code for more details. (Ranting.)
  323. var reservedEvents = $.jPlayer.reservedEvent + ' loadeddata durationchange',
  324. reservedEvent = reservedEvents.split(/\s+/g);
  325. // Generate event handlers for all the standard HTML5 media events. (Except durationchange)
  326. var bindEvent = function(name) {
  327. myPlayer.bind($.jPlayer.event[name], function(event) {
  328. if(DEBUG) console.log('Dispatched event: ' + name + (event && event.jPlayer ? ' (' + event.jPlayer.status.currentTime + 's)' : '')); // Must be after dispatch for some reason on Firefox/Opera
  329. media.dispatchEvent(name);
  330. });
  331. if(DEBUG) console.log('Created event handler for: ' + name);
  332. };
  333. for(var eventName in $.jPlayer.event) {
  334. if($.jPlayer.event.hasOwnProperty(eventName)) {
  335. var nativeEvent = true;
  336. for(var iRes in reservedEvent) {
  337. if(reservedEvent.hasOwnProperty(iRes)) {
  338. if(reservedEvent[iRes] === eventName) {
  339. nativeEvent = false;
  340. break;
  341. }
  342. }
  343. }
  344. if(nativeEvent) {
  345. bindEvent(eventName);
  346. } else {
  347. if(DEBUG) console.log('Skipped auto event handler creation for: ' + eventName);
  348. }
  349. }
  350. }
  351. myPlayer.bind($.jPlayer.event.loadeddata, function(event) {
  352. if(DEBUG) console.log('Dispatched event: loadeddata' + (event && event.jPlayer ? ' (' + event.jPlayer.status.currentTime + 's)' : ''));
  353. media.dispatchEvent('loadeddata');
  354. ready = true;
  355. });
  356. if(DEBUG) console.log('Created CUSTOM event handler for: loadeddata');
  357. myPlayer.bind($.jPlayer.event.durationchange, function(event) {
  358. duration = event.jPlayer.status.duration;
  359. dispatchDurationChange();
  360. });
  361. if(DEBUG) console.log('Created CUSTOM event handler for: durationchange');
  362. // The error event is a special case. Plus jPlayer error event assumes it is a broken URL. (It could also be a decoder error... Or aborted or a Network error.)
  363. myPlayer.bind($.jPlayer.event.error, function(event) {
  364. // Not sure how to handle the error situation. Popcorn does not appear to have the error or error.code property documented here: http://popcornjs.org/popcorn-docs/media-methods/
  365. // If any error event happens, then something has gone pear shaped.
  366. error = event.jPlayer.error; // Saving object pointer, not a copy of the object. Possible garbage collection issue... But the player is dead anyway, so don't care.
  367. if(error.type === $.jPlayer.error.URL) {
  368. error.code = 4; // MEDIA_ERR_SRC_NOT_SUPPORTED since jPlayer makes this assumption. It is the most common error, then the decode error. Never seen either of the other 2 error types occur.
  369. } else {
  370. error.code = 0; // It was a jPlayer error, not an HTML5 media error.
  371. }
  372. if(DEBUG) console.log('Dispatched event: error');
  373. if(DEBUG) console.dir(error);
  374. media.dispatchEvent('error');
  375. });
  376. if(DEBUG) console.log('Created CUSTOM event handler for: error');
  377. Popcorn.player.defineProperty( media, 'error', {
  378. set: function() {
  379. // Read-only property
  380. return error;
  381. },
  382. get: function() {
  383. return error;
  384. }
  385. });
  386. Popcorn.player.defineProperty( media, 'currentTime', {
  387. set: function( val ) {
  388. if(jPlayerObj.status.paused) {
  389. myPlayer.jPlayer('pause', val);
  390. } else {
  391. myPlayer.jPlayer('play', val);
  392. }
  393. return val;
  394. },
  395. get: function() {
  396. return jPlayerObj.status.currentTime;
  397. }
  398. });
  399. /* The joy of duration and the loadeddata event isReady() handler
  400. * The duration is assumed to be a NaN or a valid duration.
  401. * jPlayer uses zero instead of a NaN and this screws up the Popcorn track event start/end arrays padding.
  402. * This line here:
  403. * videoDurationPlus = duration != duration ? Number.MAX_VALUE : duration + 1;
  404. * Not sure why it is not simply:
  405. * videoDurationPlus = Number.MAX_VALUE; // Who cares if the padding is close to the real duration?
  406. * So if you trigger loadeddata before the duration is correct, the track event padding is screwed up. (It pads the start, not the end... Well, duration+1 = 0+1 = 1s)
  407. * That line makes the MP3 Flash fallback difficult to setup. The whole MP3 will need to load before the duration is known.
  408. * Planning on using a NaN for duration until a >0 value is found... Except with MP3, where seekPercent must be 100% before setting the duration.
  409. * Why not just use a NaN during init... And then correct the duration later?
  410. */
  411. Popcorn.player.defineProperty( media, 'duration', {
  412. set: function() {
  413. // Read-only property
  414. if(ready) {
  415. return duration;
  416. } else {
  417. return NaN;
  418. }
  419. },
  420. get: function() {
  421. if(ready) {
  422. return duration; // Popcorn has initialized, we can now use duration zero or whatever without fear.
  423. } else {
  424. return NaN; // Keep the duration a NaN until after loadeddata event has occurred. Otherwise Popcorn track event padding is corrupted.
  425. }
  426. }
  427. });
  428. Popcorn.player.defineProperty( media, 'muted', {
  429. set: function( val ) {
  430. myPlayer.jPlayer('mute', val);
  431. return jPlayerObj.options.muted;
  432. },
  433. get: function() {
  434. return jPlayerObj.options.muted;
  435. }
  436. });
  437. Popcorn.player.defineProperty( media, 'volume', {
  438. set: function( val ) {
  439. myPlayer.jPlayer('volume', val);
  440. return jPlayerObj.options.volume;
  441. },
  442. get: function() {
  443. return jPlayerObj.options.volume;
  444. }
  445. });
  446. Popcorn.player.defineProperty( media, 'paused', {
  447. set: function() {
  448. // Read-only property
  449. return jPlayerObj.status.paused;
  450. },
  451. get: function() {
  452. return jPlayerObj.status.paused;
  453. }
  454. });
  455. media.play = function() {
  456. myPlayer.jPlayer('play');
  457. };
  458. media.pause = function() {
  459. myPlayer.jPlayer('pause');
  460. };
  461. myPlayer.jPlayer(jpOptions); // Instance jPlayer. Note that the options should not have a ready event defined... Kill it by default?
  462. jPlayerObj = myPlayer.data('jPlayer');
  463. }(jQuery));
  464. },
  465. jPlayerCheck = function() {
  466. if (!jQuery.jPlayer) {
  467. if (!jPlayerDownloading) {
  468. jPlayerDownloading = true;
  469. Popcorn.getScript(JPLAYER_SCRIPT, function() {
  470. jPlayerDownloading = false;
  471. jPlayerInit();
  472. });
  473. } else {
  474. setTimeout(jPlayerCheck, 250);
  475. }
  476. } else {
  477. jPlayerInit();
  478. }
  479. },
  480. jQueryCheck = function() {
  481. if (!window.jQuery) {
  482. if (!jQueryDownloading) {
  483. jQueryDownloading = true;
  484. Popcorn.getScript(JQUERY_SCRIPT, function() {
  485. jQueryDownloading = false;
  486. jPlayerCheck();
  487. });
  488. } else {
  489. setTimeout(jQueryCheck, 250);
  490. }
  491. } else {
  492. jPlayerCheck();
  493. }
  494. };
  495. jQueryCheck();
  496. },
  497. _teardown: function() {
  498. jQuery('#' + this.id).jPlayer('destroy');
  499. }
  500. });
  501. }(Popcorn));