winsound
— Sound-playing interface for Windows¶
Thewinsound
module provides access to the basic sound-playing machineryprovided by Windows platforms. It includes functions and several constants.
- winsound.Beep(frequency,duration)¶
Beep the PC’s speaker. Thefrequency parameter specifies frequency, in hertz,of the sound, and must be in the range 37 through 32,767. Thedurationparameter specifies the number of milliseconds the sound should last. If thesystem is not able to beep the speaker,
RuntimeError
is raised.
- winsound.PlaySound(sound,flags)¶
Call the underlying
PlaySound()
function from the Platform API. Thesound parameter may be a filename, a system sound alias, audio data as abytes-like object, orNone
. Itsinterpretation depends on the value offlags, which can be a bitwise ORedcombination of the constants described below. If thesound parameter isNone
, any currently playing waveform sound is stopped. If the systemindicates an error,RuntimeError
is raised.
- winsound.MessageBeep(type=MB_OK)¶
Call the underlying
MessageBeep()
function from the Platform API. Thisplays a sound as specified in the registry. Thetype argument specifies whichsound to play; possible values are-1
,MB_ICONASTERISK
,MB_ICONEXCLAMATION
,MB_ICONHAND
,MB_ICONQUESTION
, andMB_OK
, alldescribed below. The value-1
produces a “simple beep”; this is the finalfallback if a sound cannot be played otherwise. If the system indicates anerror,RuntimeError
is raised.
- winsound.SND_ALIAS¶
Thesound parameter is a sound association name from the registry. If theregistry contains no such name, play the system default sound unless
SND_NODEFAULT
is also specified. If no default sound is registered,raiseRuntimeError
. Do not use withSND_FILENAME
.All Win32 systems support at least the following; most systems support manymore:
PlaySound()
nameCorresponding Control Panel Sound name
'SystemAsterisk'
Asterisk
'SystemExclamation'
Exclamation
'SystemExit'
Exit Windows
'SystemHand'
Critical Stop
'SystemQuestion'
Question
For example:
importwinsound# Play Windows exit sound.winsound.PlaySound("SystemExit",winsound.SND_ALIAS)# Probably play Windows default sound, if any is registered (because# "*" probably isn't the registered name of any sound).winsound.PlaySound("*",winsound.SND_ALIAS)
- winsound.SND_LOOP¶
Play the sound repeatedly. The
SND_ASYNC
flag must also be used toavoid blocking. Cannot be used withSND_MEMORY
.
- winsound.SND_MEMORY¶
Thesound parameter to
PlaySound()
is a memory image of a WAV file, as abytes-like object.Note
This module does not support playing from a memory image asynchronously, so acombination of this flag and
SND_ASYNC
will raiseRuntimeError
.
- winsound.SND_PURGE¶
Stop playing all instances of the specified sound.
Note
This flag is not supported on modern Windows platforms.
- winsound.SND_ASYNC¶
Return immediately, allowing sounds to play asynchronously.
- winsound.SND_NODEFAULT¶
If the specified sound cannot be found, do not play the system default sound.
- winsound.SND_NOSTOP¶
Do not interrupt sounds currently playing.
- winsound.SND_NOWAIT¶
Return immediately if the sound driver is busy.
Note
This flag is not supported on modern Windows platforms.
- winsound.SND_APPLICATION¶
Thesound parameter is an application-specific alias in the registry.This flag can be combined with the
SND_ALIAS
flagto specify an application-defined sound alias.
- winsound.SND_SENTRY¶
Triggers a SoundSentry event when the sound is played.
Added in version 3.14.
- winsound.SND_SYNC¶
The sound is played synchronously. This is the default behavior.
Added in version 3.14.
- winsound.SND_SYSTEM¶
Assign the sound to the audio session for system notification sounds.
Added in version 3.14.
- winsound.MB_ICONASTERISK¶
Play the
SystemDefault
sound.
- winsound.MB_ICONEXCLAMATION¶
Play the
SystemExclamation
sound.
- winsound.MB_ICONHAND¶
Play the
SystemHand
sound.
- winsound.MB_ICONQUESTION¶
Play the
SystemQuestion
sound.
- winsound.MB_OK¶
Play the
SystemDefault
sound.
- winsound.MB_ICONERROR¶
Play the
SystemHand
sound.Added in version 3.14.
- winsound.MB_ICONINFORMATION¶
Play the
SystemDefault
sound.Added in version 3.14.
- winsound.MB_ICONSTOP¶
Play the
SystemHand
sound.Added in version 3.14.
- winsound.MB_ICONWARNING¶
Play the
SystemExclamation
sound.Added in version 3.14.