如何暂停、恢复和停止 pyttsx3 说话?
How to pause, resume and stop pyttsx3 from speaking?
有什么方法可以暂停、恢复和停止 pyttsx3 说话吗?
我尝试了很多方法,但找不到解决方案。
代码如下:
from tkinter import *
import pyttsx3
import threading
root = Tk()
def read():
engine.say(text.get(1.0 , END))
engine.runAndWait()
def stop():
# Code to stop pyttsx3 from speaking
pass
def pause():
# Code to pause pyttsx3
pass
def unpause():
# Code to unpause pyttsx3
pass
engine = pyttsx3.init()
text = Text(width = 65 , height = 20 , font = "consolas 14")
text.pack()
text.insert(END , "This is a text widget\n"*10)
read_button = Button(root , text = "Read aloud" , command = lambda: threading.Thread(target=read, daemon=True).start())
read_button.pack(pady = 20)
pause_button = Button(root , text = "Pause" , command = lambda: threading.Thread(target=pause , daemon = True).start())
pause_button.pack()
unpause_button = Button(root , text = "Unpause" , command = unpause)
unpause_button.pack(pady = 20)
stop_button = Button(root , text = "Stop" , command = threading.Thread(target=stop, daemon = True).start())
stop_button.pack()
mainloop()
我想要的是通过单击按钮随时暂停、恢复和停止 pyttsx3。
有什么方法可以在 tkinter 中实现这个吗?
如果有人能帮助我就太好了。
pyttsx3
没有提供暂停和恢复功能,但是您可以通过将句子拆分成单词,然后逐字逐句地说出来来模拟这些功能。在单词之间,可以查看暂停、取消暂停或停止按钮是否被点击,并进行相应的操作:
from tkinter import *
import pyttsx3
import threading
root = Tk()
class Speaking(threading.Thread):
def __init__(self, sentence, **kw):
super().__init__(**kw)
self.words = sentence.split()
self.paused = False
def run(self):
self.running = True
while self.words and self.running:
if not self.paused:
word = self.words.pop(0)
print(word)
engine.say(word)
engine.runAndWait()
print("finished")
self.running = False
def stop(self):
self.running = False
def pause(self):
self.paused = True
def resume(self):
self.paused = False
speak = None
def read():
global speak
if speak is None or not speak.running:
speak = Speaking(text.get(1.0, END), daemon=True)
speak.start()
def stop():
global speak
if speak:
speak.stop()
speak = None
def pause():
if speak:
speak.pause()
def unpause():
if speak:
speak.resume()
engine = pyttsx3.init()
text = Text(width=65, height=20, font="consolas 14")
text.pack()
text.insert(END, "This is a text widget\n"*10)
read_button = Button(root, text="Read aloud", command=read)
read_button.pack(pady=20)
pause_button = Button(root, text="Pause", command=pause)
pause_button.pack()
unpause_button = Button(root, text="Unpause", command=unpause)
unpause_button.pack(pady=20)
stop_button = Button(root, text="Stop", command=stop)
stop_button.pack()
mainloop()
更新:使用 pygame
作为播放器:
from tkinter import *
import pyttsx3
import pygame
pygame.mixer.init()
engine = pyttsx3.init()
root = Tk()
def read():
outfile = "temp.wav"
engine.save_to_file(text.get('1.0', END), outfile)
engine.runAndWait()
pygame.mixer.music.load(outfile)
pygame.mixer.music.play()
def stop():
pygame.mixer.music.stop()
def pause():
pygame.mixer.music.pause()
def unpause():
pygame.mixer.music.unpause()
text = Text(width=65, height=20, font="consolas 14")
text.pack()
text.insert(END, "This is a text widget\n"*10)
read_button = Button(root, text="Read aloud", command=read)
read_button.pack(pady=20)
pause_button = Button(root, text="Pause", command=pause)
pause_button.pack()
unpause_button = Button(root, text="Unpause", command=unpause)
unpause_button.pack(pady=20)
stop_button = Button(root, text="Stop", command=stop)
stop_button.pack()
mainloop()
有什么方法可以暂停、恢复和停止 pyttsx3 说话吗?
我尝试了很多方法,但找不到解决方案。
代码如下:
from tkinter import *
import pyttsx3
import threading
root = Tk()
def read():
engine.say(text.get(1.0 , END))
engine.runAndWait()
def stop():
# Code to stop pyttsx3 from speaking
pass
def pause():
# Code to pause pyttsx3
pass
def unpause():
# Code to unpause pyttsx3
pass
engine = pyttsx3.init()
text = Text(width = 65 , height = 20 , font = "consolas 14")
text.pack()
text.insert(END , "This is a text widget\n"*10)
read_button = Button(root , text = "Read aloud" , command = lambda: threading.Thread(target=read, daemon=True).start())
read_button.pack(pady = 20)
pause_button = Button(root , text = "Pause" , command = lambda: threading.Thread(target=pause , daemon = True).start())
pause_button.pack()
unpause_button = Button(root , text = "Unpause" , command = unpause)
unpause_button.pack(pady = 20)
stop_button = Button(root , text = "Stop" , command = threading.Thread(target=stop, daemon = True).start())
stop_button.pack()
mainloop()
我想要的是通过单击按钮随时暂停、恢复和停止 pyttsx3。
有什么方法可以在 tkinter 中实现这个吗?
如果有人能帮助我就太好了。
pyttsx3
没有提供暂停和恢复功能,但是您可以通过将句子拆分成单词,然后逐字逐句地说出来来模拟这些功能。在单词之间,可以查看暂停、取消暂停或停止按钮是否被点击,并进行相应的操作:
from tkinter import *
import pyttsx3
import threading
root = Tk()
class Speaking(threading.Thread):
def __init__(self, sentence, **kw):
super().__init__(**kw)
self.words = sentence.split()
self.paused = False
def run(self):
self.running = True
while self.words and self.running:
if not self.paused:
word = self.words.pop(0)
print(word)
engine.say(word)
engine.runAndWait()
print("finished")
self.running = False
def stop(self):
self.running = False
def pause(self):
self.paused = True
def resume(self):
self.paused = False
speak = None
def read():
global speak
if speak is None or not speak.running:
speak = Speaking(text.get(1.0, END), daemon=True)
speak.start()
def stop():
global speak
if speak:
speak.stop()
speak = None
def pause():
if speak:
speak.pause()
def unpause():
if speak:
speak.resume()
engine = pyttsx3.init()
text = Text(width=65, height=20, font="consolas 14")
text.pack()
text.insert(END, "This is a text widget\n"*10)
read_button = Button(root, text="Read aloud", command=read)
read_button.pack(pady=20)
pause_button = Button(root, text="Pause", command=pause)
pause_button.pack()
unpause_button = Button(root, text="Unpause", command=unpause)
unpause_button.pack(pady=20)
stop_button = Button(root, text="Stop", command=stop)
stop_button.pack()
mainloop()
更新:使用 pygame
作为播放器:
from tkinter import *
import pyttsx3
import pygame
pygame.mixer.init()
engine = pyttsx3.init()
root = Tk()
def read():
outfile = "temp.wav"
engine.save_to_file(text.get('1.0', END), outfile)
engine.runAndWait()
pygame.mixer.music.load(outfile)
pygame.mixer.music.play()
def stop():
pygame.mixer.music.stop()
def pause():
pygame.mixer.music.pause()
def unpause():
pygame.mixer.music.unpause()
text = Text(width=65, height=20, font="consolas 14")
text.pack()
text.insert(END, "This is a text widget\n"*10)
read_button = Button(root, text="Read aloud", command=read)
read_button.pack(pady=20)
pause_button = Button(root, text="Pause", command=pause)
pause_button.pack()
unpause_button = Button(root, text="Unpause", command=unpause)
unpause_button.pack(pady=20)
stop_button = Button(root, text="Stop", command=stop)
stop_button.pack()
mainloop()