216 lines
6.7 KiB
JavaScript
216 lines
6.7 KiB
JavaScript
(function () {
|
||
/**
|
||
* =====================================================
|
||
* DPlayer Initializer (Scoped by dom_prefix)
|
||
* -----------------------------------------------------
|
||
* - 不使用全局变量
|
||
* - 支持一个页面多个播放器
|
||
* - 优先初始化 data-engine="dplayer" 的模块,兼容旧模板里的 #dplayer
|
||
* =====================================================
|
||
*/
|
||
|
||
function ensurePrefix(root) {
|
||
if (!root || root.dataset.prefix) {
|
||
return;
|
||
}
|
||
|
||
var cls = root.className || '';
|
||
var m = cls.match(/([a-z0-9]{4,8})-player/);
|
||
if (m) {
|
||
root.dataset.prefix = m[1];
|
||
}
|
||
}
|
||
|
||
function resolveContainer(root) {
|
||
if (root) {
|
||
if (root.matches && (root.matches('.dplayer') || root.id === 'dplayer')) {
|
||
return root;
|
||
}
|
||
if (root.querySelector) {
|
||
var scoped = root.querySelector('.dplayer, [id="dplayer"]');
|
||
if (scoped) {
|
||
return scoped;
|
||
}
|
||
}
|
||
}
|
||
|
||
return document.getElementById('dplayer') || document.querySelector('.dplayer');
|
||
}
|
||
|
||
function initDPlayer(root, strPlayUrl) {
|
||
var DomDPlayer = resolveContainer(root);
|
||
if (!DomDPlayer) {
|
||
showError(root, '播放器容器不存在');
|
||
return null;
|
||
}
|
||
if (!strPlayUrl) {
|
||
showError(root, '播放地址不存在');
|
||
return null;
|
||
}
|
||
if (typeof DPlayer === "undefined") {
|
||
showError(root, '播放器脚本加载失败');
|
||
return null;
|
||
}
|
||
if (DomDPlayer.dataset.dplayerReady === '1') {
|
||
return null;
|
||
}
|
||
DomDPlayer.dataset.dplayerReady = '1';
|
||
|
||
// ---- 创建 DPlayer 实例 ----
|
||
try {
|
||
var videoPlayer = new DPlayer({
|
||
container: DomDPlayer,
|
||
autoplay: true,
|
||
screenshot: false,
|
||
//logo: dplayerLogo,
|
||
video: {
|
||
url: strPlayUrl,
|
||
type: 'hls',
|
||
},
|
||
pluginOptions: {
|
||
hls: {
|
||
maxBufferLength: 600,
|
||
},
|
||
},
|
||
});
|
||
|
||
window.videoPlayer = videoPlayer;
|
||
var playResult = videoPlayer.play()
|
||
if (playResult && playResult.catch) {
|
||
playResult.catch(function () {});
|
||
}
|
||
|
||
// ---- 错误处理(兜底)----
|
||
videoPlayer.on('error', function () {
|
||
showError(root, '播放失败,请尝试切换线路');
|
||
});
|
||
|
||
return videoPlayer;
|
||
|
||
} catch (e) {
|
||
console.warn(e);
|
||
DomDPlayer.dataset.dplayerReady = '0';
|
||
showError(root, '播放器初始化失败');
|
||
return null;
|
||
}
|
||
}
|
||
|
||
function showError(root, msg) {
|
||
if (!root || !root.querySelector) {
|
||
console.warn(msg);
|
||
return;
|
||
}
|
||
|
||
ensurePrefix(root);
|
||
var errClass = root.dataset.prefix ? root.dataset.prefix + '-player-status' : 'player-status';
|
||
var err = root.querySelector('.' + errClass);
|
||
if (!err) {
|
||
err = document.createElement('div');
|
||
err.className = errClass + ' is-error';
|
||
root.appendChild(err);
|
||
}
|
||
err.innerText = msg;
|
||
}
|
||
|
||
function boot() {
|
||
var players = document.querySelectorAll('[data-engine="dplayer"]');
|
||
if (!players.length) return;
|
||
|
||
players.forEach(function (root) {
|
||
ensurePrefix(root);
|
||
});
|
||
}
|
||
|
||
function resolveRoot() {
|
||
var root = document.querySelector('[data-engine="dplayer"]');
|
||
if (root) {
|
||
return root;
|
||
}
|
||
|
||
var container = document.getElementById('dplayer') || document.querySelector('.dplayer');
|
||
if (!container) {
|
||
return null;
|
||
}
|
||
|
||
return container.closest ? (container.closest('[data-engine="dplayer"]') || container) : container;
|
||
}
|
||
|
||
// 获取第一个线路 key(带容错)
|
||
function getDefaultLine() {
|
||
if (typeof arrPlayUrl === "undefined" || !arrPlayUrl || typeof arrPlayUrl !== "object") {
|
||
console.warn("arrPlayUrl 无效:");
|
||
return null;
|
||
}
|
||
|
||
const keys = Object.keys(arrPlayUrl);
|
||
if (keys.length === 0) {
|
||
console.warn("没有任何播放线路");
|
||
return null;
|
||
}
|
||
|
||
return keys[0]; // 后端排序,第一位为默认
|
||
}
|
||
|
||
// 获取某个线路的第一个 url
|
||
function getFirstPlayUrl(playLine) {
|
||
if (!arrPlayUrl || !arrPlayUrl[playLine] || !arrPlayUrl[playLine][0]) {
|
||
console.warn("播放线路不存在:", playLine, arrPlayUrl);
|
||
return null;
|
||
}
|
||
return arrPlayUrl[playLine][0].url || null;
|
||
}
|
||
|
||
function getIndexedPlayUrl(playLine, playIndex) {
|
||
if (!arrPlayUrl || !arrPlayUrl[playLine]) {
|
||
console.warn("播放线路不存在:", playLine, arrPlayUrl);
|
||
return null;
|
||
}
|
||
|
||
var intIndex = Math.max(1, parseInt(playIndex, 10) || 1);
|
||
var activeUrl = arrPlayUrl[playLine][intIndex - 1] || arrPlayUrl[playLine][0];
|
||
return activeUrl ? (activeUrl.url || null) : null;
|
||
}
|
||
|
||
// DOM Ready
|
||
document.addEventListener("DOMContentLoaded", function () {
|
||
try {
|
||
if (typeof strVideoId == "undefined") return;
|
||
var root = resolveRoot();
|
||
if (!root) return;
|
||
|
||
boot();
|
||
ensurePrefix(root);
|
||
|
||
if (typeof strPlayType !== "undefined" && boolIsPlayPage) {
|
||
|
||
// 用户指定线路
|
||
if (strPlayType !== "default") {
|
||
strPlayUrl = getIndexedPlayUrl(strPlayType, intPlayUrlIndex);
|
||
if (!strPlayUrl) {
|
||
const defaultLine = getDefaultLine();
|
||
strPlayUrl = getFirstPlayUrl(defaultLine);
|
||
}
|
||
// checkLine(strPlayType);
|
||
|
||
// 默认播放第一个线路的第一个 URL
|
||
} else {
|
||
const defaultLine = getDefaultLine();
|
||
strPlayUrl = getFirstPlayUrl(defaultLine);
|
||
// checkLine(defaultLine);
|
||
}
|
||
initDPlayer(root, strPlayUrl);
|
||
|
||
} else if (strPlayType == "default" && !boolIsPlayPage) {
|
||
|
||
const defaultLine = getDefaultLine();
|
||
strPlayUrl = getFirstPlayUrl(defaultLine);
|
||
// checkLine(defaultLine);
|
||
|
||
initDPlayer(root, strPlayUrl);
|
||
}
|
||
} catch (error) {
|
||
console.warn(error);
|
||
}
|
||
})
|
||
})();
|