-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
128 lines (113 loc) · 3.51 KB
/
main.js
File metadata and controls
128 lines (113 loc) · 3.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
const { app, BrowserWindow, Menu, ipcMain } = require('electron');
const path = require('path');
let mainWindow;
function createWindow() {
// 创建浏览器窗口
mainWindow = new BrowserWindow({
width: 1000,
height: 700,
minWidth: 800,
minHeight: 600,
webPreferences: {
nodeIntegration: false,
contextIsolation: true,
enableRemoteModule: false,
preload: path.join(__dirname, 'preload.js')
},
icon: path.join(__dirname, 'icon.ico'),
titleBarStyle: 'default',
show: false
});
// 加载index.html
mainWindow.loadFile('index.html');
// 窗口准备好后显示
mainWindow.once('ready-to-show', () => {
mainWindow.show();
});
// 当窗口被关闭时
mainWindow.on('closed', () => {
mainWindow = null;
});
// 开发模式下打开开发者工具(可选)
// mainWindow.webContents.openDevTools();
}
// 创建应用菜单
function createMenu() {
const template = [
{
label: '文件',
submenu: [
{
label: '退出',
accelerator: 'Ctrl+Q',
click: () => {
app.quit();
}
}
]
},
{
label: '帮助',
submenu: [
{
label: '关于',
click: () => {
const { dialog } = require('electron');
dialog.showMessageBox(mainWindow, {
type: 'info',
title: '关于',
message: 'Kiki音乐播放器',
detail: '版本 1.0.0\n一个简洁美观的本地音乐播放器'
});
}
}
]
}
];
const menu = Menu.buildFromTemplate(template);
Menu.setApplicationMenu(menu);
}
// 处理音频元数据解析请求
ipcMain.handle('parse-audio-metadata', async (event, fileData) => {
try {
// 动态导入music-metadata(ESM模块)
const mm = await import('music-metadata');
// fileData是ArrayBuffer,需要转换为Buffer
const buffer = Buffer.from(fileData);
const metadata = await mm.parseBuffer(buffer, { size: buffer.length });
return {
success: true,
metadata: {
format: metadata.format,
common: metadata.common,
native: metadata.native
}
};
} catch (error) {
console.error('解析元数据失败:', error);
return {
success: false,
error: error.message
};
}
});
// 当Electron完成初始化并准备创建浏览器窗口时调用此方法
app.whenReady().then(() => {
createWindow();
createMenu();
app.on('activate', () => {
// 在macOS上,当单击dock图标并且没有其他窗口打开时,
// 通常在应用程序中重新创建一个窗口。
if (BrowserWindow.getAllWindows().length === 0) {
createWindow();
}
});
});
// 当所有窗口都被关闭时退出应用
app.on('window-all-closed', () => {
// 在macOS上,除非用户用Cmd + Q确定地退出,
// 否则绝大部分应用程序及其菜单栏会保持激活。
if (process.platform !== 'darwin') {
app.quit();
}
});