Sound-playing interface for Windows in Python (winsound)

The winsound module is specific to Python installations on Windows operating systems. It provides a simple interface for playing sounds and system beeps. The module defines several functions for different types of audio playback.

Beep()

When this function is called, a beep is heard from the PC's speaker. The function needs two parameters: frequency and duration. The frequency parameter specifies the frequency of the sound and must be in the range 37 through 32,767 hertz. The duration parameter specifies the duration of sound in milliseconds.

Example

import winsound

# Play a beep at 1000 Hz for 500 milliseconds
winsound.Beep(1000, 500)

MessageBeep()

This function plays a sound as specified in the Windows registry. The type argument specifies which sound to play. Possible values are:

-1, MB_ICONASTERISK, MB_ICONEXCLAMATION, MB_ICONHAND, MB_ICONQUESTION, and MB_OK (default).

The value -1 produces a "simple beep".

Example

import winsound

# Play the default system sound
winsound.MessageBeep()

# Play different system sounds
winsound.MessageBeep(winsound.MB_ICONEXCLAMATION)
winsound.MessageBeep(winsound.MB_ICONHAND)

PlaySound()

This function calls the underlying PlaySound() function from the Platform API. The function needs two parameters: sound and flags. The sound parameter may be a filename, a system sound alias, or audio data as a bytes-like object. Its interpretation depends on the value of flags.

Available Flags

Flag Description
SND_FILENAME The sound parameter is the name of a WAV file
SND_LOOP Play the sound repeatedly
SND_MEMORY The sound parameter is a memory image of a WAV file, as a bytes-like object
SND_ASYNC Return immediately, allowing sounds to play asynchronously
SND_NODEFAULT If the specified sound cannot be found, do not play the system default sound
SND_NOSTOP Do not interrupt sounds currently playing

Example

import winsound

# Play a WAV file (requires actual WAV file)
winsound.PlaySound('sample.wav', winsound.SND_FILENAME)

# Play a WAV file asynchronously
winsound.PlaySound('sample.wav', winsound.SND_FILENAME | winsound.SND_ASYNC)

Using System Sound Aliases

The Windows registry keys are associated with sound names. If the registry contains no such name, it plays the system default sound unless SND_NODEFAULT is specified. All Win32 systems support the following sound aliases:

PlaySound() Name Corresponding Control Panel Sound Name
'SystemAsterisk' Asterisk
'SystemExclamation' Exclamation
'SystemExit' Exit Windows
'SystemHand' Critical Stop
'SystemQuestion' Question

Example

import winsound

# Play Windows Exit sound
winsound.PlaySound("SystemExit", winsound.SND_ALIAS)

# Play different system sounds
winsound.PlaySound("SystemAsterisk", winsound.SND_ALIAS)
winsound.PlaySound("SystemExclamation", winsound.SND_ALIAS)

Predefined Constants

The winsound module also defines the following sound constants for use with MessageBeep():

Constant Description
MB_ICONASTERISK Play the SystemDefault sound
MB_ICONEXCLAMATION Play the SystemExclamation sound
MB_ICONHAND Play the SystemHand sound
MB_ICONQUESTION Play the SystemQuestion sound
MB_OK Play the SystemDefault sound

Conclusion

The winsound module provides a simple way to play system sounds and beeps on Windows. Use Beep() for custom frequency tones, MessageBeep() for system notification sounds, and PlaySound() for WAV files and system sound aliases.

Updated on: 2026-03-25T05:27:47+05:30

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements