Html HTML5 视频播放器:动态加载视频
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3602525/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
HTML5 video player: dynamically loading videos
提问by Benjamin Allison
So, using a HTML 5 compliant video player, (like Video JS) how would one go about loading a video dynamically, without having to reload the entire page? Imagine, a list of links (something like a playlist), and each link points to a video. When clicking the link, I want to load the selected video into player.
那么,使用符合 HTML 5 标准的视频播放器(如 Video JS)如何动态加载视频,而不必重新加载整个页面?想象一下,一个链接列表(类似于播放列表),每个链接都指向一个视频。单击链接时,我想将所选视频加载到播放器中。
Currently, I'm using an Iframe that holds the video player, so basically a I pass a variable on to the Iframe, and reload it. I don't think this is ideal, for a few reasons; it doesn't allow the video to go full screen, the Back button moves the Iframe back not just the main page, plus, it's an Iframe. I'd rather avoid this.
目前,我正在使用一个包含视频播放器的 iframe,所以基本上我将一个变量传递给 iframe,然后重新加载它。我认为这并不理想,原因有几个;它不允许视频全屏显示,后退按钮不仅将 Iframe 移回主页,而且它是一个 Iframe。我宁愿避免这种情况。
Ideas? Thanks!
想法?谢谢!
回答by Benjamin Allison
Came up with a simple solution. Here's the script; throw this in the head:
想出了一个简单的解决方案。这是脚本;把这个扔在头上:
function vidSwap(vidURL) {
var myVideo = document.getElementsByTagName('video')[0];
myVideo.src = vidURL;
myVideo.load();
myVideo.play();
}
And then the HREF will call the function:
然后 HREF 将调用该函数:
<a href="#" onClick="javascript:vidSwap('myMovie.m4v'); return false;">Link</a>