fix: voice message preview seeking not working on firefox

This commit is contained in:
hazre committed 2026-03-22 18:13:09 +01:00
1 parent 340d643233
commit 2af3c72e30
3 files changed
+41 -9

No files matched your search

@@ -0,0 +1,5 @@
---
default: patch
---
# Fixed voice message scrubbing/seeking on Firefox by switching the recorder from WebM (no seek index) to Ogg/Opus.
@@ -128,6 +128,10 @@ function PreviewAudio({ fileItem }: PreviewAudioProps) {
return undefined;
}
const audio = new Audio(audioUrl);
audio.preload = 'auto';
// Explicitly load so Firefox parses metadata immediately, making
// currentTime writable before the user has ever pressed play.
audio.load();
audioRef.current = audio;
audio.onended = () => {
@@ -177,13 +181,34 @@ function PreviewAudio({ fileItem }: PreviewAudioProps) {
}
};
const seekTo = (audio: HTMLAudioElement, targetTime: number) => {
// Alias to a local const to satisfy no-param-reassign.
const el = audio;
if (el.seekable.length > 0) {
el.currentTime = targetTime;
setCurrentTime(targetTime);
} else {
// Metadata not yet loaded (Firefox, first scrub before load() resolves).
// Do NOT call load() again here — that resets currentTime to 0 and
// restarts the fetch. load() was already called in the useEffect;
// just wait for the in-flight loadedmetadata event.
el.addEventListener(
'loadedmetadata',
() => {
el.currentTime = targetTime;
setCurrentTime(targetTime);
},
{ once: true }
);
}
};
const handleScrubClick = (e: React.MouseEvent<HTMLDivElement>) => {
const audio = audioRef.current;
if (!audio || !duration) return;
const rect = e.currentTarget.getBoundingClientRect();
const ratio = Math.max(0, Math.min(1, (e.clientX - rect.left) / rect.width));
audio.currentTime = ratio * duration;
setCurrentTime(audio.currentTime);
seekTo(audio, ratio * duration);
};
const handleKeyDown = (e: React.KeyboardEvent) => {
@@ -209,8 +234,7 @@ function PreviewAudio({ fileItem }: PreviewAudioProps) {
return;
}
audio.currentTime = newTime;
setCurrentTime(newTime);
seekTo(audio, newTime);
};
return (
@@ -13,14 +13,17 @@ const safariPreferredCodecs = [
];
const defaultPreferredCodecs = [
// Chromium / Firefox stable path.
// Firefox: ogg produces seekable blobs; webm passes isTypeSupported() but
// records without a cue index so currentTime assignment silently fails.
// Must come before webm so Firefox picks ogg.
'audio/ogg;codecs=opus',
'audio/ogg',
// Chromium: webm is seekable and preferred. Since Chromium doesn't support
// ogg recording, it will skip the above and land here.
'audio/webm;codecs=opus',
'audio/webm',
// Firefox
'audio/ogg;codecs=opus',
'audio/ogg;codecs=vorbis',
'audio/ogg',
// Fallbacks
'audio/ogg;codecs=vorbis',
'audio/wav;codecs=1',
'audio/wav',
'audio/mpeg',