news = requests.get(url).text
news_dict = json.loads(news)
arts = news_dict['articles'][:2]
for article in arts:
engine = init()
text = " ".join([article["description"] + "nnnnnn" + "pausing for the next article" + "nnnnnn" for article in arts])
engine.setProperty('rate', 185)
engine.say(text)
engine.runAndWait()
engine.endLoop()
engine.say("finished")
engine.runAndWait()
response = make_response(b'')
response.headers['Content-Type'] = 'audio/mpeg'
return response
不要因为人为的n延迟而对我大喊大叫:-),我不能让这个循环/开始/结束正确,也就是说-仍然有一个错误:
引发RuntimeError('run loop已经开始')RuntimeError: run loop already started
我已经经历了大量的例子,迭代都没有成功。
这最终工作:
def speak_text(文本):Engine = pyttsx3.init()试一试:engine.endLoop ()德尔引擎Engine = pyttsx3.init()除了:通过engine.startLoop ()引擎。setProperty("率",185)engine.say(文本)
这个设置似乎为我做了!
def speak_text(text):
engine = pyttsx3.init()
try:
engine.endLoop()
del engine
engine = pyttsx3.init()
except:
pass
engine.startLoop()
engine.setProperty('rate', 185)
engine.say(text)