我可以使用 JavaScript 动态更改视频源吗?
如何使用 JS 更改视频来源?
<video id="myVideoTag" width="670" height="377" autoplay="true" controls="controls">
<source src="http://www.test.com/test.mp4" type='video/mp4; codecs="avc1.42E01E, mp4a.40.2"'>
</video>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
当然,
您可以在
source
元素上设置src
属性:或者使用 jQuery 而不是标准 DOM 方法:
然后您需要调用视频元素上的
load
方法:videoElement.load()
Sure,
You can set the
src
attribute on thesource
element:Or using jQuery instead of standard DOM methods:
Then you need to call the
load
method on the video element:videoElement.load()
我已经多次遇到这个问题,根据以前的经验和挖掘,我可以建议以下选项:
是的,只需重新插入
元素即可新来源。这是简单而有效的方法。不要忘记重新初始化事件侦听器。
video.src
这个我在 stackoverflow 上的答案以及 github 上的源代码中看到了很多。
它可以工作,但您无法提供可供选择的浏览器选项。
.load()
视频元素根据 。 asp" rel="noreferrer">文档 您可以更新
的
的
src
属性标签,然后调用.load()
以使更改生效:尽管如此,这里据说有在某些浏览器中存在问题。
我希望将所有这些都集中在一个地方会很有用。
I have faced this problem several times and based on previous experience and digging I can suggest these options:
yes, just re-insert
<video>
element with new sources. It's straightforward, but effective approach. Don't forget to re-initialize event listeners.video.src
this I saw a lot in answers here, on stackoverflow, and also in sources on github.
It works, but you cannot provide browser options to choose from.
.load()
on video elementaccording to documentation you can update
src
attributes for<video>
's<source>
tags and then call.load()
to changes take effect:Nevertheless here it's said that there're problems in some browsers.
I hope it will be useful to have this all in one place.
我遇到了同样的问题。根据此线程:
更改 html5 视频标签上的源
这是不可能的更改源标签的 src。您必须使用视频标签本身的 src 。
I have run into the same problem. According to this thread:
changing source on html5 video tag
it is not possible to change the src of the source tag. you will have to use src of the video tag itself.
这是有效的
,在 javascript 中
关键是重新加载视频播放器的 .load() 函数。
This is working
and in the javascript
The key is the .load() function that reloads the video player.
根据此规范说明。
所以假设你有:
你可以像这样修改媒体src:
You shouldn't try to change the
src
attribute of a source element, according to this spec note.So lets say you have:
You can modify the media src like so:
尽管坎贝尔提供了正确的解决方案,但针对当前的“播放列表特定”问题(如评论中所述)提供了更完整的解决方案此处。如果正确实施,此解决方案可以应用于所有视频格式(我使用了 modernizr 来检测哪个源将播放给定的浏览器,结合上面的解决方案)。
该解决方案适用于(并且已经过测试)在所有 HTML5 浏览器(包括 IE8)中更改 HTML5 视频标签中的视频。
Although Campbell has provided the correct solution, a more complete solution to the 'playlist specific' question at hand (as noted in the comments) is provided here. This solution can be applied to all video formats if implemented correctly (I have used modernizr in order to detect which source will play for a given browser, combined with the solution above).
This solution will work (and has been tested) for changing videos in HTML5 video tags in ALL HTML5 browsers, including IE8.
看看这个。此 JavaScript 更改源标记的 src。
https://jsfiddle.net/a94zcrtq/8/
Check this out. This javascript changes the src of the source tag.
https://jsfiddle.net/a94zcrtq/8/
我希望它能工作,请根据您的需要调整属性名称和 ID
下面的 html 代码 下面的
js 代码
i hope it should work and please adjust attribute name and id as per your need
html code below
js code below
我发现动态创建和附加子
source
元素就可以解决问题。I found that dynamically creating and appending the child
source
elements did the trick.由于其他解决方案对我不起作用,这就是我所做的:
不需要其他任何东西来让这个解决方案运行 - 除非我搞砸了......
Since the other solutions didn't work for me, here's what I did:
Nothing else is needed to get this solution running - unless I messed up somehow...