簡體   English   中英

如何在非主線程上運行 pyttsx3(文本到語音)? (Python)[已回答]

[英]How to run pyttsx3 (text-to-speech) on non-main thread? (Python) [Answered]

我創建了一個小型 function 將字符串轉換為語音並保存為 .mp3 文件。

這在主線程上沒有任何問題。 但是當我在第二個線程上運行這個 function 時,它不會生成任何.mp3 文件,也不會引發任何錯誤。 你知道是什么原因造成的嗎?

編輯:我意識到我從未初始化 text_to_speech.py,只運行了 function。

文本轉語音文件 function:

# text_to_speech.py

import pyttsx3

engine = pyttsx3.init()
voices = engine.getProperty('voices') 

engine.setProperty('voice', voices[3].id)
engine.runAndWait()

def tts_to_file(text_string, save_file_path):
    
    engine.save_to_file(text_string, save_file_path)
    engine.runAndWait()

主線程(作品):

# main_file.py

from text_to_speech import tts_to_file
tts_to_file("Hello world", "./test.mp3")

主線程(不起作用):

# main_file.py

from text_to_speech import tts_to_file
from threading import Thread

p = Thread(target = tts_to_file, args = ("Hello world", "./test.mp3"))
p.start()
p.join()
# This doesn't do anything and raises no errors

我意識到我從未初始化 text_to_speech.py,只運行了 function。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM