2016-08-13 206 views
1

更新:我使用的是Fotorama插件,看起来底部菜单导致了问题。通过在视频标签周围放置div标签来禁用该菜单使得设置分辨率的功能起作用。感谢您的帮助和鼓励。对于底部菜单,我使用链接按钮创建一个简单的链接,链接到下一个视频的类似页面。使用JavaScript更改HTML 5.0视频上的视频分辨率

我已经编写了基于输入选项/选择元素来改变视频分辨率的JavaScript代码。有用。问题是,它停止工作,当我把完全相同的代码在函数内部(这样的代码可以被执行多次 - 每个选项/选择元素改变时)

Here is an image of the videoplayer and the option/select-element I have added

这里该选项/选择元素代码:

<li class="porfolionav"> 
    <select id="selectQuality" onchange="changeVidQualityFunction()"> 
     <option value="1080" selected="selected" disabled="disabled">Videokvalitet</option> 
     <option value="1080" id="1080">HD 1080</option> 
     <option value="480" id="480">SD 480</option> 
     <option value="320" id="320">SD 320</option> 
    </select> 
</li> 

下面是视频代码:

<div class="fotorama" data-width="1209px" data-height="680px" data-allowfullscreen="false" data-arrows="true" data-click="true" data-swipe="true" data-autoplay="false" data-transition="slide" data-clicktransition="slide" data-shuffle="false"> 
    <video id="video1V" height="680px" controls data-caption="320 OGG" poster="videos/img/thumb1.jpg"> 
    <source src="videos/test_q_320" id="video1">Your browser does not support HTML5 video. 
    </video> 
    <video id="video2V" height="680px" controls data-caption="480 OGG" poster="videos/img/thumb2.jpg"> 
    <source id="video2" src="videos/test_q_480.ogg" id="video2">Your browser does not support HTML5 video. 
    </video> 
    <video id="video3V" height="680px" controls data-caption="1080 OGG" poster="videos/img/thumb3.jpg"> 
    <source id="video3" src="videos/test_q_1080.ogg" id="video3">Your browser does not support HTML5 video. 
    </video> 
</div> 

这里是昌代码荷兰国际集团的分辨率(工程时,没有一个函数):

<script> 
    function changeVidQualityFunction(){ 

    $chosenVidQuality = document.getElementById("selectQuality").value; 
    $trulycompletevideolink = document.getElementById("video1").src; 
    $step1 = document.getElementById("video1").src.split("_q_"); 
    //COMMENT: step1[0] is the url from start and including the first part of the filename (not the "_q_"-part and the format) 
    $upToVidName = $step1[0]; 
    //COMMENT: step1[1] is the resolution and format, e.g. 320.ogg 
    $step2 = $step1[1].split("."); 
    //COMMENT: step2[0] is the resoltion e.g. 720 ,step2[1] is the format (without the dot in front of the format type) e.g. ogg 
    $vidresolution = $step2[0]; 
    $vidformat = $step2[1]; 

     $vidresolution = $chosenVidQuality; 
     $result = $upToVidName+"_q_"+$vidresolution+"."+$vidformat; 
     $('#video1').attr('src', $result); 
     $('#video1V').attr('data-caption', $vidresolution+" OGG"); 
     $('#video1V').load(); 

      window.alert("video1 attr src:"+document.getElementById("video1").src); //shows updated URL 

} 
      </script> 

感谢

+0

是否有错误或什么?你应该看看'$ result'是否实际上给你你想要的。 –

+0

从我可以看到的没有错误。 $结果给出正确的新URL,但视频不会更新,如果我右键点击它并选择“复制视频链接”,它会给我旧链接而不是新URL。 – Sebbe

回答

0

在您head标签的地方这<script src="jquery-1.12.2.js" charset="utf-8"></script>包括因为您使用的是从jQuery函数jQuery库。 ,并从该行

<select id="selectQuality" onchange="changeVidQualityFunction()"> 

将其更改为

<select id="selectQuality" name="video_selected"> 

和编辑你的脚本如下的jQuery的规则,进行适当的声明如下。我删除$变量,虽然这是有效的,在其声明与var

function changeVidQualityFunction() { 
     var ev = $('#selectQuality').val(); 
     console.log(ev); 
    var chosenVidQuality  = $('#selectQuality').val(); 
    var trulycompletevideolink = document.getElementById("video1").src; 
    var step1     = document.getElementById("video1").src.split("_q_"); 
    //COMMENT: step1[0] is the url from start and including the first part of the filename (not the "_q_"-part and the format) 
    var upToVidName   = step1[0]; 
    //COMMENT: step1[1] is the resolution and format, e.g. 320.ogg 
    var step2     = step1[1].split("."); 
    //COMMENT: step2[0] is the resoltion e.g. 720 ,step2[1] is the format (without the dot in front of the format type) e.g. ogg 
    var vidresolution = step2[0]; 
    var vidformat = step2[1]; 

     vidresolution = chosenVidQuality; 
     var result = upToVidName + "_q_" + vidresolution + "." + vidformat; 
     $('#video1').attr('src', result); 
     $('#video1V').attr('data-caption', vidresolution+" OGG"); 
     $('#video1V').load(); 

     window.alert("video1 attr src:"+document.getElementById("video1").src); //shows updated URL 

    } 

通知,以便JavaScript就知道他们是变量。

$(document).ready(function(){ 
     $('#selectQuality').change(function(){ 
      changeVidQualityFunction(); 
     }); 
    }); 

代码是做什么的,它会跟踪你的下拉变化,如果改变,函数将执行。

希望这将有助于

+1

不幸的是它没有工作,但无论如何感谢。我可以流式传输视频(时间轴向前移动,有声音),但视频图像是静态的,仍然是飞溅/缩略图图像(无视频),如果我复制viedolink,它仍然是旧网址,而不是用于所选视频的视频解析度。 – Sebbe

+0

那么这很好 – Fil

+0

我首先发布的代码是:如果我也触发与函数内部完全相同的代码,但当页面加载时,它将读取我现在已设置为480的options-element的标准输入。它会加载正确的分辨率(否则它会默认加载320),并且视频可以正常工作。当我然后通过选择例如1080所有代码似乎执行(我调试与window.alert(“”)),但没有发生与视频触发功能,它仍然可以播放和工作正常,但它仍然是480分辨率和没有选择1080。 – Sebbe

0

我用的是Fotorama插件,似乎底部菜单是造成问题。通过在视频标签周围放置div标签来禁用该菜单使得设置分辨率的功能起作用。感谢您的帮助和鼓励。对于底部菜单,我使用链接按钮创建一个简单的链接,链接到下一个视频的类似页面。