最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

html - In javascript what is the video equivalent of "new Audio( )" - Stack Overflow

programmeradmin2浏览0评论

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.

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.

Share Improve this question edited Mar 8, 2013 at 3:53 999k 6,5652 gold badges31 silver badges32 bronze badges asked Mar 8, 2013 at 3:52 WilliamWilliam 4,58818 gold badges66 silver badges116 bronze badges 0
Add a comment  | 

2 Answers 2

Reset to default 12

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

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

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();
发布评论

评论列表(0)

  1. 暂无评论