0

我正在使用 pycharm 并安装了 pyaudio 和 pyttsx3。我的问题是我的文本到语音的音频没有播放。我可以看到文本被放在控制台中,所以我知道其他一切都有效。这是与 tts 相关的代码。

    import pyttsx3 as tts

    import speech_recognition
    import wikipedia
    from neuralintents import GenericAssistant
    import requests
    import pyaudio
    
    recognizer = speech_recognition.Recognizer()
    
    speaker = tts.init()
    speaker.setProperty("rate", 150)
    speaker.setProperty("volume", 1)
4

2 回答 2

0

您必须安装 2 个库

对于 Mac 或 Linux pip3 install gtts pip3 install playsound

对于 Windows pip install gtts pip install playsound

from gtts import gTTS
from playsound import playsound


def voicePlay(string):

    myobj = gTTS(text=string, lang='en', slow=False)
    myobj.save("tts.mp3")
    playsound('tts.mp3')


voicePlay("string")
于 2021-07-17T03:40:55.687 回答
0

试试这个它对我有用

如果您有 WINDOWS 操作系统,请先安装这个:-pip install pyttsx3或者如果您有 MAC 操作系统:-pip3 install pyttsx3

import pyttsx3

def voicePlay(string):

    engine = pyttsx3.init()
    engine.say(f"{string}") 
    try:
        engine.runAndWait()
    except Exception as e:
        pass
    engine.runAndWait()
于 2021-07-16T05:06:02.673 回答