| 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 |
| 17 |
message = kwargs.get('message') |
| 18 |
|
| 19 |
|
| 20 |
speak_status = tts.speak( |
| 21 |
message, TextToSpeech.QUEUE_FLUSH, None |
| 22 |
) |
| 23 |
|
| 24 |
|
| 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() |