Фикс звуков из ресурсов
This commit is contained in:
@@ -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();
|
||||
|
||||
13
lib/main/ipcs/ipcRuntime.ts
Normal file
13
lib/main/ipcs/ipcRuntime.ts
Normal 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");
|
||||
});
|
||||
@@ -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'
|
||||
|
||||
2
lib/preload/index.d.ts
vendored
2
lib/preload/index.d.ts
vendored
@@ -14,7 +14,7 @@ declare global {
|
||||
deviceName: string;
|
||||
deviceId: string;
|
||||
mediaApi: {
|
||||
getSoundUrl: (fileName: string) => string;
|
||||
getSoundUrl: (fileName: string) => Promise<string>;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user