我非常喜欢面向对象的Javascript和Video.js。为了使自定义的视频控件在每次加载页面时出现,缓存的Video.js对象必须被处理掉,然后重新构建。
我创建了下面的脚本,它可以正确地检查缓存对象,但不知道是否有更好的方法,一个不需要创建第二个全局变量的方法。
<div class="video-wrapper"></div>
<script>
var VideoObj;
var Continuum = function(container, el, shortName) {
this.container = container;
this.el = el;
this.shortName = shortName;
};
Continuum.prototype = {
/* Build up the <video> tag and and attributes */
prepareVideo: function() {
if(VideoObj) {
VideoObj.dispose();
}
$(this.container).append($(this.el));
$(this.shortName).attr({
'id': 'video_1',
'class': 'video-js vjs-default-skin',
'controls': true,
'poster': 'http://video-js.zencoder.com/oceans-clip.jpg',
'preload': false
});
this.createVideoObject('video_1');
},
/* Instantiate the object with the Video.js call */
createVideoObject: function(vidID) {
_V_(vidID, {}, function () {
this.vidID = VideoObj = videojs(vidID);
this.vidID.src([
{ type: "video/mp4", src: "http://video-js.zencoder.com/oceans-clip.mp4" },
{ type: "video/webm", src: "http://video-js.zencoder.com/oceans-clip.webm" },
{ type: "video/ogg", src: "http://video-js.zencoder.com/oceans-clip.ogv" }
]);
this.vidID.pause();
});
}
};
var vid = new Continuum('div.video-wrapper', '<video/>', 'video');
vid.prepareVideo();
你可以很容易地检查Videojs是否以这种方式加载。
videojs.getAllPlayers().length
如果它是0,你可以重建它。
另一种方法是迭代
videojs.getAllPlayers()
并在重建前对其进行处置。