Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2014
400
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. HTML
  2. <div class="player">
  3.     <div class="mediaplayer">
  4.         <video controls preload="none">
  5.             <source src="https://www.youtube.com/watch?v=RnqAXuLZlaE"/>
  6.         </video>
  7.     </div>
  8.     <hr />
  9.     <p><a href="http://afarkas.github.io/webshim/demos/index.html#Media-elements">webshims mediaelement documentation</a></p>
  10.     <hr />
  11.     <table>
  12.         <thead>
  13.             <th>property</th>
  14.             <th>value/control</th>
  15.         </thead>
  16.         <tbody>
  17.             <tr>
  18.                 <th>currentTime</th>
  19.                 <td><span class="current-time"></span>
  20.  
  21.                 </td>
  22.             </tr>
  23.             <tr>
  24.                 <th>duration</th>
  25.                 <td><span class="duration"></span>
  26.  
  27.                 </td>
  28.             </tr>
  29.             <tr>
  30.                 <th>progress</th>
  31.                 <td><span class="progress">0</span>
  32.  
  33.                 </td>
  34.             </tr>
  35.             <tr>
  36.                 <th>paused-state</th>
  37.                 <td><span class="paused-state">true</span>
  38.  
  39.                 </td>
  40.             </tr>
  41.             <tr>
  42.                 <th>muted-state</th>
  43.                 <td><span class="muted-state">false</span>
  44.  
  45.                 </td>
  46.             </tr>
  47.             <tr>
  48.                 <th>volume</th>
  49.                 <td><span class="volume">1</span>
  50.  
  51.                 </td>
  52.             </tr>
  53.             <tr>
  54.                 <th>videoWidth/videoHeight</th>
  55.                 <td><span class="height-width">-/-</span>
  56.  
  57.                 </td>
  58.             </tr>
  59.             <tr>
  60.                 <th>networkState</th>
  61.                 <td><span class="network-state"></span>
  62.  
  63.                 </td>
  64.             </tr>
  65.             <tr>
  66.                 <th>readyState</th>
  67.                 <td><span class="ready-state"></span>
  68.  
  69.                 </td>
  70.             </tr>
  71.             <tr>
  72.                 <th>currentTime</th>
  73.                 <td>
  74.                     <input class="time-slider" type="range" disabled value="0" step="any" />
  75.                 </td>
  76.             </tr>
  77.             <tr>
  78.                 <th>play</th>
  79.                 <td>
  80.                     <input value="play" type="button" class="play" />
  81.                 </td>
  82.             </tr>
  83.             <tr>
  84.                 <th>pause</th>
  85.                 <td>
  86.                     <input value="pause" type="button" class="pause" />
  87.                 </td>
  88.             </tr>
  89.             <tr>
  90.                 <th>muted</th>
  91.                 <td>
  92.                     <input class="muted" type="checkbox" />
  93.                 </td>
  94.             </tr>
  95.             <tr>
  96.                 <th>volume</th>
  97.                 <td>
  98.                     <input class="volume-slider" type="range" value="1" max="1" step="0.01" />
  99.                 </td>
  100.             </tr>
  101.         </tbody>
  102.     </table>
  103. </div>
  104.  
  105.  
  106. JQUERY
  107. if (jQuery.webshims && window.Modernizr) {
  108.     (function () {
  109.        
  110.         webshims.setOptions('mediaelement', {
  111.             replaceUI: 'auto'
  112.         });
  113.         webshims.setOptions({types: 'range'});
  114.         webshims.setOptions('extendNative', true);
  115.         webshims.polyfill('mediaelement forms forms-ext');
  116.     })();
  117. }
  118.  
  119.  
  120. //add some controls
  121. jQuery(function ($) {
  122.     $('div.player').each(function () {
  123.         var player = this;
  124.         var getSetCurrentTime = createGetSetHandler(
  125.  
  126.         function () {
  127.             $('input.time-slider', player).prop('value', $.prop(this, 'currentTime'));
  128.         }, function () {
  129.             try {
  130.                 $('video, audio', player).prop('currentTime', $.prop(this, 'value'));
  131.             } catch (er) {}
  132.         });
  133.         var getSetVolume = createGetSetHandler(
  134.  
  135.         function () {
  136.             $('input.volume-slider', player).prop('value', $.prop(this, 'volume'));
  137.  
  138.         }, function () {
  139.             $('video, audio', player).prop('volume', $.prop(this, 'value'));
  140.         });
  141.         $('video, audio', this).bind('durationchange updateMediaState', function () {
  142.             var duration = $.prop(this, 'duration');
  143.             if (!duration) {
  144.                 return;
  145.             }
  146.             $('input.time-slider', player).prop({
  147.                 'max': duration,
  148.                 disabled: false
  149.             });
  150.             $('span.duration', player).text(duration);
  151.         }).bind('progress updateMediaState', function () {
  152.             var buffered = $.prop(this, 'buffered');
  153.             if (!buffered || !buffered.length) {
  154.                 return;
  155.             }
  156.             buffered = getActiveTimeRange(buffered, $.prop(this, 'currentTime'));
  157.             $('span.progress', player).text(buffered[2]);
  158.         }).bind('timeupdate', function () {
  159.             $('span.current-time', player).text($.prop(this, 'currentTime'));
  160.         }).bind('timeupdate', getSetCurrentTime.get).bind('emptied', function () {
  161.             $('input.time-slider', player).prop('disabled', true);
  162.             $('span.duration', player).text('--');
  163.             $('span.current-time', player).text(0);
  164.             $('span.network-state', player).text(0);
  165.             $('span.ready-state', player).text(0);
  166.             $('span.paused-state', player).text($.prop(this, 'paused'));
  167.             $('span.height-width', player).text('-/-');
  168.             $('span.progress', player).text('0');
  169.         }).bind('waiting playing loadedmetadata updateMediaState', function () {
  170.             $('span.network-state', player).text($.prop(this, 'networkState'));
  171.             $('span.ready-state', player).text($.prop(this, 'readyState'));
  172.         }).bind('play pause', function () {
  173.             $('span.paused-state', player).text($.prop(this, 'paused'));
  174.         }).bind('volumechange', function () {
  175.             var muted = $.prop(this, 'muted');
  176.             $('span.muted-state', player).text(muted);
  177.             $('input.muted', player).prop('checked', muted);
  178.             $('span.volume', player).text($.prop(this, 'volume'));
  179.         }).bind('volumechange', getSetVolume.get).bind('play pause', function () {
  180.             $('span.paused-state', player).text($.prop(this, 'paused'));
  181.         }).bind('loadedmetadata updateMediaState', function () {
  182.             $('span.height-width', player).text($.prop(this, 'videoWidth') + '/' + $.prop(this, 'videoHeight'));
  183.         }).each(function () {
  184.             if ($.prop(this, 'readyState') > $.prop(this, 'HAVE_NOTHING')) {
  185.                 $(this).triggerHandler('updateMediaState');
  186.             }
  187.         });
  188.  
  189.         $('input.time-slider', player).bind('input', getSetCurrentTime.set).prop('value', 0);
  190.         $('input.volume-slider', player).bind('input', getSetVolume.set);
  191.  
  192.         $('input.play', player).bind('click', function () {
  193.             $('video, audio', player)[0].play();
  194.         });
  195.         $('input.pause', player).bind('click', function () {
  196.             $('video, audio', player)[0].pause();
  197.         });
  198.         $('input.muted', player).bind('click updatemuted', function () {
  199.             $('video, audio', player).prop('muted', $.prop(this, 'checked'));
  200.         }).triggerHandler('updatemuted');
  201.         $('input.controls', player).bind('click', function () {
  202.             $('video, audio', player).prop('controls', $.prop(this, 'checked'));
  203.         }).prop('checked', true);
  204.     });
  205. });
  206.  
  207. //helper for createing throttled get/set functions (good to create time/volume-slider, which are used as getter and setter)
  208.  
  209. function createGetSetHandler(get, set) {
  210.     var throttleTimer;
  211.     var blockedTimer;
  212.     var blocked;
  213.     return {
  214.         get: function () {
  215.             if (blocked) {
  216.                 return;
  217.             }
  218.             return get.apply(this, arguments);
  219.         },
  220.         set: function () {
  221.             clearTimeout(throttleTimer);
  222.             clearTimeout(blockedTimer);
  223.  
  224.             var that = this;
  225.             var args = arguments;
  226.             blocked = true;
  227.             throttleTimer = setTimeout(function () {
  228.                 set.apply(that, args);
  229.                 blockedTimer = setTimeout(function () {
  230.                     blocked = false;
  231.                 }, 30);
  232.             }, 0);
  233.         }
  234.     };
  235. };
  236.  
  237. function getActiveTimeRange(range, time) {
  238.     var len = range.length;
  239.     var index = -1;
  240.     var start = 0;
  241.     var end = 0;
  242.     for (var i = 0; i < len; i++) {
  243.         if (time >= (start = range.start(i)) && time <= (end = range.end(i))) {
  244.             index = i;
  245.             break;
  246.         }
  247.     }
  248.     return [index, start, end];
  249. };
  250.  
  251.  
  252. CSS:
  253. .player {
  254.     margin: auto;
  255.     padding: 10px;
  256.     width: 90%;
  257.     max-width: 900px;
  258.     min-width: 320px;
  259. }
  260. .mediaplayer {
  261.     position: relative;
  262.     height: 0;
  263.     width: 100%;
  264.     padding-bottom: 56.25%;
  265.     /* 16/9 */
  266. }
  267. .mediaplayer video, .mediaplayer .polyfill-video {
  268.     position: absolute;
  269.     top: 0;
  270.     left: 0;
  271.     height: 100%;
  272.     width: 100%;
  273. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement