pythonwindowsaudiowinsound

python winsound.playsound unable to find my sound file


I've been trying to add some music to the game I've been making. I've been trying to use winsound because it allows you to stop a sound mid-way through playing. The problem is that winsound seems unable to locate my sound file.

I've tried using different modules such as the playsound module, which is able to play my music just fine, but winsound can't for some reason. Unfortunately I can't just use the playsound module for my game because it doesn't provide a way to stop sounds midway through playing.

Here's what I've tried:

#testing if winsound functions
import winsound
import playsound

#The file I want to play, test1.wav, is saved in the same folder as this file.

#Playing the sound using playsound, using a local directory
playsound.playsound('test1.wav') 
#this works and plays the sound as intended

#Playing the sound using playsound, using a global directory
playsound.playsound(r'C:\Users\61490\Documents\Python\pano tiles\test1.wav')
#this also works and plays the sound as intended


#Playing the sound using winsound, using a local directory
winsound.PlaySound('test1.wav', winsound.SND_FILENAME) 
#This only plays the windows default error sound

#Playing the sound using winsound, using a global directory
winsound.PlaySound(r'C:\Users\61490\Documents\Python\pano tiles\test1.wav', winsound.SND_FILENAME) 
#This also only plays the windows default error sound

Does anyone know why this might be?


Solution

  • For me, it works with the winsound.SND_FILENAME just like you tried, but I know that you can replace the SND_FILENAME tag with SND_ALIAS and it should work, but since the other way doesn't work for you it is possible that it still won't work.

    So it should give you this:

    import winsound
    
    # From a local directory
    winsound.PlaySound('test1.wav', winsound.SND_ALIAS)
    
    # Form a global directory
    winsound.PlaySound(
        r'C:\Users\61490\Documents\Python\pano tiles\test1.wav', 
        winsound.SND_ALIAS
    )
    
    

    So if it doesn't make sure that you've installed the library correctly or