Make Spencer listen and repeat
For the ultimate coding challenge, let's make Spencer listen to what we say and repeat that same sentence with his funny robotic voice.
We can just continue with the code you have made in the previous chapter for this one.
Firstly, delete the "synthesize and say" block by right-clicking on it and pressing the delete option.
So now we need the "start listening" block.
Place it here:
With this code, every time you press its red button, Spencer is going to check if WiFi is connected, play the loading animation, and start listening to what you say.
When Spencer is done listening, let's play an animation so that we know it's now processing our voice command.
Now find the "when speech gets processed" event block. This block will be triggered once Spencer processes your voice command and receives a transcript from the server.
Drag and drop it onto the drawing area:
Since we want Spencer to repeat what you've said to him, let's find the "Synthesize and say" block:
Place it here since we want to make Spencer repeat your words as soon as your voice gets processed:
We want Spencer to say the exact thing that you have said. That's why you need to take the red "intentResult->transcript" value block and drop it into the
"synthesize and say" block.
Finally, let's play a talking animation when Spencer's voice sample gets generated and make him wink once he's done talking.
Awesome, your code is done now.
Save it and run it on your Spencer.
Smack Spencer's head and tell him something (i.e., "Hello Spencer, you are my friend"). Spencer will process your voice command and repeat it with his robotic voice.