ExamSchedule/notification/scripts/audioController.js
MKStoler1024 29bb709eae
Notification (#10)
* refactor: 史诗级重构拆分多年狮山代码

* style: 添加设置模态框淡出动画,优化关闭和保存设置的用户体验

* style: 添加错误提示系统,优化用户体验,处理设置和全屏操作中的异常

* feat: 添加主题切换功能,支持亮/暗色模式,优化用户界面

* chore: 测试配置

* feat: 更新亮色主题背景和透明度

* style: 滑块居右

* feat: 初步实现 全是Bug

* feat: 添加提醒队列功能,支持音频预加载和配置导出

* feat: 优化文件输入和按钮样式,提升用户交互体验

* feat: 添加时间广播功能,优化错误提示系统,移除不必要的设置选项

* feat: 移除不再使用的音频文件,更新课程结束时间,优化音频文件加载和选择功能
2025-03-10 00:19:35 +08:00

110 lines
3.3 KiB
JavaScript

var audioController = (function() {
var audioPool = [];
var maxPoolSize = 3;
var soundFiles = {};
var audioSelectPopulated = false;
function init() {
fetch('audio_files.json')
.then(response => response.json())
.then(data => {
soundFiles = data;
Object.keys(soundFiles).forEach(function(type) {
for (var i = 0; i < 2; i++) {
createAudio(type);
}
});
if (!audioSelectPopulated) {
populateAudioSelect();
audioSelectPopulated = true;
}
removeInvalidAudioOptions();
})
.catch(e => errorSystem.show('音频文件加载失败: ' + e.message, 'error'));
}
function createAudio(type) {
var audio = document.createElement('audio');
audio.style.display = 'none';
audio.preload = 'auto';
audio.src = soundFiles[type];
var retryCount = 0;
function loadAudio() {
try {
audio.load();
} catch(e) {
if (retryCount++ < 3) {
setTimeout(loadAudio, 1000);
}
}
}
audio.addEventListener('error', function() {
if (retryCount++ < 3) {
setTimeout(loadAudio, 1000);
}
});
document.body.appendChild(audio);
loadAudio();
audioPool.push(audio);
return audio;
}
function play(type) {
try {
var audio = audioPool.find(function(a) { return a.paused; });
if (!audio) {
if (audioPool.length < maxPoolSize) {
audio = createAudio(type);
} else {
return errorSystem.show('系统繁忙,请稍后再试', 'error');
}
}
audio.src = soundFiles[type];
try {
audio.play();
} catch(e) {
errorSystem.show('播放失败: ' + e.message, 'error');
}
} catch(e) {
errorSystem.show('音频系统错误: ' + e.message, 'error');
}
}
function getAudioSrc(type) {
return soundFiles[type];
}
function populateAudioSelect() {
var selects = document.querySelectorAll('select[name="audioSelect"]');
selects.forEach(select => {
Object.keys(soundFiles).forEach(function(type) {
var option = document.createElement('option');
option.value = type;
option.textContent = type;
select.appendChild(option);
});
});
}
function removeInvalidAudioOptions() {
var selects = document.querySelectorAll('select[name="audioSelect"]');
selects.forEach(select => {
Array.from(select.options).forEach(option => {
if (!soundFiles[option.value]) {
option.remove();
}
});
});
}
return {
init: init,
play: play,
getAudioSrc: getAudioSrc,
populateAudioSelect: populateAudioSelect,
removeInvalidAudioOptions: removeInvalidAudioOptions
};
})();
audioController.init();