gTTS直接输出

gTTS direct output

我想在音频和文本中做出聊天机器人的响应。

所有使用 gTTS 的示例代码似乎都需要 'save the text into a file then play the file'。

是否有其他方法可以简化流程,例如使用 gTTS 自动播放 'response from chatbot'?

如果你稍微看一下 the docs,你会发现,在三个示例中,只有一个需要调用 save,而第三个专门调用 "Playing sound directly".

因此,完全按照该示例中的内容进行操作,但用您的字符串代替文字 'hello':

>>> from gtts import gTTS
>>> from io import BytesIO
>>>
>>> my_variable = 'hello' # your real code gets this from the chatbot
>>> 
>>> mp3_fp = BytesIO()
>>> tts = gTTS(my_variable, 'en')
>>> tts.write_to_fp(mp3_fp)

但请注意 gTTS 没有随附 MP3 播放器;您需要一个单独的音频库来播放 mp3_fp 缓冲区:

>>> # Load `audio_fp` as an mp3 file in
>>> # the audio library of your choice

正如文档所说,有很多这样的库,Stack Overflow 不是获取库推荐的好地方。我碰巧安装了一个名为 musicplayer 的库,以及一个可以在此处轻松调整的示例应用程序,但从长远来看,它可能不是最简单的应用程序(它是为做更强大、更底层的东西而设计的) :

>>> import musicplayer
>>> class Song:
...     def __init__(self, f):
...         self.f = f
...     def readPacket(self, size):
...         return self.f.read(size)
...     def seekRaw(self, offset, whence):
...         self.f.seek(offset, whence)
...         return f.tell()
>>> player = musicplayer.createPlayer()
>>> player.queue = [Song(mp3_fp)]
>>> player.playing = True

您也可以使用 playsound 库。

>>>import playsound

>>>playsound.playsound('sound.mp3')

有关 playsound.Visit Playsound Docs 的更多信息。

如果你想一次又一次地调用 speak 函数而没有任何错误。

基本上,这就达到了目的。

from gtts import gTTS
import os
import playsound

def speak(text):
    tts = gTTS(text=text, lang='en')

    filename = "abc.mp3"
    tts.save(filename)
    playsound.playsound(filename)
    os.remove(filename)