Hello.
YouTube.
In this video, you are going to learn how to implement text to speech in your Android apps, and that's
exactly what you're going to learn in this video.
So let's get started using text to speech with Android 12 and Caitlin.
So I have this little hello world text here, then an added text and then a button.
OK, so if we look at the code here, it's all inside of a constraint layout, text, view and edit
text and button.
Very simple.
So now in the connectivity, that's where we're going to implement the code.
So let's go ahead and set everything up that we need for it.
First of all, we need to create the text to speech object.
So I'm going to create a private VAR test, which is a shortcut for text to speech.
And you see there is this class called text to speech.
We need to import it, of course, and this will be another tool because I'm going to use it throughout
this class that I have here.
And then I'm also going to use data binding.
So let's use a binding object.
And this will be the activity main binding, which of course, only works if we have binding activated.
So let me do that real quick as well inside of my.
Build Gradle and here to build features.
Are going to be few binding being true.
All right, now, let's think it and then we could use your binding throughout our application.
All right now, activity Maine binding is portable, and let's use it.
So let's first of all, make sure that our binding object isn't going to be empty, so it will be an
activity main binding inflated with the layout inflator like so and then we can accordingly set that
as the.
Content view so binding.
Which is another bill that route.
Okay, so that's why we have this question mark here.
And then let's initialize the TSA, which is a text to speech object, which means we of course, need
to assign text to speech to it.
So here you need to assign the context as well as what is the listener.
So I'm going to say that this is also going to be the listener.
There's, of course, only works if our class is actually going to be the listener.
So let's see what actions we have here.
Let main activity implement interface text to speech on Inada listener.
That sounds great.
Let's do exactly that.
And then also implement the members and you see it added this implementation year at the top text to
speech on IT listener, as well as the override function on in it, which is called once initialized,
so called to signal the completion of the text to speech engine initialization.
That's what this does for us.
So before we use that, let's first of all, set up our button that we have, and this is the button
that has the idea of button speak and I'm going to use it using buttons.
Speak set on Click Listener here.
So once I click on this button, I want something to happen and I get a view here which I can then use,
and I can also ignore the usage of it.
But what I'm going to do is I'm going to get the enter to text, which is coming from this pad of text
here.
This added text here, and then I want to load its text.
OK, so we get the text, which is another well, so we're checking whether it's empty and if it's empty,
we're just going to write a toast saying, Hey, please enter a text.
Otherwise we can't speak anything.
So here very simple post and otherwise speak something else.
So here to do you speak?
The text.
Please.
OK, so this is where we want to speak the text.
So let's create a function which will speak to text.
All right.
So I'm going to create a private function and dysfunction will only be used in this main activity.
So I'm going to make it private, so speak out, and it should get a text that we passed to it.
And here it's going to use our text to speech to use this big method.
So here you can now enter the text that you want to speak.
Then you use the text to speech Q and we're going to flush the old stuff.
So we're going to only display the current or say the current text to speech that we want to run.
Then we pass the params and we're going to not pass any parameter here, and we need to use the utterance
ID here, which in my case is going to be an empty string.
And you see, in this case, the speak method is not going to be sliced through or crossed through because
to see if I don't have this utterance ID, it's not happy with it.
So we need to have the utterance idea, even though we can also just pass an empty string to it.
And in case you're wondering what utterance means, well, utterance is something like comment, a remark,
an expression statement, something like that.
OK, so it's it's a very well for a non-native speaker, very uncommon word.
I didn't know this word before I use text to speech.
And if you want to know more about text to speech, definitely check out.
The documentation is always useful there.
You can also find out more about every single parameter we're using.
But basically, we can either flush the current statement, which means whatever was said in the text
to speech before will be deleted.
Or we can add the new thing that we want to speak to what is already being spoken.
OK, so here there's this cue add alternative.
So you flush or queue and are the two options that you have here.
And I would recommend to try both to see how they are differing from each other.
OK, now when you are using text to speech and binding, you should use the on destroying method to
really make sure that you don't have any data leakage.
So here, super on destroying, that's great.
And if cheats is not equal now.
Then make sure.
Then make sure that you stop that it's so stop the text to speech.
So, for example, if the application should be closed, you don't want the text to speech to you.
Keep on going.
Otherwise, the user might be annoyed because he thinks, holy smokes.
This is not stopping.
It's alright if it's just a sentence.
But imagine it's a text to speech of an entire book, for example, which is not what you would use
it for.
But that would be an option, right?
And here shut it down.
So you're shutting it down and then also binding is null.
So let's make sure that our data binding is also setting off.
All right.
So now we can actually do this speaking.
So here in the last clause, let's go ahead and say, OK, we want to speak, so let's speak out.
And here, once you get the enter to text.
OK, because we know that it's not going to be empty at this point.
So let's get it and make a string out of it because you need to pass a string to our speak out method.
That's how we defined it.
OK.
All right.
Now that by itself wouldn't do the trick because we also need to make sure that we set the right language
to our text to speech because it wouldn't know.
All right.
So we can get the status, which is, by the way, test p zero.
So I'm not happy with this name because status as a more descriptive name of what it actually is.
So we are checking here whether the text to speech setting is to success.
So whether text to speech is ready.
OK.
And if that's the case, let's try to set the language of our text to speech.
And it's this.
It's another bill we're going to force on it here.
Set a language and you can use a local here and you see there are many different options.
Choose the one that fits to your language.
So if you're American, you can use us and then also use local import here.
We need to import locale.
Otherwise it won't work for us, which seems to be in the utility.
OK.
And then we can check if the result has a certain value, because if you look at the set language method,
it will return an integer which can have multiple different values.
So, for example, text to speech could be lang and you see language not supported, language available,
language country available and so forth.
So in case we have missing data or the language does not support it, we're going to throw an error,
OK, or we're going to log an error.
So result will be text to speech.
So here I'm checking the other text to speech Lang, not support it.
So if either of those is the case, then throw in a log entry.
Of course, you could also say something to the user and then otherwise, it's not success.
We also know that something went wrong, so let's lock that as well.
So we're going to say, OK, initialization failed.
So at this point, we can test our application and we can use text to speech.
So there we are, let's say hello, world first, hello, world.
And you see it comes up.
Or let's say hello.
Learners.
How's it going?
Hello, learners.
How's it going?
All right, then, if I click it multiple times, slow learner, hello learners, you see slow learners.
It's just cuts the old saying or the old speech and going just starts from the beginning.
And that is what we have achieved by using cue flush.
All right.
If we were to use add here, you add, it would look different.
So let's check it out real quick.
So again, hello, world.
Hello, world.
You see no world.
It just adds to the current queue.
And this could be annoying.
Or this could be exactly what you want.
So really depends on what you want to achieve with your text to speech application.
All right.
So this was a little introduction into text to speech.
I hope you enjoyed this video and hope to see you in the next one.