diff --git a/app/hooks/useSound.ts b/app/hooks/useSound.ts index 44ffac8..f4473e2 100644 --- a/app/hooks/useSound.ts +++ b/app/hooks/useSound.ts @@ -14,7 +14,7 @@ export function useSound() { audioRef.current.load(); }; - const playSound = (sound : string, loop: boolean = false) => { + const playSound = async (sound : string, loop: boolean = false) => { try { if(loop){ if (!loopingAudioRef.current) { @@ -24,7 +24,7 @@ export function useSound() { loopingAudioRef.current.loop = true; } - const url = window.mediaApi.getSoundUrl(sound); + const url = await window.mediaApi.getSoundUrl(sound); const player = loopingAudioRef.current; player.src = url; @@ -43,7 +43,7 @@ export function useSound() { audioRef.current.loop = loop; } - const url = window.mediaApi.getSoundUrl(sound); + const url = await window.mediaApi.getSoundUrl(sound); const player = audioRef.current; stopSound(); diff --git a/lib/main/ipcs/ipcRuntime.ts b/lib/main/ipcs/ipcRuntime.ts new file mode 100644 index 0000000..5c13ceb --- /dev/null +++ b/lib/main/ipcs/ipcRuntime.ts @@ -0,0 +1,13 @@ +import { app, ipcMain } from "electron"; +import path from "path"; + +/** + * Получить директорию с ресурсами приложения + */ +ipcMain.handle('runtime:get-resources', () => { + const isDev = !app.isPackaged && process.env['ELECTRON_RENDERER_URL']; + if(isDev){ + return path.join(process.cwd(), "resources") + } + return path.join(process.resourcesPath, "resources"); +}); \ No newline at end of file diff --git a/lib/main/main.ts b/lib/main/main.ts index 8a550ef..90c71db 100644 --- a/lib/main/main.ts +++ b/lib/main/main.ts @@ -8,6 +8,7 @@ import './ipcs/ipcUpdate' import './ipcs/ipcNotification' import './ipcs/ipcDevice' import './ipcs/ipcCore' +import './ipcs/ipcRuntime' import { Tray } from 'electron/main' import { join } from 'path' import { Logger } from './logger' diff --git a/lib/preload/index.d.ts b/lib/preload/index.d.ts index 284361b..ed3d8e1 100644 --- a/lib/preload/index.d.ts +++ b/lib/preload/index.d.ts @@ -14,7 +14,7 @@ declare global { deviceName: string; deviceId: string; mediaApi: { - getSoundUrl: (fileName: string) => string; + getSoundUrl: (fileName: string) => Promise; }; } } diff --git a/lib/preload/preload.ts b/lib/preload/preload.ts index dbe848a..288699a 100644 --- a/lib/preload/preload.ts +++ b/lib/preload/preload.ts @@ -6,12 +6,9 @@ import path from 'node:path' import fs from "node:fs"; -function resolveSound(fileName: string) { - const isDev = !process.env.APP_PACKAGED; // или свой флаг dev - const fullPath = isDev - ? path.join(process.cwd(), "resources", "sounds", fileName) - : path.join(process.resourcesPath, "resources", "sounds", fileName); - +async function resolveSound(fileName: string) { + const resourcesPath = await ipcRenderer.invoke('runtime:get-resources'); + const fullPath = path.join(resourcesPath, "sounds", fileName); if (!fs.existsSync(fullPath)) { throw new Error(`Sound not found: ${fullPath}`); } @@ -32,7 +29,7 @@ const exposeContext = async () => { } }); contextBridge.exposeInMainWorld("mediaApi", { - getSoundUrl: (fileName: string) => { + getSoundUrl: async (fileName: string) => { return resolveSound(fileName); } }); @@ -44,7 +41,7 @@ const exposeContext = async () => { window.api = api; window.shell = shell; window.mediaApi = { - getSoundUrl: (fileName: string) => { + getSoundUrl: async (fileName: string) => { return resolveSound(fileName); } }