github.com/serge-v/zero@v1.0.2-0.20220911142406-af4b6a19e68a/examples/audio/player.js (about) 1 var index = -1; 2 var pos = -1; 3 var volume = 70; 4 var dir = ''; 5 var countdown = 0; 6 7 function blink(btn) { 8 btn.style.color = 'lightGreen'; 9 sleep(200).then(() => { 10 btn.style.color = ''; 11 }); 12 } 13 14 function playPrev() { 15 index--; 16 pos = 0; 17 if (index < 0) { 18 index = fileList.length - 1; 19 } 20 blink(btnPrev); 21 playIndex(index, false); 22 } 23 24 function playNext() { 25 index++; 26 pos = 0; 27 if (index === fileList.length) { 28 index = 0; 29 } 30 blink(btnNext); 31 playIndex(index, false); 32 } 33 34 function playIndex(index, isInit) { 35 title.innerText = unescape(decodeURI(fileList[index])); 36 audio.src = "/audio/" + fileList[index]; 37 audio.volume = volume / 100; 38 audio.currentTime = pos; 39 btnStatus.innerText = volume; 40 if (!isInit) { 41 audio.play(); 42 localStorage.setItem(dir + '_index', index); 43 } 44 } 45 46 function sleep (time) { 47 return new Promise((resolve) => setTimeout(resolve, time)); 48 } 49 50 function volumeUp() { 51 volume += 10; 52 if (volume > 100) { 53 volume = 100; 54 } 55 audio.volume = volume / 100; 56 btnStatus.innerText = volume; 57 blink(btnStatus); 58 blink(btnVolumeUp); 59 } 60 61 function volumeDown() { 62 volume -= 10; 63 if (volume < 10) { 64 volume = 10; 65 } 66 audio.volume = volume / 100; 67 btnStatus.innerText = volume; 68 blink(btnStatus); 69 blink(btnVolumeDown); 70 } 71 72 function seekForward() { 73 blink(btnForward); 74 pos += 30; 75 var dur = parseInt(audio.duration.toFixed()); 76 if (pos >= dur - 2) { 77 pos = dur - 2; 78 } 79 audio.currentTime = pos; 80 } 81 82 function seekBack() { 83 blink(btnBack); 84 pos -= 30; 85 if (pos < 0) { 86 pos = 0; 87 } 88 audio.currentTime = pos; 89 } 90 91 function listReceived(data, isInit){ 92 fileList = data; 93 if (index == null || index >= fileList.length) { 94 index = -1; 95 } 96 pos = localStorage.getItem(dir + '_position'); 97 if (pos == null) { 98 pos = 5; 99 } else { 100 pos = parseInt(pos); 101 } 102 pos -= 5; 103 if (pos < 0) { 104 pos = 0; 105 } 106 btnStatus.innerText = pos + '/' + audio.duration.toFixed(); 107 if (index > -1) { 108 btnPause.style.color = 'pink'; 109 btnPlay.style.color = 'lightGreen'; 110 playIndex(index, isInit); 111 } else { 112 title.innerText = dir + ' ' + fileList.length + ' files' 113 } 114 } 115 116 function openDirPane() { 117 mainPane.style.display = 'none'; 118 dirPane.style.display = ''; 119 } 120 121 function openMainPane() { 122 dirPane.style.display = 'none'; 123 listPane.style.display = 'none'; 124 mainPane.style.display = ''; 125 } 126 127 function selectDir(name) { 128 localStorage.setItem('dir', name) 129 dir = name; 130 openMainPane(); 131 index = localStorage.getItem(dir + '_index'); 132 if (index == null) { 133 index = 0; 134 } 135 fetch("/files?dir="+dir).then(response => response.json()).then(data => listReceived(data, false)); 136 } 137 138 function addTimer(seconds, btn) { 139 blink(btn); 140 countdown += seconds; 141 } 142 143 var oldpos = 0; 144 145 function init() { 146 audio.addEventListener('pause', (event) => { 147 btnPause.style.color = 'pink'; 148 btnPlay.style.color = 'lightGreen'; 149 }); 150 audio.addEventListener('play', (event) => { 151 btnPause.style.color = 'lightGreen'; 152 btnPlay.style.color = 'pink'; 153 }); 154 155 audio.addEventListener('timeupdate', (event) => { 156 var newpos = parseInt(audio.currentTime.toFixed()); 157 if (newpos != pos) { 158 if (countdown > 0) { 159 countdown--; 160 if (countdown == 0) { 161 audio.pause(); 162 } 163 } 164 165 pos = newpos; 166 var msg = pos + '/' + audio.duration.toFixed(); 167 if (countdown > 0) { 168 msg += '//' + (countdown/60).toFixed(); 169 } 170 171 btnStatus.innerText = msg; 172 localStorage.setItem(dir + '_position', pos); 173 } 174 }); 175 audio.addEventListener('ended', (event) => { 176 playNext(); 177 }); 178 179 index = -1; 180 181 dir = localStorage.getItem('dir'); 182 if (dir != null) { 183 index = localStorage.getItem(dir + '_index'); 184 if (index == null) { 185 index = -1; 186 } else { 187 index = parseInt(index); 188 } 189 fetch("/files?dir="+dir).then(response => response.json()).then(data => listReceived(data, true)); 190 } 191 } 192 193 function openListPane() { 194 mainPane.style.display = 'none'; 195 listPane.style.display = ''; 196 } 197 198 function fillList(data) { 199 songList.innerHTML = data; 200 } 201 202 function showList() { 203 openListPane(); 204 fetch("/songlist?dir="+dir).then(response=>response.text()).then(t => fillList(t)); 205 }