1

前段时间我做了一个小项目,最近我觉得给它添加一些声音会很酷。所以我寻找在 Python 3.x 中播放声音的方法,并Playsound得到了很好的评价。我有这样的设置:我有文件夹Python Projects,里面有Sound Test但是当我尝试播放我的音频文件(test.wav)时,它会抛出以下错误:

Traceback (most recent call last):
  File "soundtest.py", line 2, in <module>
    playsound('test.wav')
  File "/Users/rhett/env/lib/python3.7/site-packages/playsound.py", line 67, in _playsoundOSX
    raise IOError('Unable to load sound named: ' + sound)
OSError: Unable to load sound named: file:///Users/rhett/Desktop/Python Projects/Sound Test/test.wav

我尝试使用直接路径,例如:

from playsound import playsound

playsound(/Users/Rhett/Desktop/Python\ Projects/Sound\ Test/test.wav)

我收到了完全相同的错误:

Traceback (most recent call last):
  File "Sound Test/soundtest.py", line 2, in <module>
    playsound("/Users/Rhett/Desktop/Python\ Projects/Sound\ Test/test.wav")
  File "/Users/rhett/env/lib/python3.7/site-packages/playsound.py", line 67, in _playsoundOSX
    raise IOError('Unable to load sound named: ' + sound)
OSError: Unable to load sound named: file:///Users/Rhett/Desktop/Python\ Projects/Sound\ Test/test.wav
4

3 回答 3

6

我找到了问题所在。在python3.7/site-packages/playsound.py文件中 开发人员没有检查文件路径中的“ ”空格,因此文件路径中的空格字符会造成麻烦。

无需更改代码的快速修复playsound.py。将您的文件夹名称替换 ~/Desktop/Python Projects/Sound Test/test.wav~/Desktop/PythonProjects/SoundTest/test.wav

(即,从文件夹名称中删除空格)

这将解决您的错误。

于 2019-02-08T14:16:24.293 回答
3

这使它可以工作...

s_musicfile = "/Users/xxxxxxxxxx/Desktop/play this file.mp3"

s_musicfile = s_musicfile.replace(" ", "%20")

playsound(s_musicfile)
于 2020-08-14T21:50:07.930 回答
0

我认为这会奏效,尤其是当它用于 raspberry 时:

蟒蛇2:

sudo apt install python-gst-1.0

蟒蛇 3:

sudo apt install python3-gst-1.0
于 2022-01-17T10:27:59.170 回答