mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-07-26 17:13:54 +08:00
43 lines
1.9 KiB
JavaScript
43 lines
1.9 KiB
JavaScript
import { log } from './utils/logger.js';
|
||||
|
|
import { updateScriptStatus } from './document.js'
|
|||
|
|
|
|||
|
|
|
|||
|
|
// 检查Opus库是否已加载
|
|||
|
|
export function checkOpusLoaded() {
|
|||
|
|
try {
|
|||
|
|
// 检查Module是否存在(本地库导出的全局变量)
|
|||
|
|
if (typeof Module === 'undefined') {
|
|||
|
|
throw new Error('Opus库未加载,Module对象不存在');
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 尝试先使用Module.instance(libopus.js最后一行导出方式)
|
|||
|
|
if (typeof Module.instance !== 'undefined' && typeof Module.instance._opus_decoder_get_size === 'function') {
|
|||
|
|
// 使用Module.instance对象替换全局Module对象
|
|||
|
|
window.ModuleInstance = Module.instance;
|
|||
|
|
log('Opus库加载成功(使用Module.instance)', 'success');
|
|||
|
|
updateScriptStatus('Opus库加载成功', 'success');
|
|||
|
|
|
|||
|
|
// 3秒后隐藏状态
|
|||
|
|
const statusElement = document.getElementById('scriptStatus');
|
|||
|
|
if (statusElement) statusElement.style.display = 'none';
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 如果没有Module.instance,检查全局Module函数
|
|||
|
|
if (typeof Module._opus_decoder_get_size === 'function') {
|
|||
|
|
window.ModuleInstance = Module;
|
|||
|
|
log('Opus库加载成功(使用全局Module)', 'success');
|
|||
|
|
updateScriptStatus('Opus库加载成功', 'success');
|
|||
|
|
|
|||
|
|
// 3秒后隐藏状态
|
|||
|
|
const statusElement = document.getElementById('scriptStatus');
|
|||
|
|
if (statusElement) statusElement.style.display = 'none';
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
throw new Error('Opus解码函数未找到,可能Module结构不正确');
|
|||
|
|
} catch (err) {
|
|||
|
|
log(`Opus库加载失败,请检查libopus.js文件是否存在且正确: ${err.message}`, 'error');
|
|||
|
|
updateScriptStatus('Opus库加载失败,请检查libopus.js文件是否存在且正确', 'error');
|
|||
|
|
}
|
|||
|
|
}
|