-
Notifications
You must be signed in to change notification settings - Fork 5
/
script.js
46 lines (41 loc) · 1.61 KB
/
script.js
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
try {
if (!'MediaSource' in window)
throw new ReferenceError('There is no MediaSource property in window object.');
var mime = 'audio/mpeg';
if (!MediaSource.isTypeSupported(mime)) {
alert('Can not play the media. Media of MIME type ' + mime + ' is not supported.');
throw ('Media of type ' + mime + ' is not supported.');
}
var audio = document.querySelector('audio'),
mediaSource = new MediaSource();
audio.src = URL.createObjectURL(mediaSource);
mediaSource.addEventListener('sourceopen', function() {
var sourceBuffer = this.addSourceBuffer(mime);
sourceBuffer.appendWindowEnd = 4.0;
var xhr = new XMLHttpRequest;
xhr.open('GET', 'https://raw.githubusercontent.com/rpsthecoder/mediasourceapi/master/sample.mp3');
// xhr.open('GET', 'sample.mp3'); /* while testing in localhost */
xhr.responseType = 'arraybuffer';
xhr.onload = function() {
try {
switch (this.status) {
case 200:
sourceBuffer.appendBuffer(this.response);
sourceBuffer.addEventListener('updateend', function (_) {
mediaSource.endOfStream();
});
break;
case 404:
throw 'File Not Found';
default:
throw 'Failed to fetch the file';
}
} catch (e) {
console.error(e);
}
};
xhr.send();
});
} catch (e) {
console.error(e);
}