William
William

Reputation: 4588

In javascript what is the video equivalent of "new Audio( )"

In Javascript you can access the HTML-5 audio object like this:

var audio = new Audio('nameOfFile.mp3');

But the equivalent syntax for the video element doesn't seem to work (I'm on Chrome).

var video = new Video('nameOfFile.ogg');

I'm curious if there is an equivalent object for the video tag that I can access via this simple new syntax.

Upvotes: 8

Views: 14737

Answers (2)

Arif Billah
Arif Billah

Reputation: 196

Just in case you need, I have one, quite similar to the new Audio() thing...

function Video(src, append) {
  var v = document.createElement("video");
  if (src != "") {
    v.src = src;
  }
  if (append == true) {
    document.body.appendChild(v);
  }
  return v;
}

You can then use it like this:

var video = new Video("YOUR_VIDEO_URL", true);
// do whatever you want...
video.height = 280;
video.width = 500;
video.controls = "controls";
video.play();

Upvotes: 6

user149341
user149341

Reputation:

There isn't one yet, but you can create one the long way around:

var video = document.createElement("video");
video.setAttribute("src", "nameOfFile.ogg");

Upvotes: 12

Related Questions