Skip to content Skip to sidebar Skip to footer

Playing Sound From A Specific Location In Python Tkinter

goal To play a wav file from the location D:1.wav, when the application is started up by the user research Saw the following questions: How would I go about playing an alarm sound

Solution 1:

Change

winsound.PlaySound('D:\1.wav',winsound.SND_FILENAME)

to

winsound.PlaySound('D:\\1.wav',winsound.SND_FILENAME)

to prevent python from escaping your path:

>>>a = '\1.wav'>>>a
'\x01.wav'

winsound.SND_FILENAME

The sound parameter is the name of a WAV file. Do not use with SND_ALIAS.

(From winsound docs)

If you don't use also the flag winsound.SND_NODEFAULT, winsound plays the default sound if it cannot find your specified file.

Create a folder (say D:\test_sounds\) with your wav file inside, add that folder to your PYTHONPATH variable and try running your code again. Or (better, if you plan to distribute your code), following the same post I just linked, add this to your code:

import sys
if"D:\\my_sound_folder" not in sys.path:
    sys.path.append("D:\\my_sound_folder")

and then you can just call winsound.PlaySOund('1.wav', winsound.SND_FILENAME) since it will be in your available paths

Post a Comment for "Playing Sound From A Specific Location In Python Tkinter"