简体   繁体   English

如何在点击时更改html5视频源?

[英]How to change html5 video source on click?

What I'm trying to do is create a sort of playlist, using just html5 and vanilla javascript (no jquery). 我想做的是创建一个播放列表,仅使用html5和普通javascript(无jquery)。 The same short video keeps looping until you click one of the arrows, then the next (or previous) video in line gets loaded in the same video element. 相同的短视频一直循环播放,直到您单击箭头之一,然后行中的下一个(或上一个)视频才加载到同一视频元素中。

Here's what I have: 这是我所拥有的:

HTML 的HTML

<video autoplay="true" loop="true" id="video">
    <source src="img-vid/video1.mp4" type="video/mp4" id="vid-src">
</video>
<img src="img-vid/Arrow_left.png" class="arrow arrow-left" id="left">
<img src="img-vid/Arrow_right.png" class="arrow arrow-right" id="right">

Javascript Java脚本

var button_next = document.getElementById("right");
var button_previous = document.getElementById("left");
var playlist = document.getElementById("video");
var source = document.getElementById("vid-src");

button_next.onclick = play_next;
button_previous.onclick = play_prev;

function play_next() {
    var filename = '';
    if (source.src == "img-vid/video1.mp4"){
        filename = "video2";
    }
    else if (source.src == "img-vid/video2.mp4"){
        filename = "video3";
    }
    else if (source.src == "img-vid/video3.mp4"){
        filename = "video4";
    }
    else {
        filename = "video1";
    }
    source.src = "img-vid/" + filename + ".mp4";
    playlist.load();
    playlist.play();
}

//same for function play_prev(), but order of videos is reversed

However, with this code, clicking either arrow doesn't seem to do anything. 但是,使用此代码,单击任一箭头似乎没有任何作用。 I'm not sure what I'm doing wrong though. 我不确定自己在做什么错。 My first thought was that the if-tests were failing, so it just re-loaded the same video, but even after removing the tests and setting filename to be "video2", it didn't do anything. 我首先想到的是if-tests失败了,所以它只是重新加载了相同的视频,但是即使在删除测试并将文件名设置为“ video2”之后,它也没有执行任何操作。

Edit 1: Changed scope on var filename, like Dev-One suggested. 编辑1:更改了var文件名的作用域,如建议的Dev-One。 Video source still doesn't change though. 视频源仍然没有改变。

Edit 2: changing the following: 编辑2:更改以下内容:

button_next.onclick = play_next;
button_previous.onclick = play_prev;

to

button_next.addEventListener("click", play_next);
button_previous.addEventListener("click", play_prev);

seems to help. 似乎有帮助。 When clicking on the arrows now, the video changes, but not to the correct one. 现在单击箭头时,视频会更改,但不会更改为正确的视频。 Pressing the arrow-right always brings up video1, while pressing arrow-left always shows video3. 按向右箭头始终会显示video1,而按向左箭头始终会显示video3。 These are the videos in the last else-statement for each method, so there still seems to be an issue with my if-tests. 这些是每种方法的最后一个else语句中的视频,因此我的if测试似乎仍然存在问题。

Edit 3: Everything is now working as planned! 编辑3:现在一切都按计划进行! I also had to change 我也不得不改变

source.src == ...

to

source.getAttribute("src") == ...

in my if-tests. 在我的if测试中。 source.src always returned the full url ( http://..../img-vid/video1.mp4 ), not the relative one (img-vid/video1.mp4), so every if-test failed. source.src始终返回完整URL( http://..../img-vid/video1.mp4 ),而不是相对URL(img-vid / video1.mp4),因此每个if-test都会失败。

如我所做的编辑所述,我设法通过将button.onclick更改为button.addEventListener()并将source.src更改为source.getAttribute(“ src”)来解决了我的问题。

From observing the code, one mistake i can find is var filename is used out of scope. 通过观察代码,我可以发现的一个错误是var filename超出范围。

Change it to: 更改为:

function play_next() {

    var filename = '';

    if (source.src == "img-vid/video1.mp4"){
        filename = "video2";
    }
    else if (source.src == "img-vid/video2.mp4"){
        filename = "video3";
    }
    else if (source.src == "img-vid/video3.mp4"){
        filename = "video4";
    }
    else {
        filename = "video1";
    }
    source.src = "img-vid/" + filename + ".mp4";
    playlist.load();
    playlist.play();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM