螢石云視頻接入
本示例可用于實際接入螢石云開放平臺視頻,同時支持音頻輸入和輸出。
實際優化內容
1.動態獲取token
2.切換各公司和車間時,自動重新初始化播放器
let EZUIKit = null; // 第三方庫引用
let EZUIKitPlayers = []; // 播放器實例數組
let gss = ''; // 全局保存當前選擇的 gss 值
let accessToken = ''; // 存儲動態獲取的 accessTokenconst APP_KEY = '----------------'; // 替換為實際 appKey
const APP_SECRET = '-----------'; // 替換為實際 appSecret// 頁面加載時引入 ezuikit.js 并初始化播放器
Page.onLoad = function () {System.import('/gemcoderAppResource?appuuid=11ce5afa919d4289bdb6e71912c172b0&path=/files/ezuikit.js&resourcesId=1280590&version=0').then(res => {EZUIKit = res.default;return fetchAccessToken(); // 先獲取 token}).then(token => {accessToken = token;initPlayers(gss); // 使用當前 gss 初始化播放器}).catch(err => {console.error('初始化失敗:', err);});
};// 下拉選擇變化時更新 gss 并重新初始化播放器
Page.下拉選擇OnChange = function () {gss = gmcom.gs.value;console.log('選中的 gss:', gss);if (EZUIKit && accessToken) {initPlayers(gss);} else {console.warn('EZUIKit 或 accessToken 尚未準備好');}
};// 獲取 accessToken
function fetchAccessToken() {const url = 'https://open.ys7.com/api/lapp/token/get';
return new Promise((resolve, reject) => {fetch(url, {method: 'POST',headers: {'Content-Type': 'application/x-www-form-urlencoded'},body: new URLSearchParams({appKey: APP_KEY,appSecret: APP_SECRET})}).then(response => response.json()).then(data => {if (data.code === '200') {resolve(data.data.accessToken);} else {reject(new Error(`獲取 token 失敗: ${data.msg}`));}}).catch(error => {reject(error);});
});
}
// 根據 gss 獲取攝像頭配置(不再包含 accessToken)
function getPlayerConfigs(gss) {const commonUrls = ['ezopen://open.ys7.com/-----1.hd.live','ezopen://open.ys7.com/---/1.hd.live','ezopen://open.ys7.com/-----/1.hd.live','ezopen://open.ys7.com/-------/1.hd.live'];
let urls = [];switch (gss) {case '0102':urls = ['ezopen://open.ys7.com/-------/1.hd.live','ezopen://open.ys7.com/------/1.hd.live','ezopen://open.ys7.com/------/1.hd.live'];break;case '0103':urls = ['ezopen://open.ys7.com/--------1.hd.live','ezopen://open.ys7.com/-----/1.hd.live','ezopen://open.ys7.com/------/1.hd.live','ezopen://open.ys7.com/--------/1.hd.live'];break;case '0110':urls = ['ezopen://open.ys7.com/-------/1.hd.live','ezopen://open.ys7.com/--------/1.hd.live','ezopen://open.ys7.com/--------/1.hd.live','ezopen://open.ys7.com/---------/1.hd.live'];break;default:urls = [...commonUrls];
}const baseIds = ['yt-dtView-10374446859161','yt-dtView-10474436971042','yt-dtView-17744698706218','yt-dtView-10744075992380'
];return urls.map((url, index) => ({id: baseIds[index],url: url
}));
}//
清理播放器資源
function clearPlayers() {if (EZUIKitPlayers.length > 0) {EZUIKitPlayers.forEach(player => {if (player.stop) player.stop();});EZUIKitPlayers = [];}
}// 初始化播放器
function initPlayers(gss) {clearPlayers();
const playerConfigs = getPlayerConfigs(gss);playerConfigs.forEach(config => {const player = new EZUIKit.EZUIKitPlayer({id: config.id,accessToken: accessToken, // 使用動態獲取的 tokenurl: config.url,autoplay: false,audio: 0,handleSuccess: () => {// 成功回調},handleError: (e) => {if (typeof doEvent === 'function') {doEvent('onError', e);} else {console.error('doEvent 未定義:', e);}}});EZUIKitPlayers.push(player);
});
}
// 播放按鈕
Page.按鈕OnClick = function () {EZUIKitPlayers.forEach(player => player.play());
};// 暫停按鈕
Page.按鈕11OnClick = function () {EZUIKitPlayers.forEach(player => player.stop());
};// 切換地址按鈕
Page.按鈕111OnClick = function () {const newUrls = ['ezopen://open.ys7.com/-----/1.live','ezopen://open.ys7.com/------/1.live','ezopen://open.ys7.com/-----/1.live','ezopen://open.ys7.com/------/1.live'
];EZUIKitPlayers.forEach((player, index) => {if (index < newUrls.length) {player.stop().then(() => {player.play({ url: newUrls[index], accessToken: accessToken });});}
});
}