Skip to content

Commit 4118c79

Browse files
authored
Merge pull request #262 from sebbayer/bugfix/speed-keyboard-controls
Fix speed plugin always controlling the first player on the page via …
2 parents 42dc804 + 0625164 commit 4118c79

File tree

3 files changed

+66
-6
lines changed

3 files changed

+66
-6
lines changed

demo/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ <h1><img src="https://cloud.githubusercontent.com/assets/910829/22357262/e6cf32b
7878
<li><a href="postroll.html">Postroll</a></li>
7979
<li><a href="preview.html">Preview</a></li>
8080
<li><a href="quality.html">Quality</a></li>
81+
<li><a href="speed.html">Speed</a></li>
8182
<li><a href="vrview.html">VRView</a></li>
8283
</ul>
8384
</main>

demo/speed.html

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta http-equiv="X-UA-Compatible" content="IE=edge">
6+
<title>MediaElement.js 3.0 - Speed Plugin</title>
7+
<meta name="viewport" content="width=device-width, initial-scale=1">
8+
<link rel="shortcut icon" href="favicon.ico" type="image/x-icon">
9+
<link rel="icon" href="favicon.ico" type="image/x-icon">
10+
11+
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/5.0.0/normalize.min.css">
12+
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/mediaelement/5.0.1/mediaelementplayer.css">
13+
<link rel="stylesheet" href="../dist/speed/speed.css">
14+
<link rel="stylesheet" href="demo.css">
15+
</head>
16+
<body>
17+
<div id="container">
18+
19+
<h1>Speed Plugin</h1>
20+
<p><a href="index.html">Back to Main</a></p>
21+
22+
<p>This plugin allows the generation of a menu with different video/audio speed.</p>
23+
24+
<h2>Video Player</h2>
25+
26+
<div class="media-wrapper">
27+
<h3>Video Player 1</h3>
28+
<video id="player1" width="750" height="421" controls preload="none" poster="http://mediaelementjs.com/images/big_buck_bunny.jpg">
29+
<source type="video/mp4" src="http://distribution.bbb3d.renderfarming.net/video/mp4/bbb_sunflower_native_60fps_normal.mp4">
30+
</video>
31+
<h3>Video Player 2</h3>
32+
<video id="player2" width="750" height="421" controls preload="none" poster="http://mediaelementjs.com/images/big_buck_bunny.jpg">
33+
<source type="video/mp4" src="http://distribution.bbb3d.renderfarming.net/video/mp4/bbb_sunflower_native_60fps_normal.mp4">
34+
</video>
35+
</div>
36+
</div>
37+
38+
<script src="https://cdnjs.cloudflare.com/ajax/libs/mediaelement/5.0.1/mediaelement-and-player.min.js"></script>
39+
<script src="../dist/speed/speed.js"></script>
40+
<script>
41+
var mediaElements = document.querySelectorAll('video, audio');
42+
43+
for (var i = 0, total = mediaElements.length; i < total; i++) {
44+
new MediaElementPlayer(mediaElements[i], {
45+
features: ['playpause', 'current', 'progress', 'duration', 'speed'],
46+
});
47+
}
48+
</script>
49+
</body>
50+
</html>

src/speed/speed.js

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,13 @@ Object.assign(MediaElementPlayer.prototype, {
141141
labels = player.speedButton.querySelectorAll(`.${t.options.classPrefix}speed-selector-label`)
142142
;
143143

144+
/**
145+
* Store a reference to the radio buttons to prevent a scope bug in keyboard events
146+
* when multiple MediaElement players are on the same page. Otherwise these keyboard
147+
* events would always control the first speed button instance on the page.
148+
*/
149+
player.speedRadioButtons = radios;
150+
144151
// hover or keyboard focus
145152
for (let i = 0, total = inEvents.length; i < total; i++) {
146153
player.speedButton.addEventListener(inEvents[i], () => {
@@ -202,9 +209,10 @@ Object.assign(MediaElementPlayer.prototype, {
202209
if (event.key != '<')
203210
return;
204211

205-
for (let i = 0; i < radios.length - 1; i++) {
206-
if (radios[i].checked) {
207-
const nextRadio = radios[i+1];
212+
const _radios = player.speedRadioButtons;
213+
for (let i = 0; i < _radios.length - 1; i++) {
214+
if (_radios[i].checked) {
215+
const nextRadio = _radios[i+1];
208216
nextRadio.dispatchEvent(mejs.Utils.createEvent('click', nextRadio));
209217
break;
210218
}
@@ -216,9 +224,10 @@ Object.assign(MediaElementPlayer.prototype, {
216224
if (event.key != '>')
217225
return;
218226

219-
for (let i = 1; i < radios.length; i++) {
220-
if (radios[i].checked) {
221-
const prevRadio = radios[i-1];
227+
const _radios = player.speedRadioButtons;
228+
for (let i = 1; i < _radios.length; i++) {
229+
if (_radios[i].checked) {
230+
const prevRadio = _radios[i-1];
222231
prevRadio.dispatchEvent(mejs.Utils.createEvent('click', prevRadio));
223232
break;
224233
}

0 commit comments

Comments
 (0)