refactor: update sound file handling to resolve base URL dynamically
This commit is contained in:
@@ -13,19 +13,30 @@ export enum CallSoundType {
|
||||
CallDialing = 'call_dialing',
|
||||
}
|
||||
|
||||
/**
|
||||
* Maps sound types to their URL paths (served from public folder)
|
||||
*/
|
||||
const SOUND_SOURCES: Record<CallSoundType, string> = {
|
||||
[CallSoundType.MicMute]: './sound/Mic_Mute.ogg',
|
||||
[CallSoundType.MicUnmute]: './sound/Mic_Unmuted.ogg',
|
||||
[CallSoundType.Deafen]: './sound/Speakers_Deafen.ogg',
|
||||
[CallSoundType.Undeafen]: './sound/Speakers_Undeafen.ogg',
|
||||
[CallSoundType.CallJoined]: './sound/Call_Joined.ogg',
|
||||
[CallSoundType.CallDepart]: './sound/Call_Depart.ogg',
|
||||
[CallSoundType.CallDialing]: './sound/Call_Dialing.ogg',
|
||||
const SOUND_FILES: Record<CallSoundType, string> = {
|
||||
[CallSoundType.MicMute]: 'Mic_Mute.ogg',
|
||||
[CallSoundType.MicUnmute]: 'Mic_Unmuted.ogg',
|
||||
[CallSoundType.Deafen]: 'Speakers_Deafen.ogg',
|
||||
[CallSoundType.Undeafen]: 'Speakers_Undeafen.ogg',
|
||||
[CallSoundType.CallJoined]: 'Call_Joined.ogg',
|
||||
[CallSoundType.CallDepart]: 'Call_Depart.ogg',
|
||||
[CallSoundType.CallDialing]: '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.
|
||||
* Handles playing local sounds and coordinating with remote participants.
|
||||
@@ -38,15 +49,15 @@ export class CallSounds {
|
||||
private isDeafened = false;
|
||||
|
||||
constructor() {
|
||||
this.preloadSounds();
|
||||
resolveSoundBaseUrl().then((base) => this.preloadSounds(base));
|
||||
}
|
||||
|
||||
/**
|
||||
* Preloads all call sounds for instant playback
|
||||
*/
|
||||
private preloadSounds(): void {
|
||||
Object.entries(SOUND_SOURCES).forEach(([type, src]) => {
|
||||
const audio = new Audio(src);
|
||||
private preloadSounds(base: string): void {
|
||||
Object.entries(SOUND_FILES).forEach(([type, file]) => {
|
||||
const audio = new Audio(`${base}/${file}`);
|
||||
audio.preload = 'auto';
|
||||
this.audioElements.set(type as CallSoundType, audio);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user