ui: fix audioplayer file length calculation and checks

This commit is contained in:
nick-delirium 2024-09-24 12:11:15 +02:00
parent 9e0b292ddb
commit 5d05a9d102
No known key found for this signature in database
GPG key ID: 93ABD695DF5FDBA0

View file

@ -40,6 +40,7 @@ function DropdownAudioPlayer({
React.useEffect(() => { React.useEffect(() => {
Object.entries(audioRefs.current).forEach(([url, audio]) => { Object.entries(audioRefs.current).forEach(([url, audio]) => {
if (audio) { if (audio) {
audio.loop = false;
audio.addEventListener('loadedmetadata', () => { audio.addEventListener('loadedmetadata', () => {
fileLengths.current[url] = audio.duration; fileLengths.current[url] = audio.duration;
}) })
@ -100,10 +101,14 @@ function DropdownAudioPlayer({
if (audio) { if (audio) {
const file = files.find((f) => f.url === key); const file = files.find((f) => f.url === key);
if (file) { if (file) {
audio.currentTime = Math.max( const targetTime = (timeMs + delta * 1000 - file.start) / 1000;
(timeMs + delta * 1000 - file.start) / 1000, const fileLength = fileLengths.current[key];
0 if (targetTime < 0 || (fileLength && targetTime > fileLength)) {
); audio.pause();
return;
} else {
audio.currentTime = targetTime;
}
} }
} }
}); });
@ -126,15 +131,18 @@ function DropdownAudioPlayer({
Object.entries(audioRefs.current).forEach(([url, audio]) => { Object.entries(audioRefs.current).forEach(([url, audio]) => {
if (audio) { if (audio) {
const file = files.find((f) => f.url === url); const file = files.find((f) => f.url === url);
if (audio.ended && fileLengths.current[url] < time) { const fileLength = fileLengths.current[url];
return; if (file) {
} if (fileLength && (fileLength*1000)+file.start < time) {
if (file && time >= file.start) { return;
if (audio.paused && playing) { }
audio.play(); if (time >= file.start) {
if (audio.paused && playing) {
audio.play();
}
} else {
audio.pause();
} }
} else {
audio.pause();
} }
} }
}); });
@ -157,10 +165,17 @@ function DropdownAudioPlayer({
Object.entries(audioRefs.current).forEach(([url, audio]) => { Object.entries(audioRefs.current).forEach(([url, audio]) => {
if (audio) { if (audio) {
const file = files.find((f) => f.url === url); const file = files.find((f) => f.url === url);
if (file && playing && time >= file.start) { const fileLength = fileLengths.current[url];
audio.play(); if (file) {
} else { if (fileLength && (fileLength*1000)+file.start < time) {
audio.pause(); audio.pause();
return;
}
if (playing && time >= file.start) {
audio.play();
} else {
audio.pause();
}
} }
} }
}); });