扩展
要让video.js支持播放MediaStream,只需要给videojs的默认tech html5添加一个SourceHandler
function videojsSupportMediaStream() {
var Html5Tech = videojs.getTech("html5");
Html5Tech.registerSourceHandler({
canPlayType: function () {
return "probably";
},
canHandleSource: function (source, tech, option) {
return source.srcObject ? "probably" : "";
},
handleSource: (source, tech, option) => {
if (source.srcObject) {
tech.el().srcObject = source.srcObject;
return {
dispose: () => {},
};
}
},
});
}
使用
然后通过调用src()函数进行播放,因为video.js内部会检查src,src只能为非空字符串所以只能设置一个无用src和type应付检查:
videojsSupportMediaStream();
var vjsPlayer = videojs(...);
vjsPlayer.src({
srcObject: mediaStream,
src: 'video/x-src',
type: 'video/x-src',
})