javascript 使用 jquery 更改源后重新加载 video.js 播放器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31663305/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 14:09:24  来源:igfitidea点击:

Reloading video.js player after changing source using jquery

javascriptjqueryhtmlvideo.js

提问by BeBe

I manage to make a script where I can change the video source without reloading the whole page, but the problem is that after the new source is loaded, the player is not and I only get a black box.

我设法制作了一个脚本,我可以在不重新加载整个页面的情况下更改视频源,但问题是在加载新源后,播放器不是,我只得到一个黑框。

HTML:

HTML:

<link href="//vjs.zencdn.net/4.12/video-js.css" rel="stylesheet">
<script src="//vjs.zencdn.net/4.12/video.js"></script>
<video id="example_video_1" class="video-js vjs-default-skin vjs-big-play-centered" controls autoplay preload="auto" width="850" height="400" data-setup='{"example_option":true}'>
  <source id="videoMP4" src="http://video-js.zencoder.com/oceans-clip.mp4" type='video/mp4' />
</video>
<div class="menu1">menu1</div>
<div class="menu2">menu2</div>
<div class="menu3">menu3</div>
<div class="menu4">menu4</div>

JavaScript:

JavaScript:

$(".menu1").click(function() {
  document.getElementById("example_video_1").innerHTML = "<source src=\"http:\/\/video-js.zencoder.com\/oceans-clip.mp4\" type='video\/mp4' \/>";
});
$(".menu2").click(function() {
  document.getElementById("example_video_1").innerHTML = "<source src=\"http:\/\/techslides.com\/demos\/sample-videos\/small.mp4\" type='video\/mp4' \/>";
});
$(".menu3").click(function() {
  document.getElementById("example_video_1").innerHTML = "<source src=\"http:\/\/video-js.zencoder.com\/oceans-clip.mp4\" type='video\/mp4' \/>";
});
$(".menu4").click(function() {
  document.getElementById("example_video_1").innerHTML = "<source src=\"http:\/\/techslides.com\/demos\/sample-videos\/small.mp4\" type='video\/mp4' \/>";
});

Full code and example: http://codepen.io/BeBeINC/pen/PqydVM

完整代码和示例:http: //codepen.io/BeBeINC/pen/PqydVM

回答by TheOnlyError

Use the .load()function. You should pause it to prevent the audio from keeping on playing.

使用该.load()功能。您应该暂停它以防止音频继续播放。

var video = document.getElementById('example_video');
var source = document.getElementById('videoMP4');
$("ELEMENT").click(function() {
    video.pause()
    source.setAttribute('src', 'NEW MP4');
    video.load();
    video.play();
});

回答by cmlndz

You should change the video's source using the player's API, specifically the src()method:

您应该使用播放器的 API 更改视频的来源,特别是src()方法:

yourPlayer.src("http://foo.com/bar.mp4");

回答by MSaudi

You can do it like that

你可以这样做

var player = videojs(document.querySelector('.video-js'));
  player.src({
                 src: 'videoURL,
                  type: 'video/mp4'/*video type*/
            });

  player.play();