0

我正在尝试使用 gTTS 将文本转换为语音。

import subprocess
from gtts import gTTS

mytext = 'Hello Rasa Bot User, I am a Bot'
language ='en'
myobj = gTTS(text = mytext, lang=language)
myobj.save("welcome.mp3")
subprocess.call(['mpg321',"welcome.mp3",'--play-and-exit'])

但我似乎听不到任何声音。我在 Ubuntu 中执行此操作并使用 PyCharm。终端内容如下:

(venv) rome@rome-VirtualBox:~/Desktop/rasa/intr2$ python ttos.py
mpg321: unrecognized option '--play-and-exit'
High Performance MPEG 1.0/2.0/2.5 Audio Player for Layer 1, 2, and 3.
Version 0.3.2-1 (2012/03/25). Written and copyrights by Joe Drew,
now maintained by Nanakos Chrysostomos and others.
Uses code from various people. See 'README' for more!
THIS SOFTWARE COMES WITH ABSOLUTELY NO WARRANTY! USE AT YOUR OWN RISK!

Playing MPEG stream from welcome.mp3 ...
MPEG 2.0 layer III, 32 kbit/s, 24000 Hz mono

请帮忙 !!

4

2 回答 2

1

您可以使用 pygame 库来播放音频。

下面是使用 gtts 库播放音频的代码:

from gtts import gTTS
from pygame import mixer #to play the audio
text="Hello Rasa Bot User! I am a Bot"
tts = gTTS(text=text, lang="en")
tts.save("temp.mp3") # save the audio in a temp file 
mixer.music.load('temp.mp3')# load the audio file
mixer.music.play()
于 2020-09-03T06:53:17.653 回答
0

你好,我想我可以帮助你

From gtts import gTTS
Import playsound 

Text= "hello world"
tts =gTTS(text=text , lang="en")
tts.save("hello.mp3")
playsound=playsound("hello.mp3")
于 2021-04-21T12:36:20.960 回答