|
| 1 | +document.addEventListener("DOMContentLoaded", () => { |
| 2 | + let currentlyPlaying = null; |
| 3 | + function playAllAudios() { |
| 4 | + let audios = Array.from(document.querySelectorAll('audio')); |
| 5 | + function playNext() { |
| 6 | + if (!audios.length) { |
| 7 | + return; |
| 8 | + } |
| 9 | + currentlyPlaying = audios.shift(); |
| 10 | + let listener = () => { playNext(); } |
| 11 | + currentlyPlaying.endedListener = listener; |
| 12 | + currentlyPlaying.addEventListener('ended', listener); |
| 13 | + currentlyPlaying.play(); |
| 14 | + } |
| 15 | + playNext(); |
| 16 | + } |
| 17 | + |
| 18 | + const audios = document.querySelectorAll('audio'); |
| 19 | + if (audios.length < 2) { |
| 20 | + // Not enough audio elements |
| 21 | + return; |
| 22 | + } |
| 23 | + // Add a button to play all |
| 24 | + const button = document.createElement('button'); |
| 25 | + button.setAttribute('type', 'button'); |
| 26 | + // Datasette button CSS looks for form button[type=button] |
| 27 | + const form = document.createElement('form'); |
| 28 | + form.style.marginBottom = '1em'; |
| 29 | + form.appendChild(button); |
| 30 | + button.innerText = `Play all ${audios.length} MP3s`; |
| 31 | + let isPlaying = false; |
| 32 | + button.addEventListener('click', () => { |
| 33 | + if (!isPlaying) { |
| 34 | + // First clean up any previous endedListener |
| 35 | + audios.forEach(el => { |
| 36 | + el.currentTime = 0; |
| 37 | + if (el.endedListener) { |
| 38 | + el.removeEventListener('ended', el.endedListener); |
| 39 | + } |
| 40 | + }); |
| 41 | + playAllAudios(); |
| 42 | + isPlaying = true; |
| 43 | + button.innerText = `Playing all ${audios.length} MP3s - Stop`; |
| 44 | + } else { |
| 45 | + if (currentlyPlaying) { |
| 46 | + currentlyPlaying.pause(); |
| 47 | + } |
| 48 | + isPlaying = false; |
| 49 | + button.innerText = `Play all ${audios.length} MP3s`; |
| 50 | + } |
| 51 | + }); |
| 52 | + const tableWrapper = document.querySelector('.table-wrapper'); |
| 53 | + tableWrapper.parentNode.insertBefore(form, tableWrapper); |
| 54 | +}); |
0 commit comments