jyyhq/witDisplay/js/video/child/flvStart.js

171 lines
5.4 KiB
JavaScript
Raw Normal View History

2025-02-10 12:18:11 +08:00
let item = localStorage.getItem("item");
let type = localStorage.getItem("type");
let layer;
let loadingMsg;
$(function () {
layui.use('layer', function () {
layer = layui.layer;
//加载层-风格3
loadingMsg = layer.msg('视频加载中,请稍候...', {icon: 16, scrollbar: false, time: 0, shade: [0.01, '#393D49']});
getCSWebSocket();
getQXConnect();
setTimeout("play(JSON.parse(item))", 2000)
});
})
function getCSWebSocket() {
$.ajax({
url: dataUrl + "video/getCSWebSocket?token=" + token,
type: "POST",
data: {name: loginName, passwd: password, token: token},
success: function (data) {
if (data.resMsg === 'success') {
var jsons = JSON.parse(data.obj);
webSocket = jsons.content.Addr;
if (webSocket.indexOf("127.0.0.1") != -1) {
webSocket = webSocket.replace("127.0.0.1", "112.30.98.105");
}
}
}, error: function (error) {
}
})
}
function getQXConnect() {
let params = {
"address": _cf.connParams.address,
"port": _cf.connParams.port,
"user": _cf.connParams.user,
"password": _cf.connParams.password,
"epid": _cf.connParams.epid,
"fixaddr": _cf.connParams.bfix
}
requestPost('login', params, function (rv) {
let result = {
errcode: -1,
token: ''
}
let respJSON = JSON.parse(rv.responseText);
if (respJSON.hasOwnProperty('errcode')) {
result.errcode = respJSON.errcode;
}
if (respJSON.hasOwnProperty('token')) {
result.token = respJSON.token;
result.errcode = 0;
tokens = result.token;
}
});
}
function play(item) {
let win = new H5Live.Window("video", {
callback: function (action, instance, data) {
if (action.action === 'stream.start') {
layer.close(loadingMsg);
}
if (action.action === 'ws.close') {
layer.msg("视频播放失败", {icon: 2, time: 2000});
}
}
});
if (item.manufactorId === "1") {
CSPlayVideo(win, item);
} else {
QXPlayVideo(item);
}
}
/* h5
* @param win
* @param puid
* @param idx
* @param webSocket
*/
function CSPlayVideo(win, item) {
let options;
console.log(type);
if (type === "0") {
options = {
wsPath: webSocket,
puid: "155165612271245609",
type: "platform",
beginTime: timeStrToTimestamp(item.begin),
endTime: timeStrToTimestamp(item.end),
speed: "0",
offsetTime: "0",
fileName: item.path + item.name
}
} else {
options = {
wsPath: webSocket,
puid: item.puid,
type: "CEFS",
idx: "0",
beginTime: 20220722000000,
endTime: 20220722000230,
speed: "0",
diskLetter: "A:"
}
}
console.log(options);
win.OpenVod(options);
}
/**
* 时间字符串转时间戳
* @param str
*/
function timeStrToTimestamp(str) {
let date = new Date(str);
return date.getTime();
}
/**
* 清新播放视频
* @param puId
* @param idx
*/
function QXPlayVideo(item) {
//播视频接口
let url;
let durationSecond = timeStrToTimestamp(item.end) - timeStrToTimestamp(item.begin)
if (type === '0') {
url = _cf.q2http_url + "CSS/VODFile.flv?id=" + item.id + "&path=" + "" + item.path + item.name + "" + "&idx=0&puid=" + item.puid + "&durationSecond=" + durationSecond + "&resType=IV&start=0&stream=0&token=" + tokens;
}else {
url = _cf.q2http_url + "SG/VODFile.flv?path=" + "" + item.path + item.name + "" + "&idx=0&puid=" + item.puid + "&durationSecond=" + durationSecond + "&startTime=0&stream=0&token=" + tokens;
}
console.log(url);
if (flvjs.isSupported()) {
let videoElement = document.getElementById('video_video');
if (videoElement) {
let flvPlayer = flvjs.createPlayer({
type: 'flv', // => 媒体类型 flv 或 mp4m3u8
isLive: true, // => 是否为直播流
hasAudio: false, // => 是否开启声音
url: url, // => 视频流地址
stashInitialSize: 128 // 减少首桢显示等待时长
}, {
enableWorker: false, //不启用分离线程
enableStashBuffer: false, //关闭IO隐藏缓冲区
reuseRedirectedURL: true, //重用301/302重定向url用于随后的请求如查找、重新连接等。
autoCleanupSourceBuffer: true, //自动清除缓存
fixAudioTimestampGap: false//false才会音视频同步
});
flvPlayer.attachMediaElement(videoElement);
flvPlayer.load();
flvPlayer.play();
flvPlayer.on(flvjs.Events.ERROR, (errorType, errorDetail, errorInfo) => {
if (flvPlayer) {
layer.msg("视频播放失败", {icon: 2, time: 2000});
}
});
flvPlayer.on(flvjs.Events.SCRIPTDATA_ARRIVED, (errorType, errorDetail, errorInfo) => {
if (flvPlayer) {
layer.close(loadingMsg);
}
});
}
}
}