Blame view
static/plugins/webshim-gh-pages/js-webshim/dev/shims/mediaelement-native-fix.js
4.29 KB
831eac332 add file |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
webshims.register('mediaelement-native-fix', function($, webshims, window, document, undefined){ var support = webshims.support; var fixBuffered = (function(){ if(support.videoBuffered){return $.noop;} var getBufferedData = function(elem){ var data = webshims.data(elem, 'mediaelementBuffered'); if(!data){ data = { buffered: { start: function(index){ if(index >= data.buffered.length){ webshims.error('buffered index size error'); return; } return 0; }, end: function(index){ if(index >= data.buffered.length){ webshims.error('buffered index size error'); return; } return data.loaded; }, length: 0 }, loaded: 0 }; webshims.data(elem, 'mediaelementBuffered', data); } return data; }; var loadProgessListener = function(e){ e = e.originalEvent; if(!e || !('lengthComputable' in e)){return;} var data = webshims.data(e.target, 'mediaelement'); if(data && data.isActive != 'html5'){return;} if(e.lengthComputable && 'loaded' in e){ var duration = e.target.duration; var bufferedData = getBufferedData(e.target); bufferedData.loaded = (duration) ? e.loaded / e.total * duration : 0; if(bufferedData.loaded){ bufferedData.buffered.length = 1; } if(e.type == 'load'){ $(e.target).triggerHandler('progress'); } } }; var removeProgress = function(e){ var data = getBufferedData(e.target); data.buffered.length = 0; data.loaded = 0; }; ['audio', 'video'].forEach(function(nodeName){ var sup = webshims.defineNodeNameProperty(nodeName, 'buffered', { prop: { get: function(){ var data = webshims.data(this, 'mediaelement'); if(data && data.isActive == 'flash' && sup.prop._supget){ sup.prop._supget.apply(this); } else { return getBufferedData(this).buffered; } } } }); }); return function(){ $(this) .off('load progress', loadProgessListener) .off('emptied', removeProgress) .on('load progress', loadProgessListener) .on('emptied', removeProgress) ; }; })(); (function(){ var videoElem = document.createElement('video'); if( !('preload' in videoElem) && ('autobuffer' in videoElem)){ var noBufferProps = { metadata: 1, none: 1 }; webshims.onNodeNamesPropertyModify(['audio', 'video'], ['preload'], { set: function(value, boolValue, curType){ if(noBufferProps[value] || curType == 'removeAttr'){ this.autobuffer = false; } else if( !(webshims.data(this, 'mediaelement') || {}).isActive == 'html5') { this.autobuffer = true; } }, initAttr: true }); } })(); var fixProgressEvent = (function(){ if(support.mediaDefaultMuted){return $.noop;} return function(){ if($.data(this, 'fixedMediaProgress')){return;} var bufferTimer; var lastBuffered; var elem = this; var getBufferedString = function(){ var buffered = $.prop(elem, 'buffered'); if(!buffered){return;} var bufferString = ""; for(var i = 0, len = buffered.length; i < len;i++){ bufferString += buffered.end(i); } return bufferString; }; var testBuffer = function(){ var buffered = getBufferedString(); if(buffered != lastBuffered){ lastBuffered = buffered; webshims.error('needed to trigger progress manually'); $(elem).triggerHandler('progress'); } }; $(this) .data('fixedMediaProgress', true) .on({ 'play loadstart progress': function(e){ if(e.type == 'progress'){ lastBuffered = getBufferedString(this); } clearTimeout(bufferTimer); bufferTimer = setTimeout(testBuffer, 800); }, 'emptied stalled mediaerror abort suspend': function(e){ if(e.type == 'emptied'){ lastBuffered = false; } clearTimeout(bufferTimer); } }) ; if('ActiveXObject' in window && $.prop(this, 'paused') && !$.prop(this, 'readyState') && $(this).is('audio[preload="none"][controls]:not([autoplay],.nonnative-api-active)')){ $(this).prop('preload', 'metadata').mediaLoad(); } }; })(); webshims.addReady(function(context, insertedElement){ $('video, audio', context) .add(insertedElement.filter('video, audio')) .each(fixBuffered) .each(fixProgressEvent) ; }); }); |