VideoJS-动态更改源

Aar*_*ain 5 javascript streaming http-live-streaming video.js

我希望动态更改 VideoJS 播放器的视频源。我尝试了一种直接通过 DOM 更改源的方法,它确实更改了它,但播放器需要重新加载。所以看看这里的官方 API:http : //docs.videojs.com/docs/api/player.html#Methodssrc

有一种方法可以更改源,但是在运行以下代码时,它会引发错误。

    var source = dropdown.options[dropdown.selectedIndex].value;

	var myVideo = videojs.getPlayers();
	console.log(myVideo);
	if (source == "Source2"){
		myVideo.src([
			{type: "application/x-mpegURL", src: "http://mycdn/playlist.m3u8"},
			{type: "rtmp/mp4", src: "rtmp://mycdn"}
		]);
	}
Run Code Online (Sandbox Code Playgroud)

尽管控制台确实验证了 myVideo 是一个对象,但调用 .src(source) 函数会抛出“TypeError: myVideo.src is not a function. (In 'myVideo.src', 'myVideo.src' is undefined)”

我还发现了这样的教程,其中明显更“官方”的方法是完全处理玩家并使用新来源重新初始化,但我似乎无法理解他在做什么。https://ineed.coffee/3201/how-to-dynamically-change-video-js-videos-and-captions-with-javascript/

任何帮助,将不胜感激。


解决方案:出于测试目的,我只有一个漂亮的小下拉菜单,并为其添加了一个点击事件,因此它将频道更改为用户想要的任何内容。

var dropdown = document.getElementById('sel1');
var source = dropdown.options[dropdown.selectedIndex].value;

dropdown.addEventListener("click", function(){
source = dropdown.options[dropdown.selectedIndex].value;
console.log(source)

var myVideo = videojs('my-video');
console.log(myVideo);   
if (source == "Public Access"){
    myVideo.src([
        {type: "application/x-mpegURL", src: "http://mycdns/playlist.m3u8"},
        {type: "rtmp/mp4", src: "rtmp://mycdn"}
    ]);
}
else if (source == "Government"){
    myVideo.src([
        {type: "application/x-mpegURL", src: "http://mycdn/playlist.m3u8"},
        {type: "rtmp/mp4", src: "rtmp://mycdn"}
    ]);
}
else if (source == "Regional"){
    myVideo.src([
        {type: "application/x-mpegURL", src: "http://mycdn/playlist.m3u8"},
        {type: "rtmp/mp4", src: "rtmp://mysource"}
    ]);
}
});
Run Code Online (Sandbox Code Playgroud)

mis*_*ben 1

getPlayers()返回一个包含所有玩家的对象,而不是一个玩家。

通常你会得到一个特定的球员videojs('my_player_id')