根据屏幕分辨率加载不同的视频(html5)质量

时间:2015-08-12 00:42:12

标签: javascript html5 video screen resolution

我试图根据用户的屏幕高度分辨率显示不同的视频分辨率,我有这个代码但我不是javascript的专家。如果res低于等于360px高度,则应该更改src属性,依此类推。 有什么建议吗?

<script type="text/rocketscript">
var v = new Array();
v[0] = ["https://dl.dropboxusercontent.com/u/51626189/nli-vids/intro/intro-360_x264.mp4"];

v[1] = ["https://dl.dropboxusercontent.com/u/51626189/nli-vids/intro/intro-480_x264.mp4"];

v[2] = ["https://dl.dropboxusercontent.com/u/51626189/nli-vids/intro/intro-720_x264.mp4"];

v[3] = ["https://dl.dropboxusercontent.com/u/51626189/nli-vids/intro/intro-1080_x264.mp4"];
function changeVid(n){
    var video = document.getElementById('video');
if (screen.height<=360) {video.setAttribute("src", v[n][0]);}
else if (screen.height<=480) {video.setAttribute("src", v[n][1]);}
else if (screen.height<=720) {video.setAttribute("src", v[n][2]);}
else if (screen.height<=768) {video.setAttribute("src", v[n][2]);}
else if (screen.height<=1080) {video.setAttribute("src", v[n][3]);}
else if (screen.width>=1920) {video.setAttribute("src", v[n][3]);}
video.load();}
</script>
<video id="video" autoplay preload src="#">
</video>

1 个答案:

答案 0 :(得分:3)

我不知道&#34; text / rocketscript&#34; (与Wordpress和/或CloudFlare有关吗?),但这个Javascript有效:

<script type="text/javascript">
var v = new Array();
v[0] = "https://dl.dropboxusercontent.com/u/51626189/nli-vids/intro/intro-360_x264.mp4";
v[1] = "https://dl.dropboxusercontent.com/u/51626189/nli-vids/intro/intro-480_x264.mp4";
v[2] = "https://dl.dropboxusercontent.com/u/51626189/nli-vids/intro/intro-720_x264.mp4";
v[3] = "https://dl.dropboxusercontent.com/u/51626189/nli-vids/intro/intro-1080_x264.mp4";

function changeVid() {
    var video = document.getElementById('video');
    if (screen.height <= 360) video.setAttribute("src", v[0]);
    else if (screen.height <= 480) video.setAttribute("src", v[1]);
    else if (screen.height <= 768) video.setAttribute("src", v[2]);
    else video.setAttribute("src", v[3]);

    video.load();
}

changeVid();
</script>

很抱歉,我无法将其作为片段工作,JS Fiddle在此反而:http://jsfiddle.net/3g59yomb/

请注意,它会加载一个高分辨率的视频(在我的系统上使用2560 x 1440的屏幕),即使我的浏览器窗口要小得多,而且JS Fiddle框架仍然更小。那可能是也可能不是错误!但它确实会加载视频。