The winsound
module in Python 3 provides an interface to interact with the sound playing machinery in Windows. The PlaySound
function plays or stops a given WAV sound file. The syntax of the PlaySound
function is as follows:
winsound.PlaySound(sound, flags)
sound
is an object that specifies the sound string |
Corresponding control panel sound |
---|---|
'SystemAsterisk' |
Asterisk |
'SystemExclamation' |
Exclamation |
'SystemExit' |
Exit Windows |
'SystemHand' |
Critical stop |
'SystemQuestion' |
Question |
Note: If a built-in
sound
is passed, thenflags
must include thewinsound.SND_ALIAS
flag.
flags
is a parameter that takes in winsound.SND_
flags. These flags provide additional commands to the play sound function. The following table summarizes the different flags and their usage:Flag | Usage |
---|---|
winsound.SND_FILENAME |
Specifies that the sound parameter given is a WAV file name. |
winsound.SND_ALIAS |
Specifies that the sound parameter given is a built-in sound. If the built-in sound does not exists, the default system sound is played, unless winsound.SND_NODEFAULT is specified. |
winsound.SND_LOOP |
Specifies that the sound must be played repeatedly. This is a blocking call; in order to play the sound in a loop in the background, winsound.SND.ASYNC must be specified. |
winsound.SND_MEMORY |
Specifies that the sound is a byte-like object that contains data in WAV format. Using this flag together with winsound.SND_ASYNC will raise a RuntimeError . |
winsound.SND_PURGE |
Specifies that all instances of the given sound must be stopped. This flag is not supported on modern Windows platforms. |
winsound.SND_ASYNC |
Specifies that the function should return immediately, allowing for the sound to play in the background. |
winsound.SND_NODEFAULT |
Specifies that if the specified sound is not found, the system default sound must not be played. |
winsound.SND_NOSTOP |
Specifies that sounds currently playing must not be interrupted. |
winsound.SND_NOWAIT |
Specifies that the function must return if the sound driver is busy. This flag is not supported on modern Windows platforms. |
PlaySound
returns None
.The following code provides an example of how to use the PlaySound
function:
import winsoundwinsoud.PlaySound('SystemHand', winsound.SND_ALIAS)winsoud.PlaySound('abcd', winsound.SND_ALIAS)winsound.PlaySound('Fire_Alarm.WAV', winsound.SND_FILENAME)
In the example above, in the first PlaySound
function call, the Critical Stop built-in sound is played. In the second PlaySound
function call, the default built-in sound is played, since abcd
is not a built-in sound in Windows. The last PlaySound
function call passes the name of the sound file as the sound
argument. To specify that the passed sound
argument is the name of a sound file, the winsound.SND_FILENAME
flag is used. Therefore, if the Fire_Alarm.WAV
file exists, it will be played; otherwise, a beep sound will be played.