Template:AudioPlayer: Difference between revisions
From Tuyin Archives
No edit summary |
No edit summary |
||
| Line 1: | Line 1: | ||
<html> | <html> | ||
<div id="audioplayer-container-{{{1}}}"></div> | |||
<script> | <script> | ||
(function(){ | (function(){ | ||
| Line 8: | Line 9: | ||
document.addEventListener('DOMContentLoaded', function() { | document.addEventListener('DOMContentLoaded', function() { | ||
var container = document.getElementById('audioplayer-container-{{{1}}}'); | |||
var container = document. | if (!container) return; | ||
container | |||
var outer = document.createElement('div'); | |||
var | outer.style.display = 'inline-flex'; | ||
outer.style.alignItems = 'center'; | |||
outer.style.gap = '12px'; | |||
var box = document.createElement('div'); | |||
var | box.style.border = '1px solid #8B7355'; | ||
box.style.padding = '8px'; | |||
box.style.display = 'inline-block'; | |||
box.style.borderRadius = '4px'; | |||
box.style.backgroundColor = '#F5F0E8'; | |||
box.style.width = '200px'; | |||
var button = document.createElement('button'); | |||
var | button.id = playerId + '_btn'; | ||
button.innerHTML = '▶️'; | |||
button.style.padding = '4px 8px'; | |||
button.style.marginRight = '8px'; | |||
button.style.fontSize = '12px'; | |||
button.style.border = '1px solid #8B7355'; | |||
button.style.background = '#D4C4A8'; | |||
button.style.color = '#5D4E37'; | |||
var progressOuter = document.createElement('div'); | |||
var | progressOuter.style.display = 'inline-block'; | ||
progressOuter.style.width = '120px'; | |||
progressOuter.style.height = '6px'; | |||
progressOuter.style.backgroundColor = '#D4C4A8'; | |||
progressOuter.style.borderRadius = '3px'; | |||
progressOuter.style.verticalAlign = 'middle'; | |||
var progress = document.createElement('div'); | |||
progress.id = playerId + '_progress'; | |||
progress.style.height = '100%'; | |||
progress.style.backgroundColor = '#8B7355'; | |||
progress.style.borderRadius = '3px'; | |||
progress.style.width = '0%'; | |||
progress.style.transition = 'width 0.1s'; | |||
progressOuter.appendChild(progress); | |||
box.appendChild(button); | |||
box.appendChild(progressOuter); | |||
outer.appendChild(box); | |||
var label = document.createElement('span'); | var label = document.createElement('span'); | ||
label.style.color = '#8B7355'; | label.style.color = '#8B7355'; | ||
label.style.fontStyle = 'italic'; | label.style.fontStyle = 'italic'; | ||
label. | label.textContent = '{{{2|Audio File}}}'; | ||
outer.appendChild(label); | |||
container.appendChild(outer); | |||
button.addEventListener('click', function () { | |||
if (audioFile.paused || audioFile.ended) { | if (audioFile.paused || audioFile.ended) { | ||
audioFile.play().then(() => { | audioFile.play().then(() => { | ||
button.innerHTML = '⏸️'; | |||
}).catch((e) => { | }).catch((e) => { | ||
alert("Could not play audio: " + e.message); | alert("Could not play audio: " + e.message); | ||
| Line 78: | Line 73: | ||
} else { | } else { | ||
audioFile.pause(); | audioFile.pause(); | ||
button.innerHTML = '▶️'; | |||
} | } | ||
}); | }); | ||
| Line 84: | Line 79: | ||
audioFile.addEventListener('timeupdate', function () { | audioFile.addEventListener('timeupdate', function () { | ||
if (audioFile.duration) { | if (audioFile.duration) { | ||
progress.style.width = ((audioFile.currentTime / audioFile.duration) * 100) + '%'; | |||
} | } | ||
}); | }); | ||
audioFile.addEventListener('ended', function () { | audioFile.addEventListener('ended', function () { | ||
button.innerHTML = '▶️'; | |||
progress.style.width = '0%'; | |||
}); | }); | ||
}); | }); | ||
Revision as of 19:58, 23 July 2025
