|
1 | 1 | <title>Video</title>
|
2 | 2 | <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> |
4 | 8 | <p id="controls">
|
5 | 9 | <input type="button" id="play" value="play">
|
6 | 10 | <span id="position">00:00</span> / <span id="duration">loading...</span>
|
|
24 | 28 | Another alternative is to put inline event handlers in the markup, again
|
25 | 29 | this is something I prefer not to do.
|
26 | 30 |
|
| 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. |
27 | 37 | */
|
28 | 38 |
|
29 | 39 |
|
30 |
| -var container = document.getElementById('altmsg'), |
31 |
| - video = document.createElement('video'), |
32 |
| - source, // support will be detected |
| 40 | +var video = document.getElementsByTagName('video')[0], |
33 | 41 | togglePlay = document.getElementById('play'),
|
34 | 42 | position = document.getElementById('position'),
|
35 | 43 | using = document.getElementById('using'),
|
36 | 44 | ready = false,
|
37 | 45 | controls = document.getElementById('controls'),
|
38 | 46 | fullscreen = null;
|
39 | 47 |
|
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 |
| - |
48 | 48 | addEvent(togglePlay, 'click', function () {
|
49 | 49 | if (ready) {
|
50 |
| - video.playbackRate = 0.5; |
| 50 | + // video.playbackRate = 0.5; |
51 | 51 | if (video.paused) {
|
52 | 52 | if (video.ended) video.currentTime = 0;
|
53 | 53 | video.play();
|
|
68 | 68 | });
|
69 | 69 |
|
70 | 70 | // this used to be canplay, but really it should have been loadedmetadata - sorry folks
|
71 |
| -addEvent(video, 'loadedmetadata', function () { |
| 71 | +function loadedmetadata() { |
72 | 72 | video.muted = true;
|
73 | 73 | ready = true;
|
74 | 74 | document.querySelector('#duration').innerHTML = asTime(this.duration);
|
|
83 | 83 | video.webkitEnterFullScreen();
|
84 | 84 | });
|
85 | 85 | }
|
86 |
| -}); |
| 86 | +} |
87 | 87 |
|
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); |
92 | 92 | }
|
93 | 93 |
|
| 94 | + |
94 | 95 | function asTime(t) {
|
95 | 96 | t = Math.round(t);
|
96 | 97 | var s = t % 60;
|
|
0 commit comments