3

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 in python?

Play audio with Python

what I tried

I tried the following lines:

Example 1

import winsound
winsound.PlaySound('D:\1.wav',winsound.SND_FILENAME)  ##Did not work

Example 2

import winsound
winsound.PlaySound('1.wav',winsound.SND_FILENAME)  ##DID not work

Both the times I got the default sound but not the sound that should have played as per the audio file

also

  1. When I write winsound.PlaySound('1.wav',winsound.SND_FILENAME) where does python check for the file 1.wav? In which directory?
  2. Why is the flag winsound.SND_FILENAME used for?

specs Python 2.7 TKinter 8.5 Windows XP SP

Please help me with this issue.

Community
  • 1
  • 1
IcyFlame
  • 5,059
  • 21
  • 50
  • 74

1 Answers1

2

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

Community
  • 1
  • 1
Samuele Mattiuzzo
  • 10,760
  • 5
  • 39
  • 63
  • i know it says winsound. but i have a question(please oblige if it is too obvious). i use winsound. then i create an exe and then send it to a pc running linux: will it work?? as in will the sound play?? – IcyFlame Apr 11 '13 at 13:09
  • 1
    winsound is only for windows – Samuele Mattiuzzo Apr 11 '13 at 13:16
  • yeah. I guessed that but just wanted to be sure. And I even ran it in Linux and found out that it did not work! thanks anyway! – IcyFlame Apr 11 '13 at 16:10
  • I tried this but for me too the default sound is triggered. The wav file is in exact directory as is the py file. `winsound.PlaySound("notify.wav",winsound.SND_FILENAME)`. Am I missing something? The path where the wav and py files are are already showing in the list generating using `print(sys.path)`. – Meet Dec 16 '21 at 11:26