python winsound.playsound 无法找到我的声音文件

python winsound.playsound unable to find my sound file

我一直在尝试为我制作的游戏添加一些音乐。我一直在尝试使用 winsound,因为它允许您在播放中途停止声音。问题是 winsound 似乎无法找到我的声音文件。

我试过使用不同的模块,例如 playsound 模块,它可以很好地播放我的音乐,但 winsound 出于某种原因不能。不幸的是,我不能只在我的游戏中使用 playsound 模块,因为它没有提供在游戏中途停止声音的方法。

这是我尝试过的方法:

#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:\Users490\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:\Users490\Documents\Python\pano tiles\test1.wav', winsound.SND_FILENAME) 
#This also only plays the windows default error sound

有人知道这是为什么吗?

对我来说,它可以像您尝试的那样与 winsound.SND_FILENAME 一起使用,但我知道您可以将 SND_FILENAME 标记替换为 SND_ALIAS 并且它应该可以工作,但是由于其他方法对你不起作用,它可能仍然不起作用。

所以它应该给你这个:

import winsound

# From a local directory
winsound.PlaySound('test1.wav', winsound.SND_ALIAS)

# Form a global directory
winsound.PlaySound(
    r'C:\Users490\Documents\Python\pano tiles\test1.wav', 
    winsound.SND_ALIAS
)

因此,如果它不能确保您已正确安装库或