Feat: 自动检索音效文件夹下的素材随机播放,并配备对应文案
This commit is contained in:
17
main/main.js
17
main/main.js
@ -1,6 +1,7 @@
|
||||
const { app, BrowserWindow, ipcMain } = require("electron");
|
||||
const path = require("path");
|
||||
const { spawn } = require('child_process')
|
||||
const fs = require("fs");
|
||||
|
||||
// 音效播放器
|
||||
function playAudioFile(filePath) {
|
||||
@ -52,6 +53,22 @@ ipcMain.handle('get-sound-path', (_, soundFile) => {
|
||||
}
|
||||
});
|
||||
|
||||
ipcMain.handle('get-sound-files', async () => {
|
||||
const soundDir = process.env.NODE_ENV === 'development'
|
||||
? path.join(__dirname, '../renderer/public/assets/sounds')
|
||||
: path.join(__dirname, '../renderer/dist/assets/sounds');
|
||||
|
||||
try {
|
||||
// 读取目录下的所有文件名
|
||||
const files = await fs.promises.readdir(soundDir);
|
||||
// 筛选出 .mp3 文件并返回
|
||||
return files.filter(file => file.endsWith('.mp3'));
|
||||
} catch (error) {
|
||||
console.error('无法读取声音目录:', error);
|
||||
return []; // 如果出错则返回空数组
|
||||
}
|
||||
});
|
||||
|
||||
let mainWindow;
|
||||
|
||||
function createWindow() {
|
||||
|
@ -9,6 +9,7 @@ contextBridge.exposeInMainWorld('electronAPI', {
|
||||
}
|
||||
},
|
||||
getSoundPath: (soundFile) => ipcRenderer.invoke('get-sound-path', soundFile),
|
||||
getSoundFiles: () => ipcRenderer.invoke('get-sound-files'),
|
||||
showTooltip: (text) => ipcRenderer.send('show-tooltip', text),
|
||||
onUpdatePosition: (callback) => {
|
||||
ipcRenderer.on('update-position', (_, position) => callback(position))
|
||||
|
@ -1,34 +1,44 @@
|
||||
<script setup>
|
||||
import { ref } from "vue";
|
||||
import { ref, onMounted } from "vue";
|
||||
import petGif from "./assets/pet.gif";
|
||||
import { Howl } from "howler";
|
||||
|
||||
// 配置数据
|
||||
const tooltips = [
|
||||
"说的道理~",
|
||||
"尊尼获加",
|
||||
"为什么不开大!!",
|
||||
"(凤鸣)",
|
||||
];
|
||||
const soundFiles = [
|
||||
"cnmb.mp3",
|
||||
"冲刺,冲.mp3",
|
||||
"哎你怎么死了.mp3",
|
||||
"哎,猪逼.mp3",
|
||||
"啊啊啊我草你妈呀.mp3",
|
||||
"嘟嘟嘟.mp3",
|
||||
"韭菜盒子.mp3",
|
||||
"哇袄.mp3",
|
||||
];
|
||||
|
||||
// 状态管理
|
||||
const soundFiles = ref([]); // 存储从主进程获取的声音文件名列表
|
||||
const showTooltip = ref(false);
|
||||
const currentTooltip = ref("");
|
||||
const isLoading = ref(true); // 跟踪文件列表是否已加载
|
||||
|
||||
// 在组件挂载后,从主进程获取声音文件列表
|
||||
onMounted(async () => {
|
||||
if (window.electronAPI && typeof window.electronAPI.getSoundFiles === 'function') {
|
||||
try {
|
||||
const files = await window.electronAPI.getSoundFiles();
|
||||
soundFiles.value = files;
|
||||
console.log("成功加载声音文件:", files);
|
||||
} catch (error) {
|
||||
console.error("获取声音文件列表失败:", error);
|
||||
} finally {
|
||||
isLoading.value = false;
|
||||
}
|
||||
} else {
|
||||
console.error("electronAPI.getSoundFiles 函数不存在。");
|
||||
isLoading.value = false;
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
// 点击事件处理
|
||||
const handleClick = async () => {
|
||||
// 如果正在加载或没有声音文件,则不执行任何操作
|
||||
if (isLoading.value || soundFiles.value.length === 0) {
|
||||
console.warn("声音文件尚未加载或列表为空。");
|
||||
return;
|
||||
}
|
||||
|
||||
// 从文件列表中随机选择一个文件
|
||||
const randomSoundFile =
|
||||
soundFiles[Math.floor(Math.random() * soundFiles.length)];
|
||||
soundFiles.value[Math.floor(Math.random() * soundFiles.value.length)];
|
||||
console.log("请求播放:", randomSoundFile);
|
||||
|
||||
try {
|
||||
@ -47,7 +57,8 @@ const handleClick = async () => {
|
||||
console.error("播放失败:", err);
|
||||
}
|
||||
|
||||
currentTooltip.value = tooltips[Math.floor(Math.random() * tooltips.length)];
|
||||
// 将提示文案设置为文件名
|
||||
currentTooltip.value = randomSoundFile.replace(/\.mp3$/, '');
|
||||
showTooltip.value = true;
|
||||
setTimeout(() => (showTooltip.value = false), 2000);
|
||||
};
|
||||
|
Reference in New Issue
Block a user