Repository navigation
Expand file tree
/
Copy pathAlertSound.cs
More file actions
72 lines (62 loc) · 1.86 KB
/
Copy pathAlertSound.cs
File metadata and controls
72 lines (62 loc) · 1.86 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
using System.Diagnostics;
namespace TimeCalc;
/// <summary>
/// Plays the alarm chime with whatever audio playback the platform provides.
/// Silently does nothing if no player/sound is available.
/// </summary>
internal static class AlertSound
{
private static readonly string[] LinuxSounds =
{
"/usr/share/sounds/freedesktop/stereo/complete.oga",
"/usr/share/sounds/freedesktop/stereo/bell.oga",
"/usr/share/sounds/alsa/Front_Center.wav",
};
private static readonly string[] LinuxPlayers = { "paplay", "pw-play", "aplay" };
public static void Play()
{
try
{
if (OperatingSystem.IsWindows())
{
TryRun("powershell",
"-NoProfile -Command \"(New-Object Media.SoundPlayer 'C:\\Windows\\Media\\chimes.wav').PlaySync()\"");
return;
}
if (OperatingSystem.IsMacOS())
{
TryRun("afplay", "/System/Library/Sounds/Glass.aiff");
return;
}
var sound = LinuxSounds.FirstOrDefault(File.Exists);
if (sound is null)
return;
foreach (var player in LinuxPlayers)
if (TryRun(player, sound))
return;
}
catch
{
// no audio available — stay silent
}
}
private static bool TryRun(string command, string arguments)
{
try
{
using var process = Process.Start(new ProcessStartInfo(command, arguments)
{
UseShellExecute = false,
CreateNoWindow = true,
});
if (process is null)
return false;
process.WaitForExit();
return process.ExitCode == 0;
}
catch
{
return false;
}
}
}