Update: 音效播放功能
This commit is contained in:
26
main/main.js
26
main/main.js
@ -1,5 +1,31 @@
|
||||
const { app, BrowserWindow, ipcMain } = require("electron");
|
||||
const path = require("path");
|
||||
const { spawn } = require('child_process')
|
||||
|
||||
// 音效播放器
|
||||
function playAudioFile(filePath) {
|
||||
if (process.platform === 'win32') {
|
||||
spawn('cmd', ['/c', `start "" "${filePath}"`])
|
||||
} else if (process.platform === 'darwin') {
|
||||
spawn('afplay', [filePath])
|
||||
} else {
|
||||
spawn('aplay', [filePath])
|
||||
}
|
||||
}
|
||||
|
||||
ipcMain.on('play-sound', (_, soundFile) => {
|
||||
const soundPath = path.join(
|
||||
__dirname,
|
||||
'../renderer/src/assets/sounds',
|
||||
soundFile
|
||||
)
|
||||
|
||||
if (require('fs').existsSync(soundPath)) {
|
||||
playAudioFile(soundPath)
|
||||
} else {
|
||||
console.error('音效文件不存在:', soundPath)
|
||||
}
|
||||
})
|
||||
|
||||
let mainWindow;
|
||||
|
||||
|
@ -1,7 +1,13 @@
|
||||
const { contextBridge, ipcRenderer } = require('electron')
|
||||
|
||||
contextBridge.exposeInMainWorld('electronAPI', {
|
||||
playSound: (soundFile) => ipcRenderer.send('play-sound', soundFile),
|
||||
playSound: (soundFile) => {
|
||||
try {
|
||||
ipcRenderer.send('play-sound', soundFile)
|
||||
} catch (err) {
|
||||
console.error('播放音效失败:', err)
|
||||
}
|
||||
},
|
||||
showTooltip: (text) => ipcRenderer.send('show-tooltip', text),
|
||||
onUpdatePosition: (callback) => {
|
||||
ipcRenderer.on('update-position', (_, position) => callback(position))
|
||||
|
Reference in New Issue
Block a user