kiftd/webContext/quickview/kplayer.js

137 lines
3.2 KiB
JavaScript
Raw Normal View History

2019-04-12 10:00:58 +00:00
/**
* Kplayer播放器内置功能
*/
var tReq;
var tTimer;
2019-07-20 08:32:26 +00:00
var pingInt;
2019-04-12 10:00:58 +00:00
$(function() {
2020-05-07 07:02:29 +00:00
window.onresize = function() {
2019-04-12 10:00:58 +00:00
showCloseBtn();
2020-05-07 07:02:29 +00:00
}
pingInt = setInterval("ping()", 60000);
2019-04-12 10:00:58 +00:00
var fileId = getFileId();
$
.ajax({
url : 'homeController/playVideo.ajax',
type : 'POST',
dataType : 'text',
data : {
fileId : fileId
},
success : function(result) {
if (result != "ERROR") {
f = eval("(" + result + ")");
2020-05-18 00:09:41 +00:00
$("#vname").text(f.fileName);
2019-04-12 10:00:58 +00:00
$("#vcreator").text(f.fileCreator);
$("#vcdate").text(f.fileCreationDate);
$("#vsize").text(f.fileSize);
if (f.needEncode == "N") {
playVideo();
} else {
$("#playerMassage")
.html(
"<h2>播放器正在努力解码中...</h2><h3>已完成:<span id='transcodeProgress'>0</span>%</h3><p class='text-muted'>提示:该视频需解码后播放,请耐心等待!</p>");
doTranscode();
}
} else {
alert("错误:无法定位要预览的文件或该操作未被授权。");
reMainPage();
}
},
error : function() {
alert("错误:请求失败,请刷新重试。");
reMainPage();
}
});
});
// 获取URL上的视频id参数它必须是第一个参数。
function getFileId() {
var url = location.search;
if (url.indexOf("?") != -1) {
var str = url.substr(1);
strs = str.split("=");
return strs[1];
}
return "";
}
2019-07-20 08:32:26 +00:00
// 显示视频信息并播放视频
2019-04-12 10:00:58 +00:00
function playVideo() {
$("#playerbox")
.html(
"<video id='kiftplayer' class='video-js col-md-12' controls preload='auto' height='500'>"
2020-05-07 07:02:29 +00:00
+ "<source src='resourceController/getResource/"
2019-04-12 10:00:58 +00:00
+ f.fileId + "' type='video/mp4'></video>");
2020-05-07 07:02:29 +00:00
var player = videojs('kiftplayer', {
2019-08-28 11:23:22 +00:00
preload : 'auto'
});
2019-04-12 10:00:58 +00:00
player.ready(function() {
this.play();
});
}
// 关闭当前窗口并释放播放器
function reMainPage() {
2020-05-07 07:02:29 +00:00
if (tReq != null) {
2019-04-12 10:00:58 +00:00
tReq.abort()
}
2020-05-07 07:02:29 +00:00
if (tTimer != null) {
2019-04-12 10:00:58 +00:00
window.clearTimeout(tTimer);
}
window.opener = null;
window.open('', '_self');
window.close();
}
// 进行转码请求并监听进度状态(轮询)
function doTranscode() {
2020-05-07 07:02:29 +00:00
tReq = $.ajax({
2019-04-12 10:00:58 +00:00
url : 'resourceController/getVideoTranscodeStatus.ajax',
type : 'POST',
dataType : 'text',
data : {
fileId : f.fileId
},
success : function(result) {
if (result == "FIN") {
playVideo();
} else if (result == "ERROR") {
alert("错误:请求失败,请刷新重试。");
reMainPage();
} else {
$("#transcodeProgress").text(result);
2020-05-07 07:02:29 +00:00
tTimer = setTimeout('doTranscode()', 500);// 每隔1秒询问一次进度
2019-04-12 10:00:58 +00:00
}
},
error : function() {
alert("错误:请求失败,请刷新重试。");
reMainPage();
}
});
}
2020-05-07 07:02:29 +00:00
function showCloseBtn() {
2019-04-12 10:00:58 +00:00
var win = $(window).width();
2020-05-07 07:02:29 +00:00
if (win < 450) {
$("#closeBtn").addClass("hidden");
} else {
$("#closeBtn").removeClass("hidden");
}
2019-07-20 08:32:26 +00:00
}
2020-05-07 07:02:29 +00:00
// 防止播放视频时会话超时的应答器,每分钟应答一次
function ping() {
2019-07-20 08:32:26 +00:00
$.ajax({
2020-05-07 07:02:29 +00:00
url : "homeController/ping.ajax",
type : "POST",
dataType : "text",
data : {},
success : function(result) {
if (result != 'pong') {
2019-07-20 08:32:26 +00:00
window.clearInterval(pingInt);
}
},
2020-05-07 07:02:29 +00:00
error : function() {
2019-07-20 08:32:26 +00:00
window.clearInterval(pingInt);
}
});
2019-04-12 10:00:58 +00:00
}