指定的设备未打开或未被 MCI 识别

The specified device is not open or is not recognized by MCI

我正在使用 Python 编写一个游戏,需要播放音效,所以我使用了 playsound 模块:

from playsound import playsound

playsound("Typing.wav", False)

当我尝试 运行 程序时,返回了这个错误:

Error 263 for command:
        open Typing.wav
    The specified device is not open or is not recognized by MCI.

我做了一些研究,一些消息来源表明这是我的声音驱动程序的问题。我更新并重新安装了它,但问题仍然存在。有什么办法可以解决吗?

我认为 PlaySound 不支持 .wav 文件。尝试将 Typing.wav 转换为 mp3 文件。然后改

playsound("Typing.wav", False)

进入

playsound("Typing.mp3", False)

尝试降级到 Python 3.7 或 3.8

几个月前,我在一个项目中成功使用了 playsound,但今天在 Python 3.9 虚拟环境中重新访问它时,我 运行 遇到了与 OP 相同的错误。降级到 Python 3.7 或 3.8 venv 可以解决问题。

我知道这感觉像是一个廉价的答案,我也不喜欢它,尤其是 playsoundCI system explicitly does a build for Python 3.9 on Windows, Linux, and Mac。如果其他人更深入地了解 为什么 playsound 似乎在 Python >3.8 中不起作用,我很想听听!

我遇到了同样的问题并使用

修复了它
audio_file = os.path.dirname(__file__) + 'audio.mp3'
playsound(audio_file)

正如之前评论中提到的,我也首先遇到了这个问题我将我的 python 版本从 3.10 降级到 3.7,但问题仍然存在。 所以实际起作用的是,最新版本的 playsound 给出了这样的错误,以便修复此 运行 cmd 中的以下命令作为 admin

pip uninstall playsound

pip install playsound==1.2.2

这应该可以完成工作。

以防万一不起作用,请尝试将您的 python 版本降级到 3.7 和 运行 这些命令,这应该是好的。

请看我的回答here: 问题在于 playsound() 处理文件路径的方式。它需要一个仅使用正斜杠的完整路径名。希望它在后续版本中变得更加便携。

**以管理员身份命令 运行

  1. pip 卸载 playsound
  2. pip install playsound==1.2.2

** Pycharm

中的终端
  1. pip 卸载 playsound
  2. pip install playsound==1.2.2

这对我有用:

from pathlib import Path

from playsound import playsound

audio = Path().cwd() / "audio.mp3"
playsound(audio)

只需使用 playsound2 即可。一切都一样,除了这个库没有错误。