Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<video controls="" autoplay="" width="320" height="240"></video>
<script>
var video = document.querySelector('video');
video.addEventListener('error', onVideoError);
window.MediaSource = window.MediaSource || window.WebKitMediaSource;
var mediaSource = new MediaSource();
video.src = window.URL.createObjectURL(mediaSource);
mediaSource.addEventListener('sourceopen', function(){
window.video.pause();
window.mediaSource.mode = 'sequence'
window.sourceBuffer = window.mediaSource.addSourceBuffer('audio/webm; codecs="opus"');
fileDownload('0.webm', function(ar) {
var appendTime = 0
sourceBuffer.timestampOffset = appendTime - 648/48000;
sourceBuffer.appendWindowEnd = appendTime + 5;
sourceBuffer.appendWindowStart = appendTime;
window.sourceBuffer.appendBuffer(new Uint8Array(ar));
fileDownload('1.webm', function(ar) {
var appendTime = 5//window.sourceBuffer.buffered.end(0);
sourceBuffer.timestampOffset = appendTime - 648/48000;
sourceBuffer.appendWindowEnd = appendTime + 5;
sourceBuffer.appendWindowStart = appendTime;
window.sourceBuffer.appendBuffer(new Uint8Array(ar));
fileDownload('2.webm', function(ar) {
var appendTime = 10//window.sourceBuffer.buffered.end(0);
sourceBuffer.timestampOffset = appendTime - 648/48000;
sourceBuffer.appendWindowEnd = appendTime + 5;
sourceBuffer.appendWindowStart = appendTime;
window.sourceBuffer.appendBuffer(new Uint8Array(ar));
window.video.play();
});
});
});
});
function onVideoError() {
var error = video.error
switch (error.code) {
case error.MEDIA_ERR_DECODE:
console.error(`[MediaLoader] Error: Failed to decode media.`)
break
case error.MEDIA_ERR_SRC_NOT_SUPPORTED:
console.error(`[MediaLoader] Error: Source is not supported.`)
break
case error.MEDIA_ERR_NETWORK:
console.error(`[MediaLoader] Error: Network error.`)
break
case error.MEDIA_ERR_ABORTED:
console.error(`[MediaLoader] Error: Aborted.`)
break
}
}
function fileDownload(url, cb) {
var xhr = new XMLHttpRequest();
xhr.open('GET', url, true);
xhr.responseType = 'arraybuffer';
xhr.send();
xhr.onload = function(e) {
cb(xhr.response);
};
};
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.