1

I tried to handle video data from api with javascript, the api response is ok but the player doesn't work as expected and when I play the video the console throw an error.

DOMException: The element has no supported sources.

Although video data is available the array buffer seem to be not pushed into media source yet

Here is the code

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>

<body>
    <h1>Hello</h1>
    <video id="video" controls autoplay></video>
</body>

</html>

<script>
    window.onload = function() {
        var mime = 'video/mp4; codecs="avc1.42E01E, mp4a.40.2"';
        const videoTag = document.getElementById("video");

        if (
            "MediaSource" in window &&
            MediaSource.isTypeSupported(mime)
        ) {
            var mediaSource = new MediaSource();
            //console.log(mediaSource.readyState);
            videoTag.src = URL.createObjectURL(mediaSource);
            mediaSource.addEventListener("sourceopen", sourceOpen);
        } else {
            console.error("Unsupported MIME type or codec: ", mime);
        }

        function sourceOpen(e) {
            var msource = this;
            console.log(msource.readyState);
            // URL.revokeObjectURL(vidElement.src);
            var sourceBuffer = msource.addSourceBuffer(mime);
            var videoUrl = "https://vjs.zencdn.net/v/oceans.mp4";
            fetch(videoUrl)
                .then(function(response) {
                    return response.arrayBuffer();
                })
                .then(function(arrayBuffer) {
                    console.log("object", arrayBuffer.byteLength);
                    sourceBuffer.addEventListener("updateend", function(_) {
                        console.log(msource.readyState);
                        if (!sourceBuffer.updating &&
                            mediaSource.readyState === "open"
                        ) {
                            msource.endOfStream();
                        }
                        videoTag.play();
                    });
                    sourceBuffer.appendBuffer(arrayBuffer);
                })
                .catch(e => console.log(e));
        }
    };
</script>
Community
  • 1
  • 1

0 Answers0