This works for me. I just clicked on ‘Make Mad Lib’ and it spoke the story to me.
Interesting idea by the way.
Thanks but I’m still not getting speech. Also, I get this error in the browser after I select a voice (see attached image also): ‘text2speech’ object has no attribute ‘list_of_voice_name’
. This is triggered when I invoke: text2speech_1.set_voice(voice) as the text2speech component has this method:def set_voice(self,voice):
'''Set the voice for speech. You can pass either the Name of the voice or the Voice object itself'''
if type(voice)!=str:
self.speech.voice=voice
else:
if voice in self.list_of_voices_name:
index=self.list_of_voices_name.index(voice)
self.speech.voice=self.list_of_voices[index]
This issue may likely be caused because the voices are not loaded at that time. This is why a ‘voiceloaded’ event is available in the text2speech component. It is best if you populate your dropdown menu upon this event.
For example
def text2speech_1_voicesloaded(self, list_of_voices, list_of_voices_name, **event_args):
self.story_dropdown.items=list_of_voices_name
Just click on the text2speech component in anvil designer and set this event on voicesloaded.
Thanks much, Divyesh! So, I tried doing what you suggested. When running from Anvil, the voice drop down gets fully populated but no speech is produced. When running outside of Anvil with published version, the voice drop down does not get populated at all. Relevant code of mine:
def speak(self,thetext):
self.text2speech_1.text=thetext
print("calling text2speech_1.start()")
self.text2speech_1.start()
def voices_drop_down_change(self, **event_args):
"""This method is called when an item is selected"""
voice_chosen = self.voices_drop_down.selected_value
print("chosen voice: "+ voice_chosen)
myvoice=voice_chosen.split()[0]
if myvoice=="Google":
myvoice=voice_chosen
print(myvoice)
self.text2speech_1.set_voice(myvoice)
def text2speech_1_voicesloaded(self, list_of_voices, list_of_voices_name, **event_args):
"""This method is called when the voices are loaded. You can use it to set the voice for TTS"""
print("load voices in drop down")
self.voices_drop_down.items=list_of_voices_name
https://anvil.works/build#clone:B2WSMKVTWM4MC2DY=L2ULH6LC26XZO42ZBDXKVFH7
Can you please check out this clone link and see if this works? Because it seems to be working fine for me.
You can also check it out on this URL and see if it works.
Hi, I think this problem is solved.
Just clone this app
https://anvil.works/build#clone:B2WSMKVTWM4MC2DY=L2ULH6LC26XZO42ZBDXKVFH7
And carry forward with your app.
The reason why this was happening is that SyeechSynthesis.getVoices() returns an empty list in the beginning. However, as soon as the voices are loaded, it fires the voiceloaded event I have added here.
But on some browsers, the voices do not load on their own. However, as soon as you call the start() method, the voices are loaded. This is why I installed a timer in your app which will keep trying to call the start() and stop() method until the voices are loaded.
Hope this fixes the problem.
I will also advise you here that the voices are not the same for all browsers. Every browser will have different voices available in it. And the number can vary a lot. I get at least 30 different voices on my laptop but only a handful 10 on my mobile. And there is not a single common voice between them.
I got it working!! The timer was the key to success! Thanks for all your help, Divyesh!!!