gTTS direct output

老子叫甜甜 提交于 2019-12-22 18:32:12

问题


I want to make a chatbot's response in audio and text.

All the example code using gTTS seem like one needs to 'save the text into a file then play the file'.

Is there another way to simplify the process such as, play the 'response from chatbot' automatically, using gTTS?


回答1:


If you look even briefly at the docs, you'll see that, of the three examples, only one of them requires you to call save, and the third one is specifically called "Playing sound directly".

So, just do exactly what's in that example, but substitute your string in place of the literal '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)

But notice that gTTS doesn't come with an MP3 player; you need a separate audio library to play that mp3_fp buffer:

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

As the docs say, there are many such libraries, and Stack Overflow is not a good place to get recommendations for libraries. I happen to have a library installed, named musicplayer, and a sample app that can be easily adapted here, but it's probably not the simplest one by a long shot (it's made for doing more powerful, low-level stuff):

>>> 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


来源:https://stackoverflow.com/questions/51164040/gtts-direct-output

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!