Merge pull request #356 from amowu/feature/add-playback-speed
Add playback speed
This commit is contained in:
@ -20,7 +20,8 @@
|
|||||||
},
|
},
|
||||||
captions: {
|
captions: {
|
||||||
defaultActive: true
|
defaultActive: true
|
||||||
}
|
},
|
||||||
|
controls: ['play-large', 'play', 'speed-up', 'progress', 'current-time', 'mute', 'volume', 'captions', 'fullscreen']
|
||||||
});
|
});
|
||||||
plyr.loadSprite('dist/demo.svg');
|
plyr.loadSprite('dist/demo.svg');
|
||||||
|
|
||||||
|
@ -244,7 +244,7 @@ Note the single quotes encapsulating the JSON and double quotes on the object ke
|
|||||||
<tr>
|
<tr>
|
||||||
<td><code>controls</code></td>
|
<td><code>controls</code></td>
|
||||||
<td>Array</td>
|
<td>Array</td>
|
||||||
<td><code>['play-large', 'play', 'progress', 'current-time', 'mute', 'volume', 'captions', 'fullscreen']</code></td>
|
<td><code>['play-large', 'play', 'speed-up', 'progress', 'current-time', 'mute', 'volume', 'captions', 'fullscreen']</code></td>
|
||||||
<td>Toggle which control elements you would like to display when using the default controls html. If you specify a <code>html</code> option, this is redundant. The default value is to display everything.</td>
|
<td>Toggle which control elements you would like to display when using the default controls html. If you specify a <code>html</code> option, this is redundant. The default value is to display everything.</td>
|
||||||
</tr>
|
</tr>
|
||||||
<tr>
|
<tr>
|
||||||
@ -383,6 +383,12 @@ Note the single quotes encapsulating the JSON and double quotes on the object ke
|
|||||||
<td>—</td>
|
<td>—</td>
|
||||||
<td>Two properties; <code>enabled</code> which toggles if local storage should be enabled (if the browser supports it). The default value is `true`. This enables storing user settings, currently it only stores volume but more will be added later. The second property <code>key</code> is the key used for the local storage. The default is <code>plyr_volume</code> until more settings are stored.</td>
|
<td>Two properties; <code>enabled</code> which toggles if local storage should be enabled (if the browser supports it). The default value is `true`. This enables storing user settings, currently it only stores volume but more will be added later. The second property <code>key</code> is the key used for the local storage. The default is <code>plyr_volume</code> until more settings are stored.</td>
|
||||||
</tr>
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td><code>speeds</code></td>
|
||||||
|
<td>Array</td>
|
||||||
|
<td>[1.0, 1.5, 2.0, 0.5]</td>
|
||||||
|
<td>Playback speed list.</td>
|
||||||
|
</tr>
|
||||||
</tbody>
|
</tbody>
|
||||||
</table>
|
</table>
|
||||||
|
|
||||||
|
@ -39,6 +39,14 @@
|
|||||||
volumeMin: 0,
|
volumeMin: 0,
|
||||||
volumeMax: 10,
|
volumeMax: 10,
|
||||||
volumeStep: 1,
|
volumeStep: 1,
|
||||||
|
defaultSpeed: 1.0,
|
||||||
|
currentSpeed: 1.0,
|
||||||
|
speeds: [
|
||||||
|
0.5,
|
||||||
|
1.0,
|
||||||
|
1.5,
|
||||||
|
2.0
|
||||||
|
],
|
||||||
duration: null,
|
duration: null,
|
||||||
displayDuration: true,
|
displayDuration: true,
|
||||||
loadSprite: true,
|
loadSprite: true,
|
||||||
@ -75,7 +83,8 @@
|
|||||||
forward: '[data-plyr="fast-forward"]',
|
forward: '[data-plyr="fast-forward"]',
|
||||||
mute: '[data-plyr="mute"]',
|
mute: '[data-plyr="mute"]',
|
||||||
captions: '[data-plyr="captions"]',
|
captions: '[data-plyr="captions"]',
|
||||||
fullscreen: '[data-plyr="fullscreen"]'
|
fullscreen: '[data-plyr="fullscreen"]',
|
||||||
|
speedup: '[data-plyr="speed-up"]'
|
||||||
},
|
},
|
||||||
volume: {
|
volume: {
|
||||||
input: '[data-plyr="volume"]',
|
input: '[data-plyr="volume"]',
|
||||||
@ -143,7 +152,8 @@
|
|||||||
toggleMute: 'Toggle Mute',
|
toggleMute: 'Toggle Mute',
|
||||||
toggleCaptions: 'Toggle Captions',
|
toggleCaptions: 'Toggle Captions',
|
||||||
toggleFullscreen: 'Toggle Fullscreen',
|
toggleFullscreen: 'Toggle Fullscreen',
|
||||||
frameTitle: 'Player for {title}'
|
frameTitle: 'Player for {title}',
|
||||||
|
speedup: 'Speed x{speed}'
|
||||||
},
|
},
|
||||||
types: {
|
types: {
|
||||||
embed: ['youtube', 'vimeo', 'soundcloud'],
|
embed: ['youtube', 'vimeo', 'soundcloud'],
|
||||||
@ -172,7 +182,8 @@
|
|||||||
mute: null,
|
mute: null,
|
||||||
volume: null,
|
volume: null,
|
||||||
captions: null,
|
captions: null,
|
||||||
fullscreen: null
|
fullscreen: null,
|
||||||
|
speedup: null
|
||||||
},
|
},
|
||||||
// Events to watch on HTML5 media elements
|
// Events to watch on HTML5 media elements
|
||||||
events: ['ready', 'ended', 'progress', 'stalled', 'playing', 'waiting', 'canplay', 'canplaythrough', 'loadstart', 'loadeddata', 'loadedmetadata', 'timeupdate', 'volumechange', 'play', 'pause', 'error', 'seeking', 'emptied'],
|
events: ['ready', 'ended', 'progress', 'stalled', 'playing', 'waiting', 'canplay', 'canplaythrough', 'loadstart', 'loadeddata', 'loadedmetadata', 'timeupdate', 'volumechange', 'play', 'pause', 'error', 'seeking', 'emptied'],
|
||||||
@ -802,6 +813,16 @@
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Speed-up button
|
||||||
|
if (_inArray(config.controls, 'speed-up')) {
|
||||||
|
html.push(
|
||||||
|
'<button type="button" data-plyr="speed-up">',
|
||||||
|
'<svg><use xlink:href="' + iconPath + '-fast-forward" /></svg>',
|
||||||
|
'<span class="plyr__sr-only">' + config.i18n.speedup + '</span>',
|
||||||
|
'</button>'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
// Progress
|
// Progress
|
||||||
if (_inArray(config.controls, 'progress')) {
|
if (_inArray(config.controls, 'progress')) {
|
||||||
// Create progress
|
// Create progress
|
||||||
@ -1270,6 +1291,9 @@
|
|||||||
// Replace seek time instances
|
// Replace seek time instances
|
||||||
html = _replaceAll(html, '{seektime}', config.seekTime);
|
html = _replaceAll(html, '{seektime}', config.seekTime);
|
||||||
|
|
||||||
|
// Replace seek time instances
|
||||||
|
html = _replaceAll(html, '{speed}', config.currentSpeed);
|
||||||
|
|
||||||
// Replace all id references with random numbers
|
// Replace all id references with random numbers
|
||||||
html = _replaceAll(html, '{id}', Math.floor(Math.random() * (10000)));
|
html = _replaceAll(html, '{id}', Math.floor(Math.random() * (10000)));
|
||||||
|
|
||||||
@ -1316,6 +1340,7 @@
|
|||||||
plyr.buttons.rewind = _getElement(config.selectors.buttons.rewind);
|
plyr.buttons.rewind = _getElement(config.selectors.buttons.rewind);
|
||||||
plyr.buttons.forward = _getElement(config.selectors.buttons.forward);
|
plyr.buttons.forward = _getElement(config.selectors.buttons.forward);
|
||||||
plyr.buttons.fullscreen = _getElement(config.selectors.buttons.fullscreen);
|
plyr.buttons.fullscreen = _getElement(config.selectors.buttons.fullscreen);
|
||||||
|
plyr.buttons.speedup = _getElement(config.selectors.buttons.speedup);
|
||||||
|
|
||||||
// Inputs
|
// Inputs
|
||||||
plyr.buttons.mute = _getElement(config.selectors.buttons.mute);
|
plyr.buttons.mute = _getElement(config.selectors.buttons.mute);
|
||||||
@ -1956,6 +1981,35 @@
|
|||||||
_seek(plyr.media.currentTime + seekTime);
|
_seek(plyr.media.currentTime + seekTime);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Speed-up
|
||||||
|
function _speedup(speed) {
|
||||||
|
if (!_is.array(config.speeds)) {
|
||||||
|
_warn('Invalid speeds format');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (!_is.number(speed)) {
|
||||||
|
var index = config.speeds.indexOf(config.currentSpeed);
|
||||||
|
if (index !== -1) {
|
||||||
|
var nextIndex = index + 1;
|
||||||
|
if (nextIndex >= config.speeds.length) {
|
||||||
|
nextIndex = 0;
|
||||||
|
}
|
||||||
|
speed = config.speeds[nextIndex];
|
||||||
|
} else {
|
||||||
|
speed = config.defaultSpeed;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
config.currentSpeed = speed;
|
||||||
|
|
||||||
|
plyr.media.playbackRate = speed;
|
||||||
|
|
||||||
|
_updateSpeedupTooltip(speed);
|
||||||
|
|
||||||
|
// Save speed to localStorage
|
||||||
|
_updateStorage({speed: speed});
|
||||||
|
}
|
||||||
|
|
||||||
// Seek to time
|
// Seek to time
|
||||||
// The input parameter can be an event or a number
|
// The input parameter can be an event or a number
|
||||||
function _seek(input) {
|
function _seek(input) {
|
||||||
@ -2523,6 +2577,34 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Set playback speed
|
||||||
|
function _setSpeedup(speed) {
|
||||||
|
// Load speed from storage or default value
|
||||||
|
if (_is.undefined(speed)) {
|
||||||
|
speed = plyr.storage.speed || config.defaultSpeed;
|
||||||
|
}
|
||||||
|
|
||||||
|
_speedup(speed);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update hover tooltip for playback speed changed
|
||||||
|
function _updateSpeedupTooltip(speed) {
|
||||||
|
if (!isNaN(speed)) {
|
||||||
|
speed = config.currentSpeed;
|
||||||
|
}
|
||||||
|
|
||||||
|
var button = plyr.buttons.speedup;
|
||||||
|
var template = config.i18n.speedup;
|
||||||
|
|
||||||
|
var elements= button.getElementsByClassName(config.classes.tooltip);
|
||||||
|
if (elements.length === 0){
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
var tooltip = elements[0];
|
||||||
|
tooltip.innerHTML = _replaceAll(template, '{speed}', speed);
|
||||||
|
}
|
||||||
|
|
||||||
// Show the player controls in fullscreen mode
|
// Show the player controls in fullscreen mode
|
||||||
function _toggleControls(toggle) {
|
function _toggleControls(toggle) {
|
||||||
// Don't hide if config says not to, it's audio, or not ready or loading
|
// Don't hide if config says not to, it's audio, or not ready or loading
|
||||||
@ -2985,6 +3067,9 @@
|
|||||||
// Fast forward
|
// Fast forward
|
||||||
_proxyListener(plyr.buttons.forward, 'click', config.listeners.forward, _forward);
|
_proxyListener(plyr.buttons.forward, 'click', config.listeners.forward, _forward);
|
||||||
|
|
||||||
|
// Speed-up
|
||||||
|
_proxyListener(plyr.buttons.speedup, 'click', config.listeners.speedup, _speedup);
|
||||||
|
|
||||||
// Seek
|
// Seek
|
||||||
_proxyListener(plyr.buttons.seek, inputEvent, config.listeners.seek, _seek);
|
_proxyListener(plyr.buttons.seek, inputEvent, config.listeners.seek, _seek);
|
||||||
|
|
||||||
@ -3370,6 +3455,9 @@
|
|||||||
_setVolume();
|
_setVolume();
|
||||||
_updateVolume();
|
_updateVolume();
|
||||||
|
|
||||||
|
// Set playback speed
|
||||||
|
_setSpeedup();
|
||||||
|
|
||||||
// Reset time display
|
// Reset time display
|
||||||
_timeUpdate();
|
_timeUpdate();
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user