|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657 |
- function AudioForm(el) {
- const interval = el.elements['interval']
- const play = el.elements['play']
- const output = el.querySelector('output')
- const audioPool = []
- let intervalHandle = null
- let audioIndex = 0
- const items = 50
-
- for (let i = 0; i < items; i += 1) {
- const newAudio = new Audio('./sfx/weapons/guns/smg-hkmp7/1_unload_var0.wav')
- newAudio.volume = 0.2;
- newAudio.addEventListener('canplaythrough', () => {
- audioPool[i].ready = true
- })
- newAudio.addEventListener('ended', () => {
- newAudio.pause()
- newAudio.currentTime = 0
- })
-
- audioPool[i] = {
- audio: newAudio,
- ended: true,
- ready: false,
- }
- newAudio.load()
- }
-
- const playSound = () => {
- for (let i = 0; i < items; i += 1) {
- if (!audioPool[i].ready) {
- return;
- }
- }
-
- audioPool[audioIndex].audio.play();
- audioIndex = (audioIndex + 1) % items;
- }
-
- el.addEventListener('submit', (e) => {
- e.preventDefault()
-
- const ms = 60000 / Number(interval.value);
- output.innerText = `${ms} ms`
- if (play.checked) {
- intervalHandle = window.setInterval(() => {
- playSound()
- }, ms)
- } else {
- window.clearInterval(intervalHandle);
- }
-
- play.checked = !play.checked
- })
- }
-
- const audioForm = new AudioForm(window.document.getElementById('formAudio'))
|