Фикс звуков из ресурсов

This commit is contained in:
RoyceDa
2026-03-20 16:46:23 +02:00
parent 8f0e8e8251
commit 9f8840e077
5 changed files with 23 additions and 12 deletions

View File

@@ -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();

View File

@@ -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");
});

View File

@@ -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'

View File

@@ -14,7 +14,7 @@ declare global {
deviceName: string;
deviceId: string;
mediaApi: {
getSoundUrl: (fileName: string) => string;
getSoundUrl: (fileName: string) => Promise<string>;
};
}
}

View File

@@ -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);
}
}