Python winsound module

In this tutorial, we will introduce you to the Winsound module and its basic functionalities in Python. After this tutorial, you will be able to play the default sound and music files of your system.

What will you learn:

Playing audio files, the default sound of your system.

Introduction Of Winsound

Basically, Winsound is a module that is used for playing “.wav” music files in your system. You can use this module on windows only. For other operating systems, you can prefer the “PlaySound” module.

You don’t need to install this module. It’s preinstalled.

Note: You can play only “.wav” files. For other files, you can use “PlaySound”.

Use Of Winsound In Python

First of all, import the Winsound module. For the first method, we are going to use the Beep(). It takes two arguments frequency(37 to 32000 range) and time in milliseconds.

import winsound

#produce beep sound

Another method is the PlaySound(). This method also takes two arguments file path and flag(there are too many flags that you can use to apply conditions on file like SND_LOOP, SND_NOSTOP, etc). Here in this method, we put the first argument “*”, which will play the default sound of your system.

To check other flags, you can go here.

#play default sound of your system 
winsound.PlaySound("*", window.SND_LOOP)

In this segment of the code, we put the file path.

#play the file of your system 
winsound.PlaySound("C:\\music\\file.wav", window.SND_LOOP)

Leave a Reply

Your email address will not be published.