Skip to content

Commit bd1d115

Browse files
committed
change to the way I'm handling the race condition
1 parent 9b7843a commit bd1d115

File tree

1 file changed

+20
-19
lines changed

1 file changed

+20
-19
lines changed

demos/video.html

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
<title>Video</title>
22
<article>
3-
<p id="altmsg">It doesn't appear that your browser supports native video, sorry :(</p>
3+
<video>
4+
<source src="assets/dizzy.mp4" type="video/mp4" />
5+
<source src="assets/dizzy.webm" type="video/webm" />
6+
<source src="assets/dizzy.ogv" type="video/ogv" />
7+
</video>
48
<p id="controls">
59
<input type="button" id="play" value="play">
610
<span id="position">00:00</span> / <span id="duration">loading...</span>
@@ -24,30 +28,26 @@
2428
Another alternative is to put inline event handlers in the markup, again
2529
this is something I prefer not to do.
2630
31+
One final alternative is to create the meida element and bind the event
32+
before dropping it in the DOM.
33+
34+
Instead of all of these workarounds, I'm going to test the readyState
35+
of the media element. If readyState is > 0, then it means the metadata
36+
has loaded, and therefore I'll have to run the event handler manually.
2737
*/
2838

2939

30-
var container = document.getElementById('altmsg'),
31-
video = document.createElement('video'),
32-
source, // support will be detected
40+
var video = document.getElementsByTagName('video')[0],
3341
togglePlay = document.getElementById('play'),
3442
position = document.getElementById('position'),
3543
using = document.getElementById('using'),
3644
ready = false,
3745
controls = document.getElementById('controls'),
3846
fullscreen = null;
3947

40-
if (video.canPlayType('video/webm')) {
41-
source = 'assets/dizzy.webm';
42-
} else if (video.canPlayType('video/mp4')) {
43-
source = 'assets/dizzy.mp4';
44-
} else if (video.canPlayType('video/ogv')) {
45-
source = 'assets/dizzy.ogv';
46-
}
47-
4848
addEvent(togglePlay, 'click', function () {
4949
if (ready) {
50-
video.playbackRate = 0.5;
50+
// video.playbackRate = 0.5;
5151
if (video.paused) {
5252
if (video.ended) video.currentTime = 0;
5353
video.play();
@@ -68,7 +68,7 @@
6868
});
6969

7070
// this used to be canplay, but really it should have been loadedmetadata - sorry folks
71-
addEvent(video, 'loadedmetadata', function () {
71+
function loadedmetadata() {
7272
video.muted = true;
7373
ready = true;
7474
document.querySelector('#duration').innerHTML = asTime(this.duration);
@@ -83,14 +83,15 @@
8383
video.webkitEnterFullScreen();
8484
});
8585
}
86-
});
86+
}
8787

88-
if (source) {
89-
video.src = source;
90-
console.log(video);
91-
container.parentNode.replaceChild(video, container);
88+
if (video.readyState > 0) { // metadata is loaded already - fire the event handler manually
89+
loadedmetadata.call(video);
90+
} else {
91+
addEvent(video, 'loadedmetadata', loadedmetadata);
9292
}
9393

94+
9495
function asTime(t) {
9596
t = Math.round(t);
9697
var s = t % 60;

0 commit comments

Comments
 (0)