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



我一直在尝试为我制作的游戏添加一些音乐。我一直在尝试使用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:Users61490DocumentsPythonpano tilestest1.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:Users61490DocumentsPythonpano tilestest1.wav', winsound.SND_FILENAME) 
#This also only plays the windows default error sound

有人知道为什么会这样吗?

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

所以它应该给你这个:

import winsound
# From a local directory
winsound.PlaySound('test1.wav', winsound.SND_ALIAS)
# Form a global directory
winsound.PlaySound(
r'C:Users61490DocumentsPythonpano tilestest1.wav', 
winsound.SND_ALIAS
)

因此,如果它不能确保你正确安装了库或

最新更新