-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSoundManager.js
More file actions
47 lines (38 loc) · 1.25 KB
/
SoundManager.js
File metadata and controls
47 lines (38 loc) · 1.25 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
const path = require('path');
const fs = require('fs');
const soundPlayer = require('node-wav-player');
const Logger = require('./logger');
const basePath = path.join(process.resourcesPath, 'sounds')
class SoundManager {
static getSoundPath(filename) {
return path.join(basePath, filename);
}
static async playSound(filename) {
const soundPath = this.getSoundPath(filename);
if (!fs.existsSync(soundPath)) {
const error = `Sound file not found: ${path.basename(soundPath)}`;
Logger.error(error);
throw new Error(error);
}
try {
await soundPlayer.play({
path: soundPath,
sync: true
});
Logger.info(`Successfully played: ${filename}`);
} catch (err) {
Logger.error(`Playback failed: ${err.message}`);
throw err;
}
}
static getAvailableSounds() {
try {
return fs.readdirSync(basePath)
.filter(file => path.extname(file).toLowerCase() === '.wav');
} catch (error) {
Logger.error(`Sound directory error: ${error.message}`);
return [];
}
}
}
module.exports = SoundManager;