Template:AudioPlayer: Difference between revisions
From Tuyin Archives
No edit summary |
No edit summary |
||
| Line 7: | Line 7: | ||
audioFile.preload = 'metadata'; | audioFile.preload = 'metadata'; | ||
function | document.addEventListener('DOMContentLoaded', function() { | ||
var | // Create container | ||
var container = document.createElement('div'); | |||
container.style.display = 'inline-flex'; | |||
container.style.alignItems = 'center'; | |||
container.style.gap = '12px'; | |||
// Create player box | |||
var playerBox = document.createElement('div'); | |||
playerBox.style.border = '1px solid #8B7355'; | |||
playerBox.style.padding = '8px'; | |||
playerBox.style.display = 'inline-block'; | |||
playerBox.style.borderRadius = '4px'; | |||
playerBox.style.backgroundColor = '#F5F0E8'; | |||
playerBox.style.width = '200px'; | |||
// Create play button | |||
var playButton = document.createElement('button'); | |||
playButton.id = playerId + '_btn'; | |||
playButton.innerHTML = '▶️'; | |||
playButton.style.padding = '4px 8px'; | |||
playButton.style.marginRight = '8px'; | |||
playButton.style.fontSize = '12px'; | |||
playButton.style.border = '1px solid #8B7355'; | |||
playButton.style.background = '#D4C4A8'; | |||
playButton.style.color = '#5D4E37'; | |||
// Create progress bar container | |||
document. | var progressContainer = document.createElement('div'); | ||
progressContainer.style.display = 'inline-block'; | |||
progressContainer.style.width = '120px'; | |||
progressContainer.style.height = '6px'; | |||
progressContainer.style.backgroundColor = '#D4C4A8'; | |||
progressContainer.style.borderRadius = '3px'; | |||
progressContainer.style.verticalAlign = 'middle'; | |||
// Create progress bar inner | |||
var | var progressBar = document.createElement('div'); | ||
button.addEventListener('click', function() { | progressBar.id = playerId + '_progress'; | ||
progressBar.style.height = '100%'; | |||
progressBar.style.backgroundColor = '#8B7355'; | |||
progressBar.style.borderRadius = '3px'; | |||
progressBar.style.width = '0%'; | |||
progressBar.style.transition = 'width 0.1s'; | |||
// Assemble | |||
progressContainer.appendChild(progressBar); | |||
playerBox.appendChild(playButton); | |||
playerBox.appendChild(progressContainer); | |||
container.appendChild(playerBox); | |||
var label = document.createElement('span'); | |||
label.style.color = '#8B7355'; | |||
label.style.fontStyle = 'italic'; | |||
label.innerText = '{{{2|Audio File}}}'; | |||
container.appendChild(label); | |||
// Append to current script's parent | |||
var scriptTag = document.currentScript; | |||
scriptTag.parentNode.insertBefore(container, scriptTag.nextSibling); | |||
// Setup button | |||
playButton.addEventListener('click', function () { | |||
if (audioFile.paused || audioFile.ended) { | if (audioFile.paused || audioFile.ended) { | ||
audioFile.play().then(() => { | audioFile.play().then(() => { | ||
playButton.innerHTML = '⏸️'; | |||
}).catch((e) => { | }).catch((e) => { | ||
alert("Could not play audio: " + e.message); | alert("Could not play audio: " + e.message); | ||
| Line 30: | Line 78: | ||
} else { | } else { | ||
audioFile.pause(); | audioFile.pause(); | ||
playButton.innerHTML = '▶️'; | |||
} | |||
}); | |||
audioFile.addEventListener('timeupdate', function () { | |||
if (audioFile.duration) { | |||
progressBar.style.width = ((audioFile.currentTime / audioFile.duration) * 100) + '%'; | |||
} | } | ||
}); | }); | ||
audioFile.addEventListener(' | audioFile.addEventListener('ended', function () { | ||
playButton.innerHTML = '▶️'; | |||
progressBar.style.width = '0%'; | |||
}); | |||
}); | }); | ||
})(); | })(); | ||
</script> | </script> | ||
</html> | </html> | ||
Revision as of 19:57, 23 July 2025
