Hire Beaven in this story are we going to go ahead and we're going to actually upload the user's voice
or an audio to firebase.
So let's go to our main activity class here.
Let's scroll to the top here.
Now we need to actually declare somebody else inside our main activity class.
So the first one the first value that we're going to do declared inside our main activity class is going
to be actually private.
And the name of it the data type of this value is going to be media record there and the name is going
to be media recorders semicolon.
The second round of it is going to be of type string and the name of this variable is going to be audio
file and audio file semi-colon.
Now let's go to our activity under like minded exemption here so let's add a button into our constraints.
Lay out.
So inside this pallet section click on all and here select this button and to strike it here and put
it here below this image.
So now you have this image this part in here below the same as you right.
Let's create a consensus here to the bottom of this image view so we can see this little circle that
is pulsating here on the top of this button.
Let's create a constraint to the bottom edge of this image view.
Let's change the top margin to number 20 and then present until January 3rd.
So now that we have this button here selected and let's change the text of this button so the taste
of this button is going to be tap and hold tap on hold to record or you record or you and in person
interview on your keyboard.
So now let's keep an eye to this button.
So the idea here just type in DTN record audio and in person and on your keyboard.
Let's go to our main activity class and here let's declare this button here.
Inside the room in the class we're just in here private Barton Pythian PTEN record audio and then put
a semicolon here.
Let's go to our uncreate MF'er and below this line of code that actually initialises this age download
image view we can initialize our PTEN record audio variable assignment operator.
So let's cast this object to our button object find view by I-T or that Id thought PTEN record or the
semicolon.
So because we want to access the audio recorder of the user's device we need to actually ask the user
to give us the permission to access the audio recorder.
So let's open the manifest file here.
And below is a line of code that actually uses the permission for a right external storage we need to
add another permission and other users permission actually tag.
So let's open a task here and just type in user's permission and here select is fun and right that permission
that record audio and then lets self close the stack here by just putting a slash and this and in fact
let's go to our main activity here and here inside this uncreate if you must also ask the user to give
us our runtime permission to access the audio recorder.
So now we have this valuable permission result.
Let's change this the name of this Friday about to get permission result number one.
And here you can see that we have this statement so let's add another one here.
If preemption is number one is equal to package manager the permission granted.
Right.
So let's create another variable here let's say in the name of this value but it's going to be permission.
Result.
Number two right.
Assignment operator and I can say contex compact that check self permission.
And here we can say manifests.
Right.
That permission that audio recorder data record audio actually record audio semicolon.
So now after this line of code here.
So here we can see that we have this error here.
That's because we actually didn't add the context.
So the first argument must be actually the context the main activity that this comma and then B can
actually be at the permission.
So this if it checks a print machine needs two arguments the first one is going to be the context and
the second argument is going to be the permission manifests the permission that record audio.
So let me create a new line here so that you can see these values better.
And here we have this statement right.
So let's actually change the condition of this statement to a logical or actually condition to a logical
operator.
We can add this ontological operator here and we can say mission result Number two is equal to package
manager permission gratin package manager that permission granted.
So we actually created these two conditions here if Actually both of these conditions are true only
in that case it's going to go inside this state and an if statement and it's going actually for example
show this message to the user.
Thank you for giving the permission to access external storage X terminal storage and audio recorder
audio recorder.
Right.
So now let me create a new line here.
So here we can see that you have this a statement.
So if they use it does it doesn't give us the permission to for example access the audio recorder.
We must ask the user to give us their permission to access the audio recorder he had inside the statement.
So here you can see that as the second argument of the at the request permissions we have an array of
permissions right the first permission is going to be this one.
So now I want to add another permission so just put a comma here and just say manifest that permission
that record audio.
That's it.
So now we have two permissions inside our stray string.
So the request quote is number two so we can see that here right.
So let me create a new line here.
So now inside these Mefford inside is uncreate Mefford after this piece of code here the unclick listener
of the city and upload image let's initialize our file actually file valuable our file audio file valuable
actually so let's leave word that is valuable and then put an assignment operator here and here I can
refer to this environment a class that gets external storage directory get external storage directory
that get absolute path semicolon.
So we are to access the extension storage directory.
And we're going to get the absolute path of that directory.
And we're going to assign that value to the audio file of it.
And now I can't say audio file assignment operator.
Audio file.
Plus Sarlat is that this audio file variabilities of type string.
Right.
So here we are concatenating string values together and then put two quotes here.
Slash and the name of our file is going to be audio file.
Or do you find that free G.
So it is going to be there actually the format is Fregelius is going to be the format of our audio file.
Now after these two lines of code I can refer to this pretty only quote audio button that sets and click
listener knew and then hold the shift key on your keyboard and then press on the needle Vicci on your
keyboard in order to select his option and click the center.
So now the Android studio put these lines of code here for you automatically.
So inside this unclick click here I can say.
So there is a problem here not is that insight or activity underlayment minute and here we said tap
and hold through the court audio.
So we want to actually use the motion event listener instead of unclick listener OK because the user
wants to for example hold the button for some seconds.
OK.
So in that case we don't want to actually use the uncle or whomever it we are we want to use the on
touch listener method.
So let's go to our main activity class and here let's actually did it.
This piece of code here that says the unclick the Senate he had for this button.
Let's again distopian record audio that sits on touch listener and then typing.
He had a new view on Tochtli Center.
That's it.
So now this Meffert Unterschied Meffert here is actually over rated.
So not is that anonymous in a class is created here and that anonymous class is implementing these articles
in an interface and inside this Unterschied is an interface we have this untouched method that we must
over right.
So let's go inside the Smurfette here here.
And before this line of code that returns follows from the Smurfette because the return type of this
is what is Boolean actually.
So this method must return a value.
So now I can actually say if so here we can we have this value of type motion events right.
We have this argument which is of a motion event and we can refer to this argument.
If event that gets action is equal to action down motion event that action down.
This means that the user has actually tapped the button and the user is actually using the action down
and the user is holding the button down.
And the user can actually now record the audio.
So in that case we can actually say we can refer to an effect that we have not created yet we can't
say start recording audio semi-colon.
Right.
So what happens if the action is actually off.
So in that case we need to create an elusive statement.
So after the curly brace of this statement I can't say LCF event that gets action is equal to motion
event that action up.
So in that case you want to actually stop the recording so you can say stop recording.
Or do you semicolon.
So now let's actually create these two methods inside our main activity class the first one is going
to be start recording audio.
So let's scroll down here.
And before the end incredible piece of this main activity class I can say private void and the name
of this movie is going to be start recording audio and then put a bit of practice this year and then
open and close it by using the curly braces.
The second if it is going to be again private Voyt stop recording or you stop recording audio and then
put a pair of parenthesis here open and closed this method by using these curly braces.
Let's go inside this article in audio Mefford.
So here I'm going to actually paste some lines of code inside these.
Start recording audio Meffert.
So now insight is Smurfit we can actually paste these codes here.
So now you can see that these are the lines of code that I pasted here.
And the first of all I am actually initialising media recorder variable that we created above inside
the main activity class.
So the record media recorder assignment operator new media recorder right.
So you know that we are actually creating a new object of state media recorder here.
So now you can see that in line number one hundred and eighty five.
We have code media recorders that sit audio source.
So we are actually specifying the source of the audio and we have seen media recorder that audio source
that microphone a mic and he had a number one hundred an 86 media recorder that said output format media
recorders that output format that free underline GPP.
OK so let me scroll to the top here.
In order to show you that here in line number one hundred and six inside is uncreate method he had specified
that data type 3 GP here.
Right.
So you can see that the format is pretty GP and if I actually scroll down here you can see that the
output format is going to be freed Underland GPP E-Loan number of one hundred and eighty seven.
I am saying.
Media recorder that said output file audio file.
So here I am referring to that audio file that you actually initialized inside the uncreate in effect
an inline number one hundred eighty eight.
I am saying media recorder that set your encoder MediaTek recorder that audio in court that data are
underlined and so it is going to be the encoding actually the process of our video recorder here and
here we have this try catch block here.
So inside this trial like we are saying media recorder that prepared so it can prepare the media recorder.
And finally if insight is atrocious like we dont have any errors we are going to actually execute this
line of thought maybe a quarter start here so it can actually start recording the voice.
So now let's go inside is stop recording audio effect here.
Now I am going to paste some lines of code here and then I'm going to explain to you what's going on
here.
So here as you can see these are the lines of code that I pasted here.
So I created an if statement and here I am saying if media recorder is not equal to null then we can
actually go inside the curly braces of this statement.
And here we can say maybe recorder that stop media recorder of that release.
And finally we get to assign the value to this valuable audio recorder.
OK.
So after the media record that is actually stopped and the value null is assigned to this media recorder
valuable.
I want to actually upload this voice.
These are actually two firebase.
So let's create a method for that.
First let's actually call this Smurfette.
And here I can say upload the audio upload the audio to Firebrace So it is going to be RMF at this with
us on the call and at the end of our statement here.
So now before the end in curly brace of this class let's just say private voit upload the audio to firebase
and let's put a pair of parenthesis.
And that often includes this method by using these curly braces so insert Smurfette here I can't say.
So first of all we can actually the first two are a Proclus dialogue.
So we have Procrustes they like here already created inside our file.
Its name is download progress they like we can actually use it inside our Meffert upload the audio to
firebase So here I can say download progress.
I like that said message.
And inside the parenthesis I can say uploading the audio uploading the audio to firebase dot dot dot.
And here I can say download progress.
They like that show that show.
Right.
So now after these two lines of code I can say create a local variable of type storage or so stories
of friends and the name of this variable is going to be audio file address assignment operator.
And here I'm going to refer to this as reference that I actually created above inside this class.
So this reference that.
So I'm going to actually create a folder inside our firebase storage named audio files.
OK.
So it is going to be that child and inside the prentices I can say audio files.
So this is the name of our folder.
And inside that folder I want to put our audio files.
So again I'm going to call that child.
And now we need to specify the name of the audio that we want to actually put inside our folders so
just put two coats here and the name is going to be my art you'll find my audio file dot free.
So it is going to be the format right.
The type and after this line of code here.
So let me create an image so that you can see what's going on here.
So here let's create a variable of type you are.
So you and I.
And the name is going to be audio file.
You are the assignment operator so let's refer to this you have a class that you are right let's call
this method you are.
This is me from file.
You are from file.
So we're going to get that you are from a file that is passed to this method.
So we already created a valuable type Stream.
And the name of that value but is audio file right.
If you remember it here inside our class we created this variable in line number 41 audio file.
And inside this uncreate method we actually initialize this variable here.
So here if I scroll down here we can see that Enceladus starts to record the audio Mefford here views
this audio file here in order to specify the output file.
Ok so now we're going to use this audio file here in order to get you out of that voice.
So you or I.
So let's go inside is upload the audio to fire Smurfette.
And he in line number two hundred and fifty four inside Apprentice's.
We can actually see a new file because we need to pass a value of a file to this MF at here right now
file.
And now I just need to pass the audio file which is of type string.
So it's going to create a file.
It's going to create a value of type file from this string value that you passed to the constructor
and this new object is created and we passed this object to the from file method of this you are right.
And it's going to return a value of type you are right.
So it's going to actually be assigned to this valuable audio.
So let me put little or hear audio file you or I.
OK so now that we have this audio file you are here.
Let's actually refer to this audio file address.
So now I want to put this file this audio file to this address right so I can say that pute fine.
So we have this right here right.
Because if it needs an argument of type you are right.
So if you already created this valuable off that you or I so if we can pass that here audio file you
are right that I actually add on success listener.
And inside the just typing here new and then just hold the shift key on your keyboard and then personal
it personally.
OK on your keyboard.
In order to get this option unsuccessfully send it and then press an intelligent new keyboard in order
to select this option.
So let me create a new line so that you can see what's going on here.
So this Smurfette is actually overrated for us.
Unsuccess my friend and he had at the end in Prentice's you can see that here right.
Let's actually just type in dot at unfairly a listener knew on.
So select is fun.
Unfilial listener so this should be the same if it is also over here.
So let me put a semicolon here at the end of this practices here.
So now let's go into this.
Unsuccess Meffert here.
So this unsuccess Meffert here is going to be called the offloading of the voice to firebase was successful.
So inside the Smurfette here we can say download progress de-lurked not dismiss.
That's it.
So now let's run our application.
Let's choose so.
Because it is going to be and I actually use the audio recorder of the device.
It is actually necessary to test this app on the actual device because the emulator cannot be used in
order to access the audio recorder.
So choose actually an actual device and then click on OK.