Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

python3

What is winsound.PlaySound() in Python 3?

Hammad Nasir

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Overview

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)

Parameters

  • sound is an object that specifies the soundThis can be a file name, a built-in sound name, or a sound-like object in memory that is to be played. The following table summarizes the built-in sounds in every Windows platform:
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, then flags must include the winsound.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.

Return value

  • PlaySound returns None.

Example

The following code provides an example of how to use the PlaySound function:

import winsound
winsoud.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.

RELATED TAGS

python3

CONTRIBUTOR

Hammad Nasir
Copyright ©2022 Educative, Inc. All rights reserved

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring