-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreminderSoundNotification.js
More file actions
111 lines (95 loc) · 2.65 KB
/
reminderSoundNotification.js
File metadata and controls
111 lines (95 loc) · 2.65 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
import chalk from "chalk";
import { spawn } from "child_process";
import fs from "fs";
import path from "path";
import { fileURLToPath } from "url";
class ReminderSound {
constructor() {
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
this.defaultSoundPath = path.join(__dirname, "sounds", "notification.wav"); // Sound Effect by Devrinta Rose Nataya from Pixabay
}
/**
* Play sound using platform-specific methods with enhanced error handling but only support .wav audio format
*/
playSound() {
if (!fs.existsSync(this.defaultSoundPath)) {
console.error(
chalk.red(`Sound file not found: ${this.defaultSoundPath}`)
);
return;
}
const platform = process.platform;
try {
switch (platform) {
case "win32":
this._playSoundWindows();
break;
case "darwin":
this._playSoundMacOS();
break;
case "linux":
this._playSoundLinux();
break;
default:
this._playSoundFallback();
}
} catch (error) {
console.error(chalk.red(`Sound playback error: ${error.message}`));
}
}
_playSoundWindows() {
try {
spawn("powershell", [
"-c",
`(New-Object Media.SoundPlayer '${this.defaultSoundPath}').PlaySync()`,
]);
} catch (error) {
console.warn(
chalk.yellow("PowerShell sound playback failed. Trying alternative...")
);
this._windowsFallback();
}
}
_windowsFallback() {
try {
// Fallback to Windows Media Player command-line
spawn("wmplayer", [this.defaultSoundPath]);
} catch {
console.warn(chalk.yellow("Windows sound playback alternatives failed."));
}
}
_playSoundMacOS() {
try {
spawn("afplay", [this.defaultSoundPath]);
} catch (error) {
console.warn(chalk.yellow("afplay failed. Trying alternative..."));
this._macOSFallback();
}
}
_macOSFallback() {
try {
spawn("say", ["-v", "Alex", "Notification"]);
} catch {
console.warn(chalk.yellow("macOS sound playback alternatives failed."));
}
}
_playSoundLinux() {
const soundCommands = ["mpv", "aplay", ]; // sudo apt-get install mpv aplay
for (const command of soundCommands) {
try {
spawn(command, [this.defaultSoundPath]);
return;
} catch {}
}
console.warn(chalk.yellow("No Linux sound playback method found."));
}
_playSoundFallback() {
console.warn(
chalk.yellow(
`Sound playback not supported on platform: ${process.platform}`
)
);
}
}
export default new ReminderSound();