Welcome back in this video, we're going to implement a sound or a media player that will run a little
sound once the exercise is done, so we know OK, now we can have a pulse, so it will basically be
the pause media player sound, so to speak, because we have a sound.
Once the exercise starts, but we have no sound to tell the user that it's done because that's usually
what you need when you, well, are in the flow of doing the exercise because you don't want to look
at your phone all the time, whether it's rest time or not, you want to have a audio cue there.
All right, so let's implement it.
Therefore, inside of the exercise activity, because that's where I'm going to play it, I'm going
to require a player which will be a media player object and it will be another ball.
And therefore we need to import media player as well as in import it.
And this will add this import right here and then we can go ahead and use it.
So I'm going to play the sound once we are setting up the rest of you.
OK.
So let me also just quickly get out of this here.
All right.
So set up riskview.
So here we are, setting up all of our bindings, and that's where I'm going to set up a sound.
So here I'm going to try and set up a sound, you or I, which will use a sound that I have in my resources.
OK, so you can use you are either pass here, which will or you're only converted.
And then you are.
I don't pass like so we don't need these extra brackets around it.
You are either pass like this.
All right.
And now we can pass in the string of where we have the you are right.
So where we have the audio file and unfortunately, we cannot just pass an idea here.
We can, however, get the resource from our raw folder.
So let's go ahead and create a roll folder which will contain things such as audio files, for example.
So if you create a new directory and you call it raw and then inside of this raw folder, that's where
I'm going to have this press start MP three audio file.
OK, so it will you can check it out.
You can also use any other audio file that you want to use for the user to you here.
Once the situation comes up.
So what do we need here?
Well, let's also, by the way, add the catch blocks so that we don't get errors here, and I'm going
to add the exception.
And here I'm just going to print it on the stack trace.
OK, so now with the war I pass, we need to use our Your I string, which means the folder where you
can find that so it will be on the resource.
Colon forward slash forward slash and then our directory here.
So the name of this folder, OK, so in my case, it is EU law tutorial start a seven minutes.
Work out app.
OK, so this is going to what is that even containing?
It's just a seven minutes workout.
OK.
And then slash.
And here we can now use the resource directory instead of its under resources.
So our dot roll dot press start.
So that's the file.
And this is just where it is stored.
OK, so we need to pass in the entire string, which is quite a long string.
So let me put that into a second line so we can read it a little better.
So basically, we are saying, OK, parse this, you are a file and store it inside of this variable
called sound your eye, which is just going to be a string.
So unfortunately, jumped over, yet there it is.
OK, so now that we have the audio file that we want to run, we can use it with our player.
So our player will use the media player class to call the create method, which needs to have an context.
And here we're going to use the application context as well as the location so that you are right.
And guess what?
This sound your eye is of tap your eye because we are parsing it.
So we're passing the location and we're can now pass this sound to you.
Are I to it?
OK, so now we can use our player, which is another bow and we can set a couple of settings.
So, for example, I don't want to loop this sample.
That's why I'm going to set it to.
Is looping equals false because otherwise the sampled loop and loop a loop, and we don't want that.
And then I will use that in order to play the sound.
So in order to play it, I'm going to use this start method.
OK, so what is going to happen now?
Each time that we get set up, Crestview, we are going to play that sound.
Even though, of course, you could put this code here into the create method and it would just exist
there.
That would be an alternative way than you would have to create this every single time, and you would
just have to play it in here.
But as this is in the try block and we have this catch statement, let's just leave it there because
even starting could cause errors.
So let's actually try this and see if there will be a sound playing once we are going over to the rest
of you.
So let's run it.
And in your case, I think you will not hear it, let me unplug my headphones.
And there was so let me go again to let it in an OK once again, that it it it.
OK.
All right.
So I'm just doing this because I have a directional microphone in it otherwise would pick up the sound.
OK, but the sound works.
So we have the audio now, and that is amazing.
So we can now use it.
And let me just check.
My own destroy.
Because whenever we create these kind of things, such as media players and everything that can cause
this kind of while leakages, we need to also destroy them in the on destroying method here.
So if it's not moral, let's also get rid of the player so we can do that by just calling the stop method.
OK, so this will stop our player.
All right.
So this will just ensure that we don't have no leakages.
But other than that, I think we're pretty much golden.
So our application is now ready and it plays a sound.
So now you know how to play sounds, but away with a media player, it's pretty useful as well.
And I'd say, see you in the next video.