refactor: update sound file handling to resolve base URL dynamically

This commit is contained in:
2026-04-03 21:14:00 +11:00
parent fe193bb062
commit f64ce6168d

View File

@@ -13,19 +13,30 @@ export enum CallSoundType {
CallDialing = 'call_dialing', CallDialing = 'call_dialing',
} }
/** const SOUND_FILES: Record<CallSoundType, string> = {
* Maps sound types to their URL paths (served from public folder) [CallSoundType.MicMute]: 'Mic_Mute.ogg',
*/ [CallSoundType.MicUnmute]: 'Mic_Unmuted.ogg',
const SOUND_SOURCES: Record<CallSoundType, string> = { [CallSoundType.Deafen]: 'Speakers_Deafen.ogg',
[CallSoundType.MicMute]: './sound/Mic_Mute.ogg', [CallSoundType.Undeafen]: 'Speakers_Undeafen.ogg',
[CallSoundType.MicUnmute]: './sound/Mic_Unmuted.ogg', [CallSoundType.CallJoined]: 'Call_Joined.ogg',
[CallSoundType.Deafen]: './sound/Speakers_Deafen.ogg', [CallSoundType.CallDepart]: 'Call_Depart.ogg',
[CallSoundType.Undeafen]: './sound/Speakers_Undeafen.ogg', [CallSoundType.CallDialing]: 'Call_Dialing.ogg',
[CallSoundType.CallJoined]: './sound/Call_Joined.ogg',
[CallSoundType.CallDepart]: './sound/Call_Depart.ogg',
[CallSoundType.CallDialing]: './sound/Call_Dialing.ogg',
}; };
/**
* Resolves the base URL for sounds.
* In Electron production, sounds are in resources/sound/ as plain files.
* In dev or browser, sounds are served from public/sound/.
*/
async function resolveSoundBaseUrl(): Promise<string> {
const electronAudio = (window as any).electron?.audio;
if (electronAudio?.getSoundBaseUrl) {
const base: string | null = await electronAudio.getSoundBaseUrl();
if (base) return base;
}
return './sound';
}
/** /**
* Manages call-related sound effects. * Manages call-related sound effects.
* Handles playing local sounds and coordinating with remote participants. * Handles playing local sounds and coordinating with remote participants.
@@ -38,15 +49,15 @@ export class CallSounds {
private isDeafened = false; private isDeafened = false;
constructor() { constructor() {
this.preloadSounds(); resolveSoundBaseUrl().then((base) => this.preloadSounds(base));
} }
/** /**
* Preloads all call sounds for instant playback * Preloads all call sounds for instant playback
*/ */
private preloadSounds(): void { private preloadSounds(base: string): void {
Object.entries(SOUND_SOURCES).forEach(([type, src]) => { Object.entries(SOUND_FILES).forEach(([type, file]) => {
const audio = new Audio(src); const audio = new Audio(`${base}/${file}`);
audio.preload = 'auto'; audio.preload = 'auto';
this.audioElements.set(type as CallSoundType, audio); this.audioElements.set(type as CallSoundType, audio);
}); });