forked from burg/timelapse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
media-continues-playing-after-replace-source.html
111 lines (98 loc) · 4.21 KB
/
media-continues-playing-after-replace-source.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
<html>
<title>Test media source replacement</title>
<body>
<p>Test that media keeps playing when the source element is replaced.</p>
<script src=video-test.js></script>
<script src=media-file.js></script>
<script src=../resources/gc.js></script>
<script>
var timeupdateEventCount = 0;
var skippedCount = 0;
var sourceReplaced = false;
function swapAudio() {
v = document.getElementsByTagName('audio')[0];
v.removeChild(v.childNodes[0]);
var s = document.createElement('source');
s.src = findMediaFile("audio", "content/test");
v.appendChild(s);
}
function errorListener(event)
{
logResult(false, "Caught 'error' event, audio.error.code = " + this.error.code);
endTest();
}
function canplaythroughListener(event)
{
consoleWrite("EVENT(canplaythrough)");
testElement = this;
testExpected("testElement.currentTime", 0);
this.play();
}
function playingListener(event)
{
consoleWrite("EVENT(playing)");
}
function timeupdateListener(event)
{
++timeupdateEventCount;
if (timeupdateEventCount-skippedCount == 1) {
// First time update after source replacement should be 0.
// We allow one late time update to come in from the previous
// source element. This was done to help the cr-linux test
// pass, and does not necessarily indicate a problem.
if (sourceReplaced) {
if (skippedCount >= 1 || this.currentTime == 0) {
testElement = this;
testExpected("testElement.currentTime", 0);
} else {
// The time is not 0 as expected. Hope this is a
// late update from the previous source.
++skippedCount;
}
}
} else if (timeupdateEventCount-skippedCount >= 2) {
// We wait 2 timeupdate events so we are sure the media engine
// is playing the media, and make sure time is advancing.
testElement = this;
testExpected("testElement.currentTime", 0, '>');
if (!sourceReplaced) {
consoleWrite("Replacing the media's source element:");
sourceReplaced = true;
timeupdateEventCount = 0;
skippedCount = 0;
// The ports are not consistent in regards to whether
// the canplaythrough and playing events are triggered
// a second time, so stop listening for them. This was
// done to help the cr-linux test pass, and does not
// necessarily indicate a problem.
this.removeEventListener('playing', playingListener);
this.removeEventListener('canplaythrough', canplaythroughListener);
swapAudio();
} else {
this.removeEventListener('timeupdate', timeupdateListener);
this.pause();
endTest();
}
}
}
function testAudioElement(count)
{
timeupdateEventCount = 0;
skippedCount = 0;
var audioElement = document.getElementsByTagName('audio')[count];
//audioElement.removeChild(audioElement.childNodes[0]);
audioElement.addEventListener('error', errorListener);
audioElement.addEventListener('canplaythrough', canplaythroughListener);
audioElement.addEventListener('timeupdate', timeupdateListener);
audioElement.addEventListener('playing', playingListener);
var s = document.createElement('source');
s.src = findMediaFile("audio", "content/silence");
audioElement.appendChild(s);
}
document.write("<audio controls></audio>");
testAudioElement(0);
gc();
gc();
</script>
</body>
</html>