-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
492 lines (426 loc) · 12.5 KB
/
app.js
File metadata and controls
492 lines (426 loc) · 12.5 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
const { app, BrowserWindow, Menu, dialog, ipcMain } = require('electron')
require('@electron/remote/main').initialize()
const fp = require("fs-props")
const fs = require('fs')
const path = require('path')
const url = require('url')
let mainWindow, loadingWindow
let metadataInfo = null
let playlistInfo = null
let isNewTrackQueued = false
const menuTemplate = [
{
label: 'File',
submenu: [
{
label: 'Import Media',
accelerator: 'Ctrl+I',
click: importSong
},
{
label: 'Import Folder',
accelerator: 'Ctrl+F',
click: importFolder
},
{
type: 'separator',
},
{
label: 'Quit App',
accelerator: 'Ctrl+X',
role: 'quit'
}
]
},
{
label: 'Audio',
submenu: [
{
label: 'Mute Track',
type: 'checkbox',
accelerator: 'Ctrl+M',
click: muteTrack
},
{
label: 'Loop Track',
type: 'checkbox',
accelerator: 'Ctrl+L',
click: loopTrack
},
{
label: 'Playback Rate',
submenu:[
{
label: 'Normal Speed',
accelerator: 'Ctrl+Shift+1',
type: 'radio',
click: normalPlaybackRate
},
{
label: 'Half Speed',
accelerator: 'Ctrl+Shift+2',
type: 'radio',
click: halfPlaybackRate
},
{
label: 'Faster Speed',
accelerator: 'Ctrl+Shift+3',
type: 'radio',
click: fasterPlaybackRate
},
]
}
]
},
{
label: 'Media',
submenu:[
{
label: 'View Track Metadata',
accelerator: 'Ctrl+Shift+M',
click: viewTrackMetadata
},
{
label: 'View Playlist',
accelerator: 'Ctrl+Shift+L',
click: viewPlaylist
},
{
label: 'Clear Playlist',
accelerator: 'Ctrl+Shift+D',
click: clearPlaylist
}
]
},
{
label: 'Window',
submenu:[
{
label: 'Toggle Fullscreen',
accelerator: 'F11',
role: 'togglefullscreen'
},
]
},
{
label: 'About',
submenu:[
{
label: 'About App',
accelerator: 'F9',
click: openAboutAppDialog
},
{
label: 'Contact Me',
accelerator: 'F8',
click: openContactDialog
},
]
}
]
ipcMain.on('current-track-list', (event, playlist) => {
playlistInfo = playlist
})
ipcMain.on('current-track-metadata', (event, metadata) => {
metadataInfo = metadata
})
// ipcMain.on('play-selected-file', (event) => {
// let selectedFile = null
// if(process.platform == 'win32' && process.argv.length >= 2){
// selectedFile = process.argv[1]
// selectedFileExtension = selectedFile.split('.')[1]
// if(selectedFileExtension === 'mp3'){
// fp.props(selectedFile).then((properties) => {
// event.returnValue = { song: selectedFile, metadata: properties }
// }).catch(err => {throw err})
// }
// }
// })
//console.log('length: ', process.argv.length, 'contents:', process.argv)
ipcMain.on('queued-to-playlist', (event) => {
if(!isNewTrackQueued){
dialog.showMessageBox(mainWindow, {
type: 'info',
title: 'Reminder',
message: `Queued to playlist`,
})
.then((response) => console.log(response))
.catch((err) => {
throw err
})
isNewTrackQueued = true
}
})
function viewPlaylist(){
if(playlistInfo !== null){
const songs = playlistInfo.map((track) => { return track.metadata })
dialog.showMessageBox(mainWindow, {
type: 'info',
title: 'Playlist',
message: `${playlistInfo.length} Track(s) Imported`,
detail: `
${songs.map((song, index) => metadataInfo.fileName === song.fileName ? 1 + index + '. ' + song.fileName + ' ⏸ (currently playing)' + '\n\n' : 1 + index + '. ' + song.fileName + '\n\n' )}
`
})
.then((response) => console.log(response))
.catch((err) => {
throw err
})
}else{
dialog.showMessageBox(mainWindow, {
type: 'error',
title: 'Playlist',
message: 'No playlist found',
detail: 'Make sure you import tracks to the player to view the playlist'
})
.then((response) => console.log(response))
.catch((err) => {
throw err
})
}
}
function viewTrackMetadata(){
if(metadataInfo !== null){
if(metadataInfo.isVideo){
dialog.showMessageBox(mainWindow, {
type: 'info',
title: 'Video Track Metadata Information',
message: `${metadataInfo.fileName}`,
detail: `
Resolution: ${metadataInfo.resolution ? metadataInfo.resolution : metadataInfo.dimensions}
Frame-Rate: ${metadataInfo.frameRatePretty ? metadataInfo.frameRatePretty : 'N/A'}
Duration: ${metadataInfo.durationPretty ? metadataInfo.durationPretty : 'N/A'}
Ratio: ${metadataInfo.ratio ? metadataInfo.ratio : 'N/A'}
Size: ${metadataInfo.sizePretty ? metadataInfo.sizePretty : 'N/A'}
Mime-Type: ${metadataInfo.mimeType ? metadataInfo.mimeType : 'N/A'}
Bitrate: ${metadataInfo.bitRatePretty ? metadataInfo.bitRatePretty : 'N/A'}
Created: ${metadataInfo.createdRelative ? metadataInfo.createdRelative : 'N/A'}
Modified: ${metadataInfo.modifiedRelative ? metadataInfo.modifiedRelative : 'N/A'}
`
}).then((response) => console.log(response))
.catch((err) => {
throw err
})
}else{
dialog.showMessageBox(mainWindow, {
type: 'info',
title: 'Track Metadata Information',
message: `${metadataInfo.fileName}`,
detail: `
Artist: ${metadataInfo.artist ? metadataInfo.artist : 'N/A'}
Title: ${metadataInfo.title ? metadataInfo.title : 'N/A'}
Album: ${metadataInfo.album ? metadataInfo.album : 'N/A'}
Genre: ${metadataInfo.genre ? metadataInfo.genre : 'N/A'}
Composer: ${metadataInfo.composer ? metadataInfo.composer : 'N/A'}
Year: ${metadataInfo.year ? metadataInfo.year : 'N/A'}
Bitrate: ${metadataInfo.bitRatePretty ? metadataInfo.bitRatePretty : 'N/A'}
Channels: ${metadataInfo.channels ? metadataInfo.channels : 'N/A'}
`
}).then((response) => console.log(response))
.catch((err) => {
throw err
})
}
}else{
dialog.showMessageBox(mainWindow, {
type: 'error',
title: 'Track Metadata Information',
message: 'No metadata found',
detail: 'Make sure you load the song to the player to view its metadata'
})
.then((response) => console.log(response))
.catch((err) => {
throw err
})
}
}
function openContactDialog(){
dialog.showMessageBox(mainWindow, {
type: 'info',
title: 'Contact Me',
message: 'https://hbfl3x.netlify.app',
detail: `
Email me: hbfl3x@gmail.com, happybanda@dyuni.ac.mw
Phone: 0982272003
Github: https://github.com/HBFLEX
Twitter: https://twitter.com/HB_FL3X
`
})
.then((response) => console.log(response))
.catch((err) => {
throw err
})
}
function openAboutAppDialog(){
dialog.showMessageBox(mainWindow, {
type: 'info',
title: 'About SpacePlayer🪐',
message: 'Version 1.5.2',
detail: 'Developed by Happy Banda @Space-Labs-Tech, All rights reserved.'
})
.then((response) => console.log(response))
.catch((err) => {
throw err
})
}
function clearPlaylist(){
metadataInfo = null
playlistInfo = null
mainWindow.title = "SpacePlayer🪐"
mainWindow.webContents.send('clear-playlist')
}
function normalPlaybackRate(menuItem, mainWindow){
mainWindow.webContents.send('playNormalRate')
}
function halfPlaybackRate(menuItem, mainWindow){
mainWindow.webContents.send('playHalfRate')
}
function fasterPlaybackRate(menuItem, mainWindow){
mainWindow.webContents.send('playFasterRate')
}
function loopTrack(menuItem, mainWindow){
menuItem.checked ? mainWindow.webContents.send('loop-track') : mainWindow.webContents.send('unloop-track')
}
function muteTrack(){
mainWindow.webContents.setAudioMuted(!mainWindow.webContents.isAudioMuted())
}
function importSong(){
dialog.showOpenDialog(mainWindow, {
title: 'Import Media',
buttonLabel: 'Import',
properties: ['openFile', 'multiSelections'],
filters: [ { name: '*.mp3, *.ogg, *.wav, *.mp4, *.webm,', extensions: ['mp3', 'ogg', 'wav', 'mp4', 'webm'] } ],
}).then(songs => {
// read every single file in a directory and send them
// to the renderer process
songs.filePaths.forEach((song) => {
fp.props(song).then((properties) => {
mainWindow.webContents.send('imported-song', { song: song, metadata: properties, totalTracks: songs.filePaths })
}).catch(err => console.log(err))
})
}).catch(err => {throw err})
}
function importFolder(){
dialog.showOpenDialog(mainWindow, {
title: 'Import folder',
buttonLabel: 'Import Folder',
properties: ['openDirectory'],
}).then(folder => {
// read every single file in a directory except non-mp3 files and send them
// to the renderer process
fs.readdir(folder.filePaths[0], (err, files) => {
if(err) throw err
const songs = files.filter((song) => {
const ext = song.split('.')[1].toLowerCase()
if(ext === 'mp3' || ext === 'mp4' || ext === 'wav' || ext === 'ogg' || ext === 'webm') return song
})
songs.forEach((audioFile) => {
const songUrl = folder.filePaths[0] + '\\' + audioFile
fp.props(songUrl).then((properties) => {
mainWindow.webContents.send('imported-song', { song: songUrl, metadata: properties })
}).catch(err => {throw err})
})
})
}).catch(err => console.log(err))
}
ipcMain.on('currentPlayingTrack', (event, trackInfo) => {
mainWindow.title = 'SpacePlayer🪐 - ' + trackInfo.metadata.fileName
})
ipcMain.on('dragDroppedFiles', (event, files) => {
files.forEach((file) => {
fp.props(file).then((properties) => {
mainWindow.webContents.send('imported-song', { song: file, metadata: properties })
}).catch(err => {throw err})
})
})
ipcMain.on('play-selected-track', (event) =>{
if(process.argv.length >= 2){
fp.props(process.argv[1]).then((properties) => {
event.returnValue = { song: process.argv[1], metadata: properties }
})
}
})
const gotSingleInstanceLock = app.requestSingleInstanceLock()
// check if the app got a single lock or if it contains multiple window instances
// if it doesnt which apply that there is no single lock, quit the other window
// instance, otherwise get process arguments from the second window instance which
// is closed and pass it to the first instance window, focus the main window then
// get the process arguments. The process arguments returns an array this time with
// different values coming thru, loop the array check if a value ends with an extension
// of .mp3 , if it does send it to the renderer process to finally play the selected song.
if(!gotSingleInstanceLock) app.quit()
else{
app.on('second-instance', (_, argv) => {
if(app.hasSingleInstanceLock()){
if(mainWindow?.isMinimized()) mainWindow?.restore()
mainWindow.focus()
process.argv = argv
const argvArr = process.argv
argvArr.forEach((value) => {
if(path.extname(value) === '.mp3' || path.extname(value) === '.mp4' || path.extname(value) === '.wav' || path.extname(value) === '.ogg' || path.extname(value) === '.webm'){
fp.props(value).then((properties) => {
mainWindow.webContents.send('new-selected-track', { song: value, metadata: properties })
}).catch(err => { throw err })
}
})
}
})
}
const createAppWindow = (options = {}, fileName) => {
let win
win = new BrowserWindow(options)
win.loadURL(url.format({
pathname: path.join(__dirname, 'src', fileName),
protocol: 'file:',
slashes: true
}))
//win.webContents.openDevTools()
win.once('ready-to-show', () => {
loadingWindow.hide()
loadingWindow.close()
win.show()
})
win.on('closed', () => win = null)
return win
}
function createLoadingWindow(){
loadingWindow = new BrowserWindow({
frame: false,
width: 900,
height: 500,
})
loadingWindow.loadURL(url.format({
pathname: path.join(__dirname, 'src', 'loading.html'),
protocol: 'file:',
slashes: true
}))
loadingWindow.on('closed', () => loadingWindow = null)
}
// set app custom model Id
app.setAppUserModelId('SpaceLabsTech.SpacePlayer🪐')
app.on('ready', () => {
createLoadingWindow()
Menu.setApplicationMenu(Menu.buildFromTemplate(menuTemplate))
mainWindow = createAppWindow({
show: false,
backgroundColor: '#FFF',
autoHideMenuBar: true,
width: 1000,
height: 600,
minWidth: 900,
minHeight: 600,
icon: path.join(__dirname, 'src', 'static', 'images', 'player_icon.ico'),
webPreferences: {
nodeIntegration: true,
contextIsolation: false,
enableRemoteModule: true
}
}, 'app.html')
// initialize the remote module
require('@electron/remote/main').enable(mainWindow.webContents)
mainWindow.webContents.send('info',{ argvCount: process.argv.length, argv: process.argv })
})
app.on('window-all-closed', () => app.quit())
// Most of the code is self descriptive, i tried to implement the most readable
// variable and function names but if you feel lost always ask me on my email what
// most of the code is all about. GO AHEAD AND CONTRIBUTE TO THIS PROJECT ASTRONAUT 🚀