Spaces:
Running
Running
// Audio Table Control Scripts | |
document.addEventListener('DOMContentLoaded', function() { | |
const audioElements = document.querySelectorAll('.audio-player, audio[controls]'); | |
// 当播放一个音频时,暂停其他所有音频 | |
function pauseOtherAudios(currentAudio) { | |
audioElements.forEach(audio => { | |
if (audio !== currentAudio && !audio.paused) { | |
audio.pause(); | |
} | |
}); | |
} | |
// 为每个音频元素添加播放事件监听器 | |
audioElements.forEach(audio => { | |
audio.addEventListener('play', function() { | |
pauseOtherAudios(this); | |
}); | |
// 加载开始 | |
audio.addEventListener('loadstart', function() { | |
this.classList.add('loading'); | |
}); | |
// 加载完成 | |
audio.addEventListener('canplay', function() { | |
this.classList.remove('loading'); | |
}); | |
// 错误处理 | |
audio.addEventListener('error', function() { | |
console.log('Audio loading failed:', this.src); | |
this.classList.remove('loading'); | |
}); | |
}); | |
// 添加键盘控制(可选功能) | |
document.addEventListener('keydown', function(e) { | |
// 按空格键暂停所有音频 | |
if (e.code === 'Space' && e.target.tagName !== 'INPUT') { | |
e.preventDefault(); | |
audioElements.forEach(audio => { | |
if (!audio.paused) { | |
audio.pause(); | |
} | |
}); | |
} | |
}); | |
}); |