Welcome back and this video, we are going to use a very, very powerful feature that is super cool
and that you can reuse for so many different applications and that is going to be text to speech.
So we are going to implement text to speech, which means that we will have some text that will be spoken
by our application.
And the text that should be spoken in this case is the exercise name.
So the idea is that once the user gets to the next exercise, the next exercises name will be spoken
out so that the user will know which exercise to do.
So he doesn't have to look at his phone all the time, but he will just hear the name and will understand,
OK, that's the next exercise that I need to do.
So let's just have a look at this.
We started our application and we can see jumping jacks should be the next exercise.
So I'm going to shut up until jumping jacks is spoken out.
Jumping jacks, so there we are.
It was spoken out, we heard it jumping jacks that was spoken by our application.
So that's text speech that we're using here.
So in order to implement text to speech, we need to do a couple of things.
So first of all, we go to our exercise activity.
And if you want to implement such things as text to speech or other cool little features that are super
powerful, sometimes you just need to implement that as an additional listener.
So in this case, I'm going to say and actually let me stop the application in the background.
So I'm going to say that it should not only implement the app compared activity or inherit from it,
but it should also implement text to speech.
So there is this class called text to speech, and it has something called on in it listener.
So if you want to know more about our listener, you can see it here, interface definition of callback
to be invoked, indicating the completion of the text to speech engine and liberalizations.
And once we implement this, you can see that it's complaining because we also need to implement members.
So it says that we also need to implement this public abstract fun on it.
And yeah, let's just do that.
Let's implement this member by clicking this button.
You can see then the screen pops up where it says, OK, it's from text to speech that on in a listener
and the function is called on in it.
And yeah, that's just go ahead and implement that so you can see to do not implement it.
So currently there is nothing happening when we initialize this screen.
So this will be called once the screen is initialized.
So once our exercise activity is initialized and that is also the time where we need to set everything
up.
So here in on in it, we will need to set up our text to speech.
But before we do that, we need to create the variable for it.
So I'm going to create a new object here.
Private VAR Teets is I'm going to call it, and that stands for text to speech, which should be of
type text to speech nullable.
So text the speech, Nullable, and I'm going to set that to now because I can't initialize it directly
here.
So where would be a great spot to initialize it?
Well, I'd say in uncreate.
So once our screen is loaded, let's just set our text to speech in here and I'm going to do it up here.
So Tietz is going to be text to speech.
So it's going to be an object of type text to speech with this as its context and this as its listener.
Why does that work?
Well, the thing is, our activity can be the context of something, as we've seen, for example, with
the toaster.
And here we do the same thing.
We say, okay, this class or this activity is going to be the context for this text to speech.
So in this context, the text to speech should be used or created or played.
The sound should be played in this activity, particularly, or in particular.
And then we have the listener that we also need to pass.
And the cool thing is we can say this because.
Our exercise activity is conform with the inner listener, so it is the listener, so it already knows
how to be a listener because we implemented it and that's why we can use this activity as the listener.
Now we have this new method that we implemented or function this on in it, but we haven't used it yet
or we didn't do anything in there.
This is not a function that we call manually, but it's something that is called by the system.
And on in it is called When We Initialize.
So if you want to know more about it, as it seems.
There are no declarations that I can use for this, what we need to do here is to check if we actually
have access to text to speech.
So it's possible that text to speech is not installed or is not installed for the language that the
user is using on his phone.
So what we need to check is if we actually can access text to speech.
All right.
So if status is equal, equal text to speech, thought success.
So that's what we can do if text to speech works.
So what I'm going to say here is that the result is I'm creating a new variable here.
Result is going to be text to speech language.
And for now, I'm just going to say the local should be us.
So I want to use.
English as the language here.
And then we can check if the result is equal to text to speech.
But Lang, missing data or the result is text to speech.
Lang.
Not support it.
So these are two options of success.
You know, it's a successful operation and you can see the alternative is error, and then there's also
one that is called stop.
So the thing is, even though we might not have the data installed or the language is not supported,
but it can still from a success.
So text to speech can still say, OK, text, the speech works on this device, but for some reason
the language is not installed.
So the US language is not installed or the data is missing for whatever reason.
So if that is the case, what shall we do then?
Then we're just going to write a long statement here, so I'm just going to use this statement here
and we need to import Android util log.
So I'm going to say the tech is outstanding for text to speech, and the message should be the language
specified is not supported.
So this will only be visible here in the Lockette once we run this application.
It's not going to be visible on the phone itself when we test this.
But this is just for testing purposes here that what we want to do here, of course, we could write
a message to the user language not supported or something like that.
But for now, we're just going to keep it simple and just have it in the lock.
But overall, you can see if the status text to speech does work.
But we want to do is we want to set the language to us.
All right, or to English in this case, and then if for whatever reason this language doesn't work,
then we want to write something onto the log.
Now, what do we want to do if text to speech does not have the success of flag?
So if the status is not success but for example, error or stop or something like that, well, then
we just want to print something like initialization failed.
So I'm just going to say, OK, Titos initialization failed.
All right, so that's the initialization meaning, that's the preparation for Texas speech.
Now we can go ahead and use text to speech so we can just go ahead and say something.
And the easiest way to do so is probably to create our own function to which we can pass a text and
then it will use text to speech.
So that's what I'm going to do here.
I'm going to create a private function called Speak Out, and it's just going to require a text of type
string and then it should just go ahead.
And speak this text.
So we can just pass text here and then you can see there are other things that we need to pass on the
Kumo parents bundle utterance I.D. So let's just give the second entry here, which is text to speech,
dot, cue flush.
And then the next parents that we want to pass, well, we don't have any specific parameters that we
want to pass.
I'm just going to say no.
And the other side is going to be an empty string.
So you can see sometimes you don't need to pass anything and it's still going to work.
So in this case, speak if we look at it, it needs a child sequence text.
Well, that's also a string, so Charles sequence can also be a string, then we have the commode bundle
and another string here.
If you want to know more about the individual parameters, you can see here all the details, the string
of text to be spoken no longer than get mixed speech input length.
So it cannot be an endless long text.
So there is a limitation to it.
The commode is the queuing strategy to use, so, for example, if you have multiple text speeches,
which are called just after another, without enough time for the first text to speech to be finished,
then we need to define how we want to do it.
Do we want to make a break or do we want to stop the spoken text that we had before and just go on with
a new one?
Or do we want to wait until the text is finished and so forth?
All right.
So these are the things you can play around with, of course.
And yeah, I'm just going to say let's go ahead and use speak out, because that's the text that we
want to display.
And a good position would be when we start our exercise.
So at the moment that our exercise is being.
Set up so here and set up exercise view, that's where I want to speak to text, so I'm just going to
do it just here.
And I'm going to say speak out.
And the text should be coming from the exercise list at the position current position get shape.
So it's pretty much just text you or this.
Here we have.
So what is going to do is it's going to go to the exercise list and then it's going to look OK, it's
in the list, right.
And then where does it come from?
Well, it comes from exercise model.
And there we have a name.
And if we look at the name in our concerns, it will be in the first.
Execution here, jumping jacks than a football set and so forth.
Push up, abdominal crunch and so forth.
All right, so that is where we speak our text and that is over here.
So speak out the text and our set up exercise view.
And now there's finally one thing that we may not forget, and that is to close text to speech at the
moment, that our activity is shut down because things can go wrong if you open up a channel such as
text to speech and then you don't close it before going to another screen or when going to another screen,
this can cause a bunch of problems.
So in order to avoid this, we do the same thing as we did with the timer.
We get rid of it and on destroyed, so on destroyed.
So here I'm just going to say, if text to speech is active and by the way, I can see here something
that I have not done before, which is good that we realized that here because we stopped the rest timer,
but we didn't do the same thing for the exercise timer.
So exercise progress, we said that his role as well.
Then we.
Check if Tietz is not now, so text to speech if we have text to speech active this variable, then
we want to.
Stop it.
And then we want to shut it down.
So this means even if text to speech is currently speaking while we shut down the application or this
screen, this exercise activity, we want it to stop and then we also make sure that text to speech
is completely shut down.
And we do that with Shuto.
All right, so now we can test this, we can run this, and it should be capable of playing the code
or playing the text.
So let's see, we start.
And we are jumping jacks, jumping jacks, awesome, so as you can see, it works, so it speaks the
text out and that is great.
All right, so now you see how you can use text to speech in order to do so.
Just as a little summary, we need to have this on listener that we follow, which is an interface,
and we just go ahead and implement that interface to our exercise activity.
And if we do so, it also requires two.
Or it asks us to implement this on in it, which is called once the exercise activity is initialized
and there we check if the text speech worked, if it did work, then we need to set its language.
And if it didn't work, well, then we want to write something onto the log.
In our case, of course, we could also inform the user about it with a dialogue or something like that,
but it's free to you to do.
Then we need to have the method where we need to speak the out the text.
At one point in our case, I created this additional function here, which takes care of that.
Of course, you could have used to this line of code directly where we used to speak out.
So that's totally fine as well.
And then at one point, you, of course, need to call this and in our case we call it when we set up
our exercise view.
So here we speak it out and what we speak out while we speak out the text.
All right.
So I hope you enjoyed this.
And in the next video, we're going to add some cool new features to our SO see you there.