If you’re on Windows,
winsound is a good option as it’s built into Python. Its usage is very similar to
playsound from earlier. Inside your text editor, go ahead and
filename, so for me let’s do
And then using
winsound, call the
PlaySound() method. And in here, pass in the
filename and then add in this
SND_FILENAME is just saying that the sound that you’re passing into here is a filename. So, this won’t work for me because I’m on a Mac right now, but you should get the idea.
Keep in mind that
winsound only works for WAV files, so if you’re using MP3s, it won’t work. A cool thing that
winsound can do is beep the speakers on your PC by calling the
Beep() method. Let’s just comment this out real quick.
And if you call
winsound, you can say
Beep(), and if you wanted to do a 1000 Hz tone for 100 milliseconds, you could just do that. For a project that’s going onto a PC, this can be a great way to notify a user that something has happened. Just by adding one line of code, you can add in beeps of different frequencies for different durations without needing to create your own sounds or import different filenames.
All right. In the next video, we’re going to cover
python-sounddevice, which is a NumPy array based player.
Become a Member to join the conversation.