Debian Codegraph @

plyer/platforms/android/tts.py

raw symbols SCIP
1 from time import sleep
2 from jnius import autoclass
3 from plyer.facades import TTS
4 from plyer.platforms.android import activity
5
6 Locale = autoclass('java.util.Locale')
7 TextToSpeech = autoclass('android.speech.tts.TextToSpeech')
8
9
10 class AndroidTextToSpeech(TTS):
11 def _speak(self, **kwargs):
12 tts = TextToSpeech(activity, None)
13
14 tts.setLanguage(Locale.US)
15
16 retries = 0 # First try rarely succeeds due to some timing issue
17 message = kwargs.get('message')
18
19 # first try for while loop
20 speak_status = tts.speak(
21 message, TextToSpeech.QUEUE_FLUSH, None
22 )
23
24 # -1 indicates error. Let's wait and then try again
25 while retries < 100 and speak_status == -1:
26 sleep(0.1)
27 retries += 1
28 speak_status = tts.speak(
29 message, TextToSpeech.QUEUE_FLUSH, None
30 )
31
32
33 def instance():
34 return AndroidTextToSpeech()