我真的很喜欢面向对象的 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();