Welcome back.
And this video, I will show you the fundamentals of text to speech and how to implement it with Kotlin
for your Android apps, and what you can see here is that we have this little screen with a little text
here and an added text in which we can enter a text.
And once we click on the button below, you can see that text speech is executed.
OK, so text speech is pretty good in English and this will, of course, only work if text to speech
is installed on your phone.
And let's check it up.
What we need is, of course, a little UI to test it.
So I have to text you then have an edited text in which I can enter a text and then I have this button.
Right.
And the idea is that whatever is entered in the edit text will be spoken once we press the button.
Now going to our main activity where all the magic happens.
And if you want to use text to speech, what is important is that we extend our main activity with the
text to speech on in a listener doing so, as you can see.
You can do that by adding a comma and then.
The listener that you want to add, so now our whole main activity will also follow the structure of
that listener and it will need to implement the on in init function.
OK, so it won't work without this on.
And you can very easily test that once you get rid of it.
You can see that the main activity wants to add the members so wants us to implement the members, for
example.
They're on in it.
OK.
And of course I'm going to bring it back here because we're going to need it.
All right.
Now, the first thing we need to do is to create a variable, which is our text to speech variable.
And I'm going to call that one Tietz standing for text to speech.
It's another.
And I'm going to set that now because I'll initialize it in our uncreate method.
You can see that our text to speech variable here will be assigned a text to speech object and which
we need to parse the context and to listen so you can say this involved because the context is the main
activity.
So it should be spoken in the main activity.
And at the same time, the main activity is also the listener.
And this year is only possible because it is actually an on in IT listener here.
So otherwise our main activity couldn't be the listener, OK?
That's why it's important to understand that.
OK, then we have this button, a speaker button, and once we click on it, I want to check if there
is a text in the edit text.
So if there's no added text, if it's empty, then I want to show a toast, which is just going to say
enter a text to use Tietz and otherwise call this Speakout method in which you pass in a text.
So what I'm doing is I'm getting the added text text and making a string out of it and passing it to
the Speakout method.
Now let's look at the Speakout method.
The speak out method, what it does is it calls the speak method of tweets so so the text to speech
class, it just goes to speak method, the speak method.
It needs to text that it should speak then the Q type.
So text to speech.
Q Flush is what I'm using.
So this will just stop the other element that was already in the queue and will start speaking.
We can test that real quickly.
This is as if I think it twice and I want to see the old entry is getting deleted and all of the new
entries there so it's not queued up, but it's actually flushed.
And this is what I'm using with Cue Flush.
You can use different types here as well.
You can add kieu or use to cue add, which will just add things up.
So if you have multiple takes, the speeches after another at will call them in a row.
So let's check that out.
OK, so I did the test and I want to see if it work.
This is a test and I want to see if you see it multiple times after another.
And it's constantly.
Spoke to text, so that's what happens when you use cue, add and cue flush, of course, gets rid of
the old entry and only speaks to most currently called one.
All right.
If you want to know more about the speak method, you can check it out here.
It's rather interesting.
So it's a speech to text using the specified queuing strategy and speech parameters.
The text may be spent with it spent.
So if you want to have some time in between, this method is asynchronous.
So what happens is that the method just adds the request to the queue of text to speech requests and
then returns.
The synthesis might not have finished or even started at the time when this method returns.
In order to reliably detect errors during synthesis, we recommend setting utterance progress listeners.
So this is going to be a little bit too advanced.
We're not going to require that.
But if you really want to dig deeper into it, then that's definitely something that you should look
into on how to use this on utterance progress listener.
And what's really interesting for us is actually the text, the Q Mode.
Well, you can add some parameters for the request, but it can be null as well.
And the utterance ID if you want to have a unique identifier for this particular request and you can
pass that in as well.
So.
Now, that's our speak out method and let's go to the on inhered method, because that's the one that
is required.
It's really important.
We need to override that.
And what happens here is that we get a status because we know on in it, if there is an on, then this
is something that will be called automatically and not by us.
So we get a status which is of type integer so we can check if the status is equal to text to speech
success.
So if text to speech can be used, then this one will be true.
So the statement here and we can go ahead and set the language.
So here I'm setting it to English.
So I stored the result of whatever.
Is the result of this setting, so something could go wrong here, studying the language could go wrong,
for example, they could not be installed or it could be missing.
So we're checking next is if the result is actually language data missing or if the result is text to
speech, language not supported.
So, for example, if the language is not supported by the phone or if it's just not installed, then
this will not work.
So then we, in our case, just log something so you could, of course, inform the user rather than
tell them to install text to speech once he presses on the text of speech button.
But we're going to keep it simple here.
It's really just a very basic demo.
So you can see I'm just adding this log entry here, saying the language specified is not supported.
And then we have this El's statement here as well, which is for this status comparison.
So if text to speech was unsuccessful from the start, then you can already see, OK, initialization
failed.
So something else might have come up or something else is an issue or creates an issue.
OK, so let's on in it now let's look at on destroy on this Driscol just before our application will
shut down, we're just before this activity will shut down.
And what I want to do is I want to.
See if the Texas peach object is empty or not, if it's not empty, then stop it and then shut it down.
And then call super undestroyed, what we do is we make sure that our program closes correctly and shuts
down correctly without leaving any fragments of our text to speech in the background or things like
that.
OK, so that's it.
You can see it's rather basic, actually.
So there's not that much that you need to implement in order to use text to speech.
It's a very powerful tool and it's rather easy to implement and I really like that.
So I it's great if you have something like that where you really need to write very little code and
Android is taking care of everything itself, which is amazing.
And we're going to use text to speech, of course, in our application in our seven minute workout app.
So I hope you enjoy that and see you in the next video.