【发布时间】:2022-05-12 23:43:31
【问题描述】:
我在 Spyder IDE 中使用 Playsound 和 gtts 来创建 mp3 文件然后播放它。
import gtts
from playsound import playsound
#pass text to gTTS object
# make request to google to get synthesis
english = gtts.gTTS("Hello world") #retrieved the actual audio speech from the API
# save the audio file
english.save("hello.mp3")
# play the audio file
playsound("hello.mp3")
第一次播放很好,但每次运行时都会显示:
File "C:\Users\ASUS\.spyder-py3\all codes\Text2Speech.py", line 27, in <module>
english.save("hello.mp3")
File "C:\Users\ASUS\AppData\Local\Programs\Python\Python39\Lib\site-packages\gtts\tts.py", line 312, in save
with open(str(savefile), 'wb') as f:
PermissionError: [Errno 13] Permission denied: 'hello.mp3'
我重新启动 Spyder 后它再次正常工作,然后只玩一次后再次无法正常工作。我的操作系统是 Windows 10,Playsound 版本 1.2.2
【问题讨论】:
-
很难说没有看到完整的回溯,但我的猜测是
playsound没有关闭文件句柄,所以english.save下次失败。 -
感谢您的反馈。我只是在回溯部分添加了更多行。如果因为
playsound没有关闭而发生这种情况,如何解决该问题? -
第一次运行代码时,您会得到
Error 263 for command: close hello.mp3 Device is not open or not recognized by MCI. Failed to close the file: hello.mp3的输出,这表明 playsound 没有关闭文件。因此,下一次,GTTS 不能再写入它了。我个人的经验是playsound不是一个成熟的库。它会做所有无法真正控制的外部事务。 -
我也不推荐
pydub,因为它需要安装FFMPEG的副本。我也不推荐Mpg123,因为它需要安装libmpg123。我之前用pygame没问题,但它不能播放GTTS的MP3文件。是的,用 Python 播放声音真的很糟糕。
标签: python python-3.x spyder gtts python-playsound