基本上我正在尝试编写一个使用 python 打开声音文件的代码,但我得到的只是这个错误。使用 MacOS 和 Python 3.10。尝试了很多解决方案但没有成功。
这是我首先尝试过的代码:
from playsound import playsound
playsound('/Users/cairo/Desktop/i wish.mp3')
/Users/cairo/PycharmProjects/pythonProject/venv/bin/python /Users/cairo/PycharmProjects/pythonProject/main.py
playsound is relying on a python 2 subprocess. Please use `pip3 install PyObjC` if you want playsound to run more efficiently.
Traceback (most recent call last):
File "/Users/cairo/PycharmProjects/pythonProject/main.py", line 3, in <module>
playsound(r'/Users/cairo/Desktop/beep sound 2')
File "/Users/cairo/PycharmProjects/pythonProject/venv/lib/python3.10/site-packages/playsound.py", line 244, in <lambda>
playsound = lambda sound, block = True: _playsoundAnotherPython('/System/Library/Frameworks/Python.framework/Versions/2.7/bin/python', sound, block, macOS = True)
File "/Users/cairo/PycharmProjects/pythonProject/venv/lib/python3.10/site-packages/playsound.py", line 223, in _playsoundAnotherPython
raise PlaysoundException('Cannot find a sound with filename: ' + sound)
playsound.PlaysoundException: Cannot find a sound with filename: /Users/cairo/Desktop/beep sound 2
Process finished with exit code 1
我已经通过终端安装了 pip3 install PyObjC 但它一直显示错误。 似乎是什么问题?
编辑: 尝试过这个:
from pydub import AudioSegment
from pydub.playback import play
song = AudioSegment.from_wav("/Users/cairo/Desktop/i wish.mp3")
play(song)
这些是我得到的错误:
/Users/cairo/PycharmProjects/beepsound/venv/lib/python3.10/site-packages/pydub/utils.py:170: RuntimeWarning: Couldn't find ffmpeg or avconv - defaulting to ffmpeg, but may not work
warn("Couldn't find ffmpeg or avconv - defaulting to ffmpeg, but may not work", RuntimeWarning)
/Users/cairo/PycharmProjects/beepsound/venv/lib/python3.10/site-packages/pydub/utils.py:198: RuntimeWarning: Couldn't find ffprobe or avprobe - defaulting to ffprobe, but may not work
warn("Couldn't find ffprobe or avprobe - defaulting to ffprobe, but may not work", RuntimeWarning)
Traceback (most recent call last):
File "/Users/cairo/PycharmProjects/beepsound/main.py", line 4, in <module>
song = AudioSegment.from_wav("/Users/cairo/Desktop/i wish.mp3")
File "/Users/cairo/PycharmProjects/beepsound/venv/lib/python3.10/site-packages/pydub/audio_segment.py", line 808, in from_wav
return cls.from_file(file, 'wav', parameters=parameters)
File "/Users/cairo/PycharmProjects/beepsound/venv/lib/python3.10/site-packages/pydub/audio_segment.py", line 728, in from_file
info = mediainfo_json(orig_file, read_ahead_limit=read_ahead_limit)
File "/Users/cairo/PycharmProjects/beepsound/venv/lib/python3.10/site-packages/pydub/utils.py", line 274, in mediainfo_json
res = Popen(command, stdin=stdin_parameter, stdout=PIPE, stderr=PIPE)
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/subprocess.py", line 971, in __init__
self._execute_child(args, executable, preexec_fn, close_fds,
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/subprocess.py", line 1847, in _execute_child
raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: 'ffprobe'
Process finished with exit code 1
我将不胜感激任何帮助
也许 python 不是适合这项工作的 PL?
我遇到了同样的错误,并在 Mac 上解决了 - 您还需要安装 PyObjC
印有一行小字,上面写着 “playsound 依赖于 python 2 子进程。如果您希望 Playsound 更高效地运行,请使用
pip3 install PyObjC
。”
截图
如果你安装了PyObjC,它就可以成功运行。
不是问题的解决方案,而是替代方案。 我遇到了同样的问题,playsound 似乎被破坏了,因为它依赖于 python 2.7。
您可以使用 pydub 作为替代方案。
from pydub import AudioSegment
from pydub.playback import play
song = AudioSegment.from_wav("path/to/file.wav")
play(song)
我认为你应该使用 pygame 来播放声音
https://www.pygame.org/wiki/GettingStarted
pip3 install pygame
pygame.mixer.init()
pygame.mixer.music.load("file.mp3")
pygame.mixer.music.play()
免责声明:
我建议您使用
nava
代替。
您只需通过以下方式安装即可:
pip install nava
然后通过以下方式播放声音:
from nava import play
play('/Users/cairo/Desktop/i wish.mp3')