Skip to content

Commit 9d9d665

Browse files
committed
Update SoundHelper.cs
1 parent 5bc6659 commit 9d9d665

File tree

1 file changed

+88
-13
lines changed

1 file changed

+88
-13
lines changed

src/ManagedShell.Common/Helpers/SoundHelper.cs

Lines changed: 88 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,94 @@ public class SoundHelper
99
{
1010
private const string SYSTEM_SOUND_ROOT_KEY = @"AppEvents\Schemes\Apps";
1111

12+
/// <summary>
13+
/// Flag values for playing the sound.
14+
/// </summary>
1215
[Flags]
13-
private enum PlaySoundFlags
16+
public enum SND : uint
1417
{
15-
SND_ASYNC = 0x00000001,
16-
SND_NODEFAULT = 0x00000002,
17-
SND_ALIAS = 0x00010000,
18-
SND_FILENAME = 0x00020000,
19-
SND_SYSTEM = 0x00200000,
18+
/// <summary>
19+
/// The sound is played synchronously; PlaySound returns after the sound event completes (default behavior).
20+
/// </summary>
21+
SYNC = 0x0000,
22+
23+
/// <summary>
24+
/// The sound is played asynchronously; PlaySound returns immediately after initiating the sound.
25+
/// To stop an asynchronously played sound, call PlaySound with pszSound set to NULL.
26+
/// </summary>
27+
ASYNC = 0x00000001,
28+
29+
/// <summary>
30+
/// No default sound event is used. If the sound is not found, PlaySound returns without playing a sound.
31+
/// </summary>
32+
NODEFAULT = 0x00000002,
33+
34+
/// <summary>
35+
/// The pszSound parameter points to a sound loaded in memory.
36+
/// </summary>
37+
MEMORY = 0x00000004,
38+
39+
/// <summary>
40+
/// The sound plays repeatedly until PlaySound is called with pszSound set to NULL.
41+
/// Use the ASYNC flag with LOOP.
42+
/// </summary>
43+
LOOP = 0x00000008,
44+
45+
/// <summary>
46+
/// The specified sound event will yield to another sound event already playing in the same process.
47+
/// If the required resource is busy, the function returns immediately without playing the sound.
48+
/// </summary>
49+
NOSTOP = 0x00000010,
50+
51+
/// <summary>
52+
/// The pszSound parameter is a system-event alias from the registry or WIN.INI file.
53+
/// Do not use with FILENAME or RESOURCE.
54+
/// </summary>
55+
ALIAS = 0x00010000,
56+
57+
/// <summary>
58+
/// The pszSound parameter is a predefined identifier for a system-event alias.
59+
/// </summary>
60+
ALIAS_ID = 0x00110000,
61+
62+
/// <summary>
63+
/// The pszSound parameter is a file name. If the file is not found, the default sound is played unless NODEFAULT is set.
64+
/// </summary>
65+
FILENAME = 0x00020000,
66+
67+
/// <summary>
68+
/// The pszSound parameter is a resource identifier; hmod must identify the instance that contains the resource.
69+
/// </summary>
70+
RESOURCE = 0x00040004,
71+
72+
/// <summary>
73+
/// The pszSound parameter is an application-specific alias in the registry.
74+
/// Can be combined with ALIAS or ALIAS_ID to specify an application-defined sound alias.
75+
/// </summary>
76+
APPLICATION = 0x00000080,
77+
78+
/// <summary>
79+
/// Requires Windows Vista or later. If set, triggers a SoundSentry event when the sound is played,
80+
/// providing a visual cue for accessibility.
81+
/// </summary>
82+
SENTRY = 0x00080000,
83+
84+
/// <summary>
85+
/// Treats the sound as a ring from a communications app.
86+
/// </summary>
87+
RING = 0x00100000,
88+
89+
/// <summary>
90+
/// Requires Windows Vista or later. If set, the sound is assigned to the audio session for system notification sounds,
91+
/// allowing control via the system volume slider. Otherwise, it is assigned to the application's default audio session.
92+
/// </summary>
93+
SYSTEM = 0x00200000,
2094
}
2195

22-
private const PlaySoundFlags DEFAULT_SYSTEM_SOUND_FLAGS = PlaySoundFlags.SND_ASYNC | PlaySoundFlags.SND_NODEFAULT | PlaySoundFlags.SND_SYSTEM;
96+
private const SND DEFAULT_SYSTEM_SOUND_FLAGS = SND.ASYNC | SND.NODEFAULT | SND.SYSTEM;
2397

2498
[DllImport("winmm.dll", SetLastError = true, CharSet = CharSet.Auto)]
25-
private static extern bool PlaySound(string pszSound, IntPtr hmod, PlaySoundFlags soundFlags);
99+
public static extern bool PlaySound(string pszSound, IntPtr hmod, SND soundFlags);
26100

27101
/// <summary>
28102
/// Plays the specified system sound using the audio session for system notification sounds.
@@ -47,7 +121,7 @@ public static bool PlaySystemSound(string app, string name)
47121
return false;
48122
}
49123

50-
return PlaySound(soundFileName, IntPtr.Zero, DEFAULT_SYSTEM_SOUND_FLAGS | PlaySoundFlags.SND_FILENAME);
124+
return PlaySound(soundFileName, IntPtr.Zero, DEFAULT_SYSTEM_SOUND_FLAGS | SND.FILENAME);
51125
}
52126
catch (Exception e)
53127
{
@@ -60,15 +134,16 @@ public static bool PlaySystemSound(string app, string name)
60134
/// Plays the specified system sound using the audio session for system notification sounds.
61135
/// </summary>
62136
/// <param name="alias">The name of the system sound for ".Default" to play.</param>
63-
public static void PlaySystemSound(string alias)
137+
public static bool PlaySystemSound(string alias)
64138
{
65139
try
66140
{
67-
PlaySound(alias, IntPtr.Zero, DEFAULT_SYSTEM_SOUND_FLAGS | PlaySoundFlags.SND_ALIAS);
141+
return PlaySound(alias, IntPtr.Zero, DEFAULT_SYSTEM_SOUND_FLAGS | SND.ALIAS);
68142
}
69143
catch (Exception e)
70144
{
71-
ShellLogger.Error($"SoundHelper: Unable to play sound {alias}: {e.Message}");
145+
ShellLogger.Debug($"SoundHelper: Unable to play sound {alias}: {e.Message}");
146+
return false;
72147
}
73148
}
74149

@@ -82,7 +157,7 @@ public static void PlayNotificationSound()
82157
if (EnvironmentHelper.IsWindows8OrBetter)
83158
{
84159
// Toast notification sound.
85-
if (!PlaySystemSound(".Default", "Notification.Default"))
160+
if (!PlaySystemSound("Notification.Default"))
86161
PlayXPNotificationSound();
87162
}
88163
else

0 commit comments

Comments
 (0)