Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
542 views
in Technique[技术] by (71.8m points)

python 3.x - pyttsx3 can't stop talking when using it in kivy

I was trying to write a really simple kivy mobile app, to listen pdfs. For the GUI I was using Kivy and for the audio I was using pyttsx3. In the app you can choose a pdf to listen and it starts talking, also from a specific page, but it doesn't stop. I've tried a lot of different codes, but anyone was working. This is my actual code:

# Kivy Imports
from kivymd.app import MDApp # It allows us to create classes for our methods
from kivy.lang import Builder # Parsing of instruction for our app

# Other Imports
import pyttsx3
import PyPDF2

speaker = pyttsx3.init()

stop_audio = False
run = True

def onWord(name, location, length, stop_audio):
   if stop_audio:
      speaker.stop()

KV = """
Screen:

    BoxLayout:
        orientation: 'vertical'
        
        BoxLayout:
            orientation: 'vertical'
        
            MDTextField:
                id: page
                hint_text: "Pagina di Partenza"
                helper_text_mode: "on_focus"
                line_color_normal: app.theme_cls.accent_color
                pos_hint: {"center_x": 0.5, "center_y": 0.6}
                size_hint_x: None
                width: 300
            
            MDIconButton:
                icon: "pause"
                theme_text_color: "Custom"
                id: pause_bt
                text_color: app.theme_cls.primary_color
                pos_hint: {"center_x": 0.4, "center_y": 0.5}
                on_press: app.pause()

        FileChooserListView:
            id: filechooser
            on_selection: app.play(self.selection[0])
    
        
            
"""

class App(MDApp):

    def build(self):
        self.title = "PDF Listener" #The Name of the App is "Safed": "Save" + "Saved"
        self.theme_cls.theme_style = "Dark" # Light
        self.theme_cls.primary_palette = "Blue"
        return Builder.load_string(KV)

    def play(self, pdf_path):
        global stop_audio, run
        if self.root.ids['page'].text != "":
            starting_page = int(self.root.ids['page'].text)-1
        else:
            starting_page = 0
        book = open(pdf_path, 'rb')
        pdfReader = PyPDF2.PdfFileReader(book)
        pages = pdfReader.numPages
        i = starting_page
        while run and i <= pages:
            speaker = pyttsx3.init()
            speaker.connect('started-word', onWord)
            page = pdfReader.getPage(i)
            text = page.extractText()
            speaker.say(text)
            speaker.runAndWait()
            i += 1
            if stop_audio:
                speaker.stop()
                run = False

        stop_audio = False

    def pause(self):
        global stop_audio
        stop_audio = True

if __name__ == "__main__":
    App().run()
question from:https://stackoverflow.com/questions/65933194/pyttsx3-cant-stop-talking-when-using-it-in-kivy

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...