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
fuchsia_web / webengine / test / data / play_audio.html [blame]
<html>
<body>
<audio></audio>
<script>
var audio = document.querySelector('audio'),
mediaSource = new MediaSource();
audio.src = URL.createObjectURL(mediaSource);
audio.onended = function() { document.title = 'ended'; }
audio.onerror = function() { document.title = 'media element error'; }
// Play two files with different sample rate to force mid-stream
// re-initialization.
var files = ["bear-44.1kHz.webm", "bear-48kHz.webm"];
mediaSource.addEventListener('sourceopen', function() {
var index = 0;
var sourceBuffer = mediaSource.addSourceBuffer('audio/webm;codecs="vorbis"');
sourceBuffer.addEventListener('updateend', function (_) {
if (++index == files.length) {
mediaSource.endOfStream();
return;
}
loadFile(sourceBuffer, index);
});
loadFile(sourceBuffer, index);
});
function loadFile(sourceBuffer, index) {
var xhr = new XMLHttpRequest;
xhr.open('GET', './' + files[index]);
xhr.responseType = 'arraybuffer';
xhr.onload = function() {
if (this.status != 200) {
console.error('Failed to fetch the file ' + files[index]);
return;
}
sourceBuffer.timestampOffset = index;
sourceBuffer.appendBuffer(this.response);
};
xhr.send();
}
audio.play();
</script>
</body>
</html>