简体   繁体   English

无法在 python 中使用 gTTS 模块

[英]Not able to use gTTS module in python

I tried using gTTS module in python for text to speech.我尝试在 python 中使用 gTTS 模块进行文本到语音转换。 However, when I run the code, I am not able to hear anything.但是,当我运行代码时,我听不到任何声音。 I referred to https://pypi.org/project/gTTS/ for the installation and documentation.我参考了https://pypi.org/project/gTTS/的安装和文档。

(I am using Ubuntu) (我使用的是 Ubuntu)

My code:我的代码:

from gtts import gTTS
tts = gTTS('hello')
tts.save('hello.mp3')

I am not getting any errors.我没有收到任何错误。 However, I am not able to hear anything.但是,我什么也听不到。

gTTS only converts your text to speech and saves it. gTTS 只会将您的文本转换为语音并保存。 You have to use another module like playsound or use an audio player to play your audio file.您必须使用其他模块,例如 playsound 或使用音频播放器来播放您的音频文件。 For example:例如:

from gtts import gTTS
import playsound

tts = gTTS(text='hello', lang='en')
tts.save("hello.mp3")
playsound.playsound("hello.mp3")

gTTS only converts your text to speech and saves it with the help of "tts" API. gTTS 仅将您的文本转换为语音并在“tts”API 的帮助下进行保存。 You have to use another module like playsound to directly play your audio file.您必须使用另一个模块(如 playsound)来直接播放您的音频文件。 For example:例如:

from gtts import gTTS
import playsound
import os

tts = gTTS(text='hello', lang='en')
tts.save("hello.mp3")
playsound.playsound("hello.mp3")
os.remove("hello.mp3")

like this you can add modules in your current library you can run the codes first pip install gTTS像这样你可以在你当前的库中添加模块你可以先运行代码 pip 安装 gTTS

then然后

you can run pip install playsound你可以运行 pip 安装 playsound

commands then restart your IDE by clicking file->restart ide option命令然后通过单击文件-> 重新启动 ide 选项重新启动 IDE

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM