So basically we want to set this up so that if there's an error that it will stop the app and print
the error.
Otherwise we want it to do what we need it to.
So let's type if let error equals error.
So if we were going to create a constant called error and if this error is made equal to the error that
the recognizer throws
then we're going to print.
There was an error Colin.
And we're going to give it the error that it created using this little encapsulation type thingamabob
string interpellation that's what it's called.
So if there's an error it'll be thrown in right there and it will print nicely for us but now we want
to say else otherwise we want to basically print the transcription.
So let's let's go and just stop and run through this.
Here's our path to our audio file and we're going to start playing the music.
Then immediately after that we set up a recognizer we create a request that pulls the file and it silently
parses through this file entry and turns it into text which is so cool.
So then we create a recognition task.
And this is actually sorry.
This is actually where it recognizes the text and then prints a result.
And so while it's doing that we're going to actually print the end result.
So it's called result dot best transcription.
And this is the most accurate one that is created and we want to set it up as a formatted string so
that it starts with a capital and is formatted nicely.
So this is a lot of stuff and we have not yet checked to see if it works.
So let's go ahead and create an Iby action to trigger loading our audio and transcribing.
So open your mind up storyboard open your assistant editor and pull open your view controller an automatic
of course.
OK.
So underneath this is the button.
So you may have to access it from here.
I like to create my actions at the bottom of my view controllers
so down here you up right click from button down below here.
Now well let me.
Why is this strange.
Let's try it again.
Oh there we go.
I wonder.
Oh you know what.
Let me cancel this.
I must have accidentally deleted an important bracket.
Yeah.
Looks like that's what I did.
Let's go ahead and scoot those over scoot that over cleaned it up there.
OK.
All right.
So now what is that huh.
So I delete two
maybe I do it into 1.
It's strange.
What is this expected declaration.
My class is here.
Let's lower that.
So if I go
one more OK.
Now our classes finally closed.
There we go.
Let's cancel the simulator.
We don't really need that right now.
That's just taking up a lot of extra memory.
OK.
So we're going to go out and create space for an IB action right click drag and drop it down here.
Change it to action of course and we're going to call this play button.
Pressed ok cool.
So let's go out and close the assistant editor go back to our new controller just to save on some memory
and space and time here.
Ok cool.
So let's go down to that IAB action.
Now what we should do is basically when that button is pressed we want to hide our activity I'm sorry.
We want to show our hidden activity spinner so activity spinner that is hidden is no longer true.
It is now false.
We also want our activity spinner to start spinning of course because otherwise it just looks like a
static photo which is nowhere near as cool.
So activity spinner does start animating
equals true very very cool.
And you know what we don't actually need to just start animating because it's a function that is called
Right on it.
Now it's indexing the files here which is why everything looks purple and white and not properly colored
but in a moment it will change back I promise.
OK.
So beneath this go ahead and type the name of our function request speech.
And we are not building nearly as much as we should.
So we're finally at a point where we can build this app.
So let's go ahead and give it a shot.
Let's try building it on our simulator and see what happens.
OK.
Here we go.
Let's take a look.
Let's see what happens.
Ok cool.
So nothing looks different here but let's try to see if it works if I click it should start playing
my audio file.
It should recognize the audio and transcribe it and print it here in the console so let's give it a
shot.
So it doesn't seem Oh we crashed Interesting OK let's look at our error code here.
The app's info up list must contain an n s speech recognition usage description key Oh right we forgot
something very very important.
We need to add this and speech recognition usage description.
Wow.
That is a mouthful.
Go ahead and terminate the app and we're going to add that to our info duppy list.
So basically click on your project over here and find your info up the list file and then basically
we're going to add a new key here.
By clicking the plus make sure this is closed not open.
Make sure this is closed click plus and then you're going to type what we just saw here.
So go ahead and type an s speech recognition usage description.
And when I press Enter you'll see it actually changes into what it actually should look like but it's
not yet auto completing because we're still in Beta.
But by the time that the official XTO day comes out we'll be good to go.
OK.
So basically this is the nice little message that will pop up when you request speech off.
So when it pops up you want to say something like We need to analyze and transcribe your audio.
We hope that's cool.
Let's add a nice little emoji here.
Let's go ahead and give it a nice little thumbs up.
I actually have one of these.
We go.
All right.
So this is I think the reason our app crash let's build it one more time.
Let's give it a shot and see if it works the way we want it to.
Now right we go.
So when we click this button it should request authorization.
Hey look at that.
OK so now we have a permission thingy we need to analyze and transcribe your audio we hope that's cool.
So click allow.
OK.
And it appears to be not working.
There was an error when it was suspended.
OK.
So it appears to be working but we know that right here in the in the console that it is not.
And you know what I actually know why that is using the speech API on the simulator for whatever reason
does not work on a simulator device.
So I'm going to switch over to my iPhone here and I'm going to basically create a new movie recording
and I'm going to set it up here to use my iPhone.
So just like that.
Here we go.
And so I'm going to build it to my iPhone and then we're going to be able to see if this actually works
the way it's supposed to.
So I'll select my humourously named iPhone and build it to that one and we'll see if it's going to be
nice to us.
I apologize for my messy homescreen but least my wallpapers call.
We go scribe is now installed.
It's going to pop open here and on my iPhone.
Let's go ahead and give it a shot.
OK.
So I tap the button it requests my permission and I click OK.
So oh hey awesome.
So look at that.
That is exactly what we need it.
Scribe just might be the coolest app I've ever made.
Great.
So let's go back to my iPhone here which is recording.
Do you notice that the spinner is still spinning.
Yeah that's because we have not yet told when to stop.
So let's go ahead and stop that from running.
And in our view controller we need to add a couple of things.
So we need to create Well basically all we need to do is tell it what we want it to do.
As far as stopping goes and in order to do that we need to implement the avy audio player delegate.
So up at the top of your class type avi audio player flips out the player audio player delegate.
Just like that and there is a function inside this delegate that will help us monitor and analyze what
an audio player has finished.
And this is really really cool.
So let's go ahead add that and it is called Audio Player did finished playing just like that.
So all we need to do inside of this is basically just say player dot stop because what this does is
it basically searches for an instance of AB audio player.
And then we can pass this command stop straight to it.
Beneath that we want to say activity spinner whip's activity spinner datt stop animating and activity
spinner dot is hidden equals truth because when the audio is done we want the button to be enabled again
so we can analyze the audio if we want to.
So this now will analyze when the player is finished.
It will stop the player.
It will hide the spinner and it will stop it from animating.
Now there's one last thing we need to do and it won't work without it.
But we basically need to tell the audio player that it has a delegate like that sounds silly but all
we need to do is type cell audio player which is the one that we created here.
Self-taught audio player delegate equals self.
So this basically just sets this view controller up to be its delegate and it's giving me an issue here
what is the deal expression resolves to an unused.
I value that is really interesting.
Why would that be up.
Looks like it just needed to build.
Never mind sometimes if you add in some sub subclassing and things like that here are some inheritance
here.
It takes a while for it to analyze and really understand what it is that you are doing.
So now that it is good to go it has a delegate.
It has the function to detect that it has finished playing.
Let's go ahead and run it and let's see if it works how we hope.
OK let's check to see if it does what it's supposed to.
So as soon as it's done playing the audio file it should go ahead and stop the spinner and make the
button look just like it is right now so let's tap the button scribe just might be the coolest app I've
ever made.
Hey and it's finished and the button stopped.
That is exactly what we wanted to do.
Super duper cool.
So let's let's think.
Is there anything else that we are forgetting.
Let's go inside of scribe.
Oops.
Am I forgetting.
Oh of course we're not even putting in the audio into our app or mean the transcription we know it's
printing here but we have yet to actually put it in where it's supposed to go.
Now if you remember the result.
Best descript best transcription formatted string.
This is where it's supposed to be.
But we actually can just set the text value of our text to you here to fill with this text.
So delete this delete print and we're basically going to say self dot.
And remember we already created an outlet for our transcription textfield.
So where was that self transcription textfield text equals result.
Best transcription formatted string.
So in theory when I run this app it should transcribe and it should drop this text right in to our app.
Let's give it a shot.
Let's let it run and let it build and run again.
And here we go.
OK let's take a look what's on my phone.
Scribe just might be the call it is ever made.
OK.
It finishes playing the player turns off just like we did with the delegate function there.
Let's do it again.
Scribe just might be the coolest app I've ever made.
Awesome.
OK guys well you know what we just created a beautiful app that uses the new speech recognition API
from Apple in Iowa.
We requested a speech authorization using that function in the info up list.
We created a recognizer that basically analyzes our audio turns it into text and we also simultaneously
created a player that played it at the same time.
So this is only scratching the surface of what is possible with this speech API.
And in a future video I'm going to be talking about how you can analyze live audio and then eventually
how you can analyze live audio and set a new function based on a certain key word or phrases set.
So stay tuned for those.
Thanks for watching.
This is cool.
So some folks like.