JSFiddle - React, Tailwind, and code Playground
HTML
<html>
<body>
<video id="video" muted controls autoplay></video>
</body>
</html>
JavaScript
function playVideo(video) {
var NUM_CHUNKS = 20;
var sourceBuffer;
var segments = [];
var mimeCodec = 'video/mp4; codecs="avc1.4d401f,mp4a.40.2"';
var mediaSource = new MediaSource();
video.src = URL.createObjectURL(mediaSource);
video.play();
function addBuffer() {
if (!segments.length) return console.error("segments empty");
if (sourceBuffer.updating) return console.error("buffer updating");
var segment = segments.shift();
console.info("appendBuffer", segments.length, mediaSource.duration);
sourceBuffer.appendBuffer(segment);
}
mediaSource.addEventListener('sourceopen', () => {
sourceBuffer = mediaSource.addSourceBuffer(mimeCodec);
//sourceBuffer.mode = 'segments';
sourceBuffer.addEventListener('error', (e) => {
console.error("sourceBuffer error", segments);
});
var xhr = new XMLHttpRequest();
xhr.responseType = 'arraybuffer';
sourceBuffer.onupdateend = () => {
console.info("onupdateend");
//addBuffer();
};
$(video).on("waiting abort canplay canplaythrough durationchange emptied encrypted ended error interruptbegin interruptend loadeddata loadedmetadata loadstart mozaudioavailable pause play playing progress ratechange seeked seeking stalled suspend timeupdate volumechange", (e) => {
var vbuf = video.buffered;
var vstart = vbuf && vbuf.length > 0 ? vbuf.start(0) : "";
var vend = vbuf && vbuf.length > 0 ? vbuf.end(0) : "";
var videoDelta = vend ? (vend - video.currentTime).toFixed(2) : "-";
var videoLen = vend ? (vend - vstart).toFixed(2) : "-";
var quality = video.getVideoPlaybackQuality();
let warn = ["timeupdate", "progress"].indexOf(e.type) === -1;
console[!warn ? "log" : "error"].call(console, "VIDEO EVENT", e.type,
`currentTime: ${video.currentTime}, videoDelta: ${videoDelta}, videoBuffLen: ${videoLen}, mediaSource: ${mediaSource.duration}`,
...