diff options
author | Sam Potts <sam@potts.es> | 2018-06-13 00:41:30 +1000 |
---|---|---|
committer | Sam Potts <sam@potts.es> | 2018-06-13 00:41:30 +1000 |
commit | aae1092bacd464a074ee26feab6f371903e83d90 (patch) | |
tree | f64843714e6511c843a5597c4fed8ebea9474bfd /src/js/html5.js | |
parent | 392dfd024c505f5ae1bbb2f0d3e0793c251a1f35 (diff) | |
parent | 7158e507adb11e187380f3148750435f2bd611ae (diff) | |
download | plyr-aae1092bacd464a074ee26feab6f371903e83d90.tar.lz plyr-aae1092bacd464a074ee26feab6f371903e83d90.tar.xz plyr-aae1092bacd464a074ee26feab6f371903e83d90.zip |
Merge branch 'develop' of github.com:sampotts/plyr into develop
# Conflicts:
# src/js/captions.js
# src/js/controls.js
# src/js/fullscreen.js
# src/js/html5.js
# src/js/listeners.js
# src/js/plugins/youtube.js
# src/js/plyr.js
# src/js/utils.js
Diffstat (limited to 'src/js/html5.js')
-rw-r--r-- | src/js/html5.js | 85 |
1 files changed, 22 insertions, 63 deletions
diff --git a/src/js/html5.js b/src/js/html5.js index d13e6aa6..6aa96f4c 100644 --- a/src/js/html5.js +++ b/src/js/html5.js @@ -3,43 +3,28 @@ // ========================================================================== import support from './support'; -import { dedupe } from './utils/arrays'; import { removeElement } from './utils/elements'; -import { trigger } from './utils/events'; -import is from './utils/is'; +import { triggerEvent } from './utils/events'; const html5 = { getSources() { if (!this.isHTML5) { - return null; + return []; } - return this.media.querySelectorAll('source'); + const sources = Array.from(this.media.querySelectorAll('source')); + + // Filter out unsupported sources + return sources.filter(source => support.mime.call(this, source.getAttribute('type'))); }, // Get quality levels getQualityOptions() { - if (!this.isHTML5) { - return null; - } - - // Get sources - const sources = html5.getSources.call(this); - - if (is.empty(sources)) { - return null; - } - - // Get <source> with size attribute - const sizes = Array.from(sources).filter(source => !is.empty(source.getAttribute('size'))); - - // If none, bail - if (is.empty(sizes)) { - return null; - } - - // Reduce to unique list - return dedupe(sizes.map(source => Number(source.getAttribute('size')))); + // Get sizes from <source> elements + return html5.getSources + .call(this) + .map(source => Number(source.getAttribute('size'))) + .filter(Boolean); }, extend() { @@ -54,60 +39,34 @@ const html5 = { get() { // Get sources const sources = html5.getSources.call(player); + const [source] = sources.filter(source => source.getAttribute('src') === player.source); - if (is.empty(sources)) { - return null; - } - - const matches = Array.from(sources).filter(source => source.getAttribute('src') === player.source); - - if (is.empty(matches)) { - return null; - } - - return Number(matches[0].getAttribute('size')); + // Return size, if match is found + return source && Number(source.getAttribute('size')); }, set(input) { // Get sources const sources = html5.getSources.call(player); - if (is.empty(sources)) { - return; - } + // Get first match for requested size + const source = sources.find(source => Number(source.getAttribute('size')) === input); - // Get matches for requested size - const matches = Array.from(sources).filter(source => Number(source.getAttribute('size')) === input); - - // No matches for requested size - if (is.empty(matches)) { + // No matching source found + if (!source) { return; } - // Get supported sources - const supported = matches.filter(source => support.mime.call(player, source.getAttribute('type'))); - - // No supported sources - if (is.empty(supported)) { - return; - } - - // Trigger change event - trigger.call(player, player.media, 'qualityrequested', false, { - quality: input, - }); - // Get current state const { currentTime, playing } = player; // Set new source - player.media.src = supported[0].getAttribute('src'); + player.media.src = source.getAttribute('src'); // Restore time const onLoadedMetaData = () => { player.currentTime = currentTime; - player.off('loadedmetadata', onLoadedMetaData); }; - player.on('loadedmetadata', onLoadedMetaData); + player.once('loadedmetadata', onLoadedMetaData); // Load new source player.media.load(); @@ -118,7 +77,7 @@ const html5 = { } // Trigger change event - trigger.call(player, player.media, 'qualitychange', false, { + triggerEvent.call(player, player.media, 'qualitychange', false, { quality: input, }); }, @@ -133,7 +92,7 @@ const html5 = { } // Remove child sources - removeElement(html5.getSources()); + removeElement(html5.getSources.call(this)); // Set blank video src attribute // This is to prevent a MEDIA_ERR_SRC_NOT_SUPPORTED error |