Video.js改变源,但不显示新的源

Video.js changing source, but does not show new source

本文关键字:显示 js 改变 Video      更新时间:2023-09-26

很抱歉我对这个话题缺乏了解

我有一个脚本,改变视频播放器的来源。它就是这么做的。唯一的问题是Video.js播放器播放分配给它的第一个源。

document.getElementById("vid-player_html5_api").innerHTML = "";
document.getElementById("vid-player_html5_api").innerHTML = "<source src='" + link + "' type='video/mp4'>";
document.getElementById("vid-player_html5_api").muted = false;

如果有两个按钮,你点击按钮1它会改变播放器的源并显示正确的视频。然后假设你点击了按钮2它会改变播放器的源,但它仍然会显示与按钮1

相同的视频

这是证明,它改变了源代码,我检查了Chrome开发工具,肯定足够它改变了源代码

我如何解决这个问题?

刚刚发现这篇文章,在VideoJS当前的模式是设置。src与JSON对象,例如

player.src({ type: 'video/mp4', src: new_url });

你可以尝试如下,

function playVideo(videoSource, type) {
  var videoElm = document.getElementById('testVideo');
  var videoSourceElm = document.getElementById('testVideoSource'); 
   if (!videoElm.paused) {
        videoElm.pause();
     }
    
   videoSourceElm.src = videoSource;
   videoSourceElm.type = type;
  
    videoElm.load();
    videoElm.play();
  }
<video id="testVideo" width="400" controls>
  <source id="testVideoSource">
</video>
<br/>
<input type="button" value="Play Video 1" onclick="playVideo('http://www.w3schools.com/html/mov_bbb.mp4', 'video/mp4')" />
<input type="button" value="Play Video 2" onclick="playVideo('http://www.w3schools.com/html/mov_bbb.ogg', 'video/ogg')" />

对我来说唯一有效的方法就是这样

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

你需要在加载完新视频后再次调用video.js:

var current_vid = document.getElementById("vid-player_html5_api")
var next_vid = document.createElement('video');
next_vid.innerHTML = '<source src="' + link + '" type="video/mp4">';
next_vid.muted = false;
current_vid.parentNode.replaceChild( next_vid, current_vid );
videojs(current_vid, {}, function(){
  // do something when video.js is ready
});

Video.js:动态加载视频

中返回这个
 return() => {
        if(player){
            player.dispose()
        }
    }