【问题标题】:python winsound.playsound unable to find my sound filepython winsound.playsound 找不到我的声音文件
【发布时间】:2020-12-21 03:49:26
【问题描述】:

我一直在尝试为我正在制作的游戏添加一些音乐。我一直在尝试使用 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:\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

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

【问题讨论】:

    标签: python windows audio winsound


    【解决方案1】:

    对我来说,它适用于 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:\Users\61490\Documents\Python\pano tiles\test1.wav', 
        winsound.SND_ALIAS
    )
    
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-16
      • 1970-01-01
      • 1970-01-01
      • 2012-09-22
      • 2013-05-22
      相关资源
      最近更新 更多