在javascript中替换HTML5视频src字符串不会替换视频

Replacing HTML5 video src strings in javascript does not replace video

本文关键字:替换 视频 字符串 src HTML5 javascript      更新时间:2023-09-26

我是一个javascript新手,正在构建一个简单的页面来练习使用HTML5视频。它基本上是一个完整的页面循环视频和一个跟踪和更新页面时间的javascript计时器。(当然,灵感来自无限醉酒的罗恩·斯旺森)。这是该页面的实时版本。小心:两个链接都播放声音。

我希望脚本从七个视频中选择一个在页面加载时播放。代码应选择一个介于1和7之间的随机整数,为"media/"目录中的相应文件生成字符串,并用新字符串替换视频<source>标记的src属性。

这一切似乎都正常工作:控制台中没有错误,无论是本地的还是上传到github页面的,当我检查Chrome开发者工具栏中的<video>元素时,我可以看到代码正确地替换了src字符串。但是页面似乎没有加载不同的视频!这是额外的加重,因为它在几次提交之前都是正确的。我回顾了历史,想看看发生了什么变化,但我什么都找不到。

我来自Python,我怀疑有什么我还不知道,但我不知道该怎么办!

UPDATE:借助这个问题,我已经解决了这个问题,使用createElementappendChild插入源标记,而不是更改querySelectorAll返回的NodeList中每个元素的属性。这是相关的新代码:

function add_source(element, src, type) {
    var source = document.createElement("source");
    source.src = src;
    source.type = type;
    element.appendChild(source);
    }
function main() {
    var video_no = random_int(1,7);
    var video_string_mpeg = "http://ecmendenhall.github.com/Infinite-Charleston/media/Trudy" + video_no + ".mp4";
    var video_string_webm = "http://ecmendenhall.github.com/Infinite-Charleston/media/Trudy" + video_no + ".webm";
    var videoplayer = document.querySelector(".videoplayer");
    add_source(videoplayer, video_string_mpeg, "video/mp4");
    add_source(videoplayer, video_string_mpeg, "video/webm");
    get_seconds();
    }

这很好,但我不完全理解为什么它有效。欢迎解释!

这是javascript:

start = new Date();
start_time = start.getTime();
function get_seconds() {
    var now = new Date();
    var time_now = now.getTime();
    var time_diff = time_now - start_time;
    var seconds = time_diff/1000;
    var timer_string = Math.round(seconds);
    document.getElementById("timer").innerHTML = timer_string;
    window.setTimeout("get_seconds();", 1000);
    }
function random_int(min, max) {
    return Math.floor(Math.random() * (max - min + 1)) + min; }

function main() {
    var video_no = random_int(1,7);
    var video_string_mpeg = "http://ecmendenhall.github.com/Infinite-Charleston/media/Trudy" + video_no + ".mp4";
    var video_string_webm = "http://ecmendenhall.github.com/Infinite-Charleston/media/Trudy" + video_no + ".webm";
    var sources = document.querySelectorAll(".videoplayer source");
    sources.item(0).src = video_string_mpeg;
    sources.item(1).src = video_string_webm;
    get_seconds();
    }

以及HTML:

<html>
<link rel="stylesheet" href="charleston.css">
<script src="charleston.js" type="text/javascript">
</script>
<body onload="main();">
<video class="videoplayer" loop autoplay>
<source src="http://ecmendenhall.github.com/Infinite-Charleston/media/Trudy1.mp4" type="video/mp4" />
<source src="http://ecmendenhall.github.com/Infinite-Charleston/media/Trudy1.webm" type="video/webm" />
Christ on a cracker, Trudy! Update your web browser!
<audio loop autoplay>
<source src="http://ecmendenhall.github.com/Infinite-Charleston/media/charleston.mp3" type="audio/mpeg" />
<source src="http://ecmendenhall.github.com/Infinite-Charleston/media/charleston.ogg" type="audio/ogg" />
</audio>
</video>
<div class="timer_box">
<p>Hell's bells, Trudy! You've been dancing for <a id="timer">0</a> seconds.
<a href="https://twitter.com/share" class="twitter-share-button" data-count="none">Tweet</a>
<script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0];if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src="//platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);}}(document,"script","twitter-wjs");</script></p>
</div>
</body>
</html>

我不认为CSS或媒体文件是问题所在,但如果你愿意,你可以在项目存储库中看到它们。此处提供实时版本。(注意:它播放声音)。正如你所看到的,除了选择不同的视频之外,它做的一切都很好。

我相信您也可以在视频对象上调用.load()来重新加载在相应视频对象中定义的<source>

"当您的监听器函数被触发时,它应该更改媒体的src属性,然后调用load方法来加载新媒体,调用play方法来播放它,如清单3-4所示。"-Apple