微件:音频
更多操作
<button class="ec-audio-btn" type="button" aria-label="播放音效">
</button>
<audio
preload="none"
src=""
data-file-url=""
></audio>
<style> .ec-audio{position:relative;display:inline-flex;align-items:center;gap:7px;vertical-align:middle;line-height:1.5} .ec-audio-btn{display:inline-flex;align-items:center;justify-content:center;width:26px;height:26px;padding:0;border:1px solid #c9d2de;border-radius:50%;background:#fff;cursor:pointer;font-size:14px;line-height:1;color:inherit;transition:background .15s,border-color .15s,box-shadow .15s,transform .08s;user-select:none;-webkit-user-select:none;-webkit-touch-callout:none;touch-action:manipulation} .ec-audio-btn:hover{background:#eef3fd;border-color:#2f6fed} .ec-audio-btn:active{transform:scale(.92)} .ec-audio-btn:focus-visible{outline:2px solid #2f6fed;outline-offset:2px} .ec-audio-ico{display:inline-flex;align-items:center;justify-content:center;line-height:0;color:#48525f} .ec-audio.is-playing .ec-audio-ico{color:#fff} .ec-audio.is-playing .ec-audio-btn{background:#2f6fed;border-color:#2f6fed;box-shadow:0 0 0 3px rgba(47,111,237,.18)} .ec-audio.is-error .ec-audio-btn{border-color:#d9534f;background:#fdf1f0} .ec-audio-name{font-size:13.5px;color:#2f3742}
.ec-audio-tip{
position:absolute; left:0; top:100%; margin-top:6px; z-index:30; display:none; align-items:center; gap:2px; max-width:320px; padding:5px 9px; border:1px solid #dbe3ee; border-radius:8px; background:#fff; box-shadow:0 8px 20px rgba(20,40,80,.14); font-size:12px; line-height:1.45; color:#2f3742; white-space:nowrap;
} .ec-audio-tip::before{content:"";position:absolute;left:0;right:0;top:-8px;height:8px} .ec-audio.is-tip .ec-audio-tip{display:inline-flex} .ec-audio-tip-label{color:#5b6472;flex:0 0 auto} .ec-audio-tip-link{
display:inline-block; max-width:220px; overflow:hidden; text-overflow:ellipsis; white-space:nowrap; color:#2f6fed; text-decoration:none; font-weight:600;
} .ec-audio-tip-link:hover{text-decoration:underline} .ec-audio-tip-empty{color:#98a2b3} </style>
<script> (function () {
if (window.__ecAudioReady) return; window.__ecAudioReady = true;
var FILE_PAGE_PREFIX = 'https://wiki.easecation.net/file:';
function closest(el, sel) {
while (el && el.nodeType === 1) {
if (el.matches && el.matches(sel)) return el;
el = el.parentNode;
}
return null;
}
function stop(box) {
var a = box.querySelector('audio');
if (a) { try { a.pause(); a.currentTime = 0; } catch (e) {} }
box.classList.remove('is-playing');
}
function audioFileName(audio) {
var raw = audio ? (audio.getAttribute('src') || ) : ;
if (!raw) return ;
try {
var u = new URL(raw, location.href);
var segs = u.pathname.split('/').filter(Boolean);
var last = segs.length ? segs[segs.length - 1] : ;
if (!last) return ;
try { return decodeURIComponent(last); } catch (e) { return last; }
} catch (e) {
var parts = raw.split(/[\\/]/);
var name = parts[parts.length - 1] || ;
try { return decodeURIComponent(name); } catch (err) { return name; }
}
}
function filePageInfo(audio) {
var name = audioFileName(audio);
var configured = (audio && audio.getAttribute('data-file-url')) || ;
configured = configured.trim();
if (configured) {
return { name: name || '未命名音频', href: configured };
}
if (!name) {
return { name: '未命名音频', href: };
}
return {
name: name,
href: FILE_PAGE_PREFIX + encodeURIComponent(name)
};
}
function buildTip(box) {
if (box.__tip) return box.__tip;
var audio = box.querySelector('audio');
var info = filePageInfo(audio);
var tip = document.createElement('span');
tip.className = 'ec-audio-tip';
tip.setAttribute('role', 'tooltip');
if (info.href) {
var label = document.createElement('span');
label.className = 'ec-audio-tip-label';
label.textContent = '进入页面:';
var link = document.createElement('a');
link.className = 'ec-audio-tip-link';
link.href = info.href;
link.target = '_blank';
link.rel = 'noopener noreferrer';
link.textContent = info.name;
link.title = info.name + '\n' + info.href;
link.addEventListener('click', function () {
setTimeout(function () { hideTip(box); }, 0);
});
tip.appendChild(label);
tip.appendChild(link);
} else {
var empty = document.createElement('span');
empty.className = 'ec-audio-tip-empty';
empty.textContent = '无法生成文件页面链接';
tip.appendChild(empty);
}
box.appendChild(tip); box.__tip = tip; return tip; }
function showTip(box) {
buildTip(box);
var open = document.querySelectorAll('.ec-audio.is-tip');
for (var i = 0; i < open.length; i++) {
if (open[i] !== box) open[i].classList.remove('is-tip');
}
box.classList.add('is-tip');
}
function hideTip(box) {
box.classList.remove('is-tip');
}
function hideAllTips() {
var open = document.querySelectorAll('.ec-audio.is-tip');
for (var i = 0; i < open.length; i++) open[i].classList.remove('is-tip');
}
var canHover = !window.matchMedia || window.matchMedia('(hover: hover)').matches;
if (canHover) {
document.addEventListener('mouseover', function (e) {
var btn = closest(e.target, '.ec-audio-btn');
if (!btn) return;
var box = closest(btn, '.ec-audio');
if (box) showTip(box);
});
document.addEventListener('mouseout', function (e) {
var btn = closest(e.target, '.ec-audio-btn');
if (!btn) return;
var box = closest(btn, '.ec-audio');
if (!box) return;
var to = e.relatedTarget;
if (to && box.contains(to)) return;
hideTip(box);
});
}
var pressTimer = null; var pressStart = null;
function clearPress() {
if (pressTimer) { clearTimeout(pressTimer); pressTimer = null; }
pressStart = null;
}
document.addEventListener('pointerdown', function (e) {
var btn = closest(e.target, '.ec-audio-btn');
if (!btn) return;
var box = closest(btn, '.ec-audio');
if (!box) return;
clearPress();
pressStart = { x: e.clientX, y: e.clientY };
pressTimer = setTimeout(function () {
pressTimer = null;
box.__longPressed = true;
showTip(box);
}, 450);
}, true);
document.addEventListener('pointermove', function (e) {
if (!pressStart) return;
if (Math.abs(e.clientX - pressStart.x) > 8 || Math.abs(e.clientY - pressStart.y) > 8) {
clearPress();
}
}, true);
document.addEventListener('pointerup', clearPress, true);
document.addEventListener('pointercancel', clearPress, true);
document.addEventListener('click', function (e) {
var btn = closest(e.target, '.ec-audio-btn');
if (btn) {
var btnBox = closest(btn, '.ec-audio');
if (btnBox && btnBox.__longPressed) {
btnBox.__longPressed = false;
return;
}
var audio = btnBox ? btnBox.querySelector('audio') : null;
if (btnBox && audio) {
if (btnBox.classList.contains('is-playing')) { stop(btnBox); return; }
var playing = document.querySelectorAll('.ec-audio.is-playing');
for (var i = 0; i < playing.length; i++) stop(playing[i]);
btnBox.classList.remove('is-error');
try { audio.currentTime = 0; } catch (err) {}
btnBox.classList.add('is-playing');
var p = audio.play();
if (p && p.catch) {
p.catch(function (err) {
btnBox.classList.remove('is-playing');
btnBox.classList.add('is-error');
btn.title = '播放失败:' + (err && err.name ? err.name : '未知错误');
});
}
}
return;
}
if (!closest(e.target, '.ec-audio')) hideAllTips(); });
document.addEventListener('ended', function (e) {
var box = closest(e.target, '.ec-audio');
if (box) box.classList.remove('is-playing');
}, true);
document.addEventListener('error', function (e) {
var box = closest(e.target, '.ec-audio');
if (box) {
box.classList.remove('is-playing');
box.classList.add('is-error');
var btn = box.querySelector('.ec-audio-btn');
if (btn) btn.title = '音频加载失败:文件不存在或浏览器不支持该格式';
}
}, true);
})(); </script>