2

I have a video named video.mp4 I want to stream it in a video tag in HTML, I tried too many answers from other questions and none of them worked.

var URL = this.window.URL || this.window.webkitURL;
var file = new Blob(["./video.mp4"], {"type": "video\/mp4"});
var value = URL.createObjectURL(file);

console.log(value);
video.src = value;

When I tried the code above it printed a blob, but when I want to play the video it doesn't work and shows this error in the console:

Uncaught (in promise) DOMException: The element has no supported sources.

1
  • You are not running the same code as in the duplicate, BTW. You are missing the video.load() step at the end. Commented Sep 25, 2022 at 16:32

2 Answers 2

1

You could do it like this:

var vid= document.createElement("video");
var vidsrc= document.createElement("source");
vidsrc.setAttribute('src','video.mp4');
vidsrc.setAttribute('type','video/mp4');
vid.appendChild(vidsrc);
document.body.appendChild(vid);

This code simply creates a new video element with a source element and adds it to the document body.

Sign up to request clarification or add additional context in comments.

2 Comments

Your answer doesn't make the src as a blob
@Navspace The question seems to imply that he wants it as a <video> - "I want to stream it in a video tag in HTML"
1

new Blob cannot be used to directly read a file. You may need something like instantiating a XMLHttpRequest. Also look at <video> element documentation: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/video. You need to specify <source> elements with type attribute.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.