Everybody what's going on this is Caleb with slopes dotcom and in this video we're going to use replay
kits to actually start beginning.
We're going to use replay kit to actually create a screen recording.
It's going to be awesome.
We're just going to be able to start the process in this video and in the video to follow will stop
it.
Edit and save the video to our cameral.
But in this video we're going to get started with the recording so let's go ahead and let's pull open
our view controller here and we need to think about what we're going to be doing basically we're going
to write two functions one called Start recording and one called Stop recording and in order to manage
you know whether a screen recording is happening or not we're going to create a property called is recording
and it's going to be a boolean that's going to help us manage whether or not we're recording.
So let's actually make that now.
It's just going to be a little private variable called is recording and it's going to be equal to false
from the beginning.
OK because when we open the app we're not recording anything.
The user should have control over that.
Now when the record button is pressed and let me make some more room for us here when the record button
is pressed we're essentially going to do one of two things start recording or stop recording.
OK.
And so if we are not recording or if is recording is false We're going to go ahead and call start
recording.
OK.
Now we have not yet written this function.
Otherwise if recording is true we're going to go ahead and call.
Stop recording.
OK.
And again we have not written this function but we're not going to get to this one in this video so
I'm just going to comment it out so that we don't have any issues that we don't need.
But we are going to write start recording.
And this is a function that's going to allow us to actually start a recording session using our our
P screen recorder.
OK so let's do it down below your Iby action right the function start recording.
OK.
Now what we need to do first of all is we need to check to make sure that our recorder is even available
for instance there are some problems with how I'm recording this chorus because I have my actual phone
plugged in so I need the microphone to make the recording but when I'm recording and using the screen
the recording capabilities of the microphone are sort of overridden for this application.
So we're going to check to see if the recorder is actually available.
Otherwise it could crash the app.
So go ahead and type.
Guard.
Guard recorder dot is available.
OK.
And assuming that it's available we're going to go ahead and continue.
Otherwise we're going to go ahead and just return and you know what just so that we know what's going
on let's go ahead and print.
Maybe recording is not available the lobal at this time.
And that will just print out to the console to tell us hey something's wrong with the recorder.
But assuming that we do have an available recorder what we can do then is to check to see hey is the
microphone toggle switch on or off.
Because remember in our interface we have a little switch and if it's on we're going to grant access
to the microphone if it's off we're going to say hey you can't use the microphone so to do that what
we're going to do is we're going to say hey if Mike toggle.
That's the name of our switch.
Dot is on.
We're going to go ahead and call recorder.
Dot is microphone enabled and it's a boolean value that indicates whether or not the microphone can
be enabled.
So go ahead and set that to be true if the switch is on otherwise.
Or else we're going to go ahead and call recorder.
Is microphone enabled equals false and that's going to say hey recorder don't record the microphone.
Now we have checked if the recorders are available we have checked if the microphone is enabled.
And now what we need to do is call recorder does start recording.
How cool is that.
So we just tap on that we press Enter on the handler and now we have to handle what happens if we get
an optional error.
So go ahead and type error and that's just the name of the parameter for the air that's returned.
If there is one and now what we're going to do is we're going to use a guard statement to say if error
is nil we're going to continue.
Otherwise we're going to print saying hey there was an error starting the recording and then returns
that we don't get a crash so go ahead and type guard error equals nil else.
And you know what this needs to be the boolean equals to check for equivalency.
But basically what this is saying is hey if air is nil then we're going to continue with air being nil.
Otherwise we're going to go ahead and print saying there was an error starting the recording.
And then we're just going to return to that we don't get a crash.
But assuming that the air is definitely nil we're going to print out started recording successfully.
That's just for us to show hey started recording and now that we're recording that's all happening in
the background.
But we need to basically do a few things when recording to our user interface.
Right.
We want the mike toggle button to basically be disabled so that we can't turn it on or off mid recording.
We also want to set the record button to change its text to say stop and to be of a red color.
We also want this status label to say recording dot dot dot and also to have read text then we're going
to set the boolean we made.
Is recording to be true.
OK.
So go ahead and type self dot Mike toggle and we're going to set is enabled to be false.
Ok then we're going to go ahead and change the record button to be read and have a title that says stop.
So call self dot record button and we're going to call.
Set title color to be a color literal.
And we're going to choose a nice red color.
OK so you can just choose red or any red color you want and the control state is normal.
OK then we're going to call self-doubt record button and we're going to just set the title itself to
say stop for control state normal that's normal normal.
That's not a word.
OK.
So next we need to set our status label up at the top of our app to say recording dot dot dot.
So go ahead and type self dot status label dot text and set that to be equal to recording after that
we're going to set the color to read.
So self status label text color and use a color literal of red to change the text color to red.
Finally go ahead and call self.
Is recording and set it to be equal to true because remember when we start the app it comes in is false.
So now we are successfully beginning a recording.
This all happens with the helpfulness of replay kit.
It handles it for us in the background and based on a successful beginning.
We're going to go ahead and change all of these properties so that they are represented in our app.
So let's go ahead and let's BuildOn run this let's tap on the button and let's see if we can successfully
start a recording or if we get an error.
OK so go ahead and choose your physical device from the device list build and run it and we're going
to go ahead and check out to see how we did.
We're going to look at it on the screen here.
I'm going to pull in my phone screen here on the on the screen so you can see it.
And now here we go let's see how we did.
All right.
So as you can see it's a bigger screen so we definitely get these stretched out a bit and we could we
could go and mess with the constraints but for the purposes of this app I'm not going to.
But we are going to test what happens when I tap on that green record button if it's successful we should
get some changes in the appearance if it's unsuccessful.
We should get an error.
So here we go.
We're going to go ahead and push on the record button there.
We're going to see what happens.
And it appears that nothing happened there.
Oh you know what.
Look at that we expect we accidentally removed our start recording function somehow that's super weird.
Let's build and run it again.
I must have selected it and push delete by mistake.
If you were watching that happened you're probably going oh this guy is an idiot.
But I promise I'm not.
Most of the time.
Sorry.
Anyway moving on so we can now see the app on our screen here.
We can change the images.
Blah blah blah.
But watch what happens when I push record.
As you can see we get a pop up right here allow screen recording in screen the record screen and microphone
or record screen only.
Check it out.
If I tap record screen and microphone we're going to go ahead and we're going to enable it to essentially
do what we want it to do to record the screen and microphone.
So if I tap that you'll see that we get a nice little start and stop thing but it looks like it freezes.
Let me build and run it again to show you what's actually going on behind the scenes here.
Basically the issue is that start recording is happening on a background thread and we're trying to
update the UI on a background thread which is a big no no on iOS development.
So watch what happens if I push it again.
We're going to get this error here saying hey UI API called on a background thread and we're going to
get this weird purple air here.
Now the way that we fix this is simply by calling dispatch.
Q main.
Async and we're going to pull all of this UI updating into the main queue instead of the background.
Q So let's build and run it again and let's see what we get when we actually do this.
Kate's going to pull up here that a background thread error goes away.
And here we are.
So now if I tap record watch what happens the red label at the top updates to say recording the button
changes.
The switch is disabled and it says started recording successfully.
Now I can still interface with the app with the screen but at the moment we're not actually able to
stop our recording so I'm going to go ahead and push stop.
Nothing's going to happen and I'm going to just kill the app so it stops recording to my device.
Now we are done starting our recording the next video is basically the last step we're going to stop
the recording create a video file from that edit that video then save it to our phone.
And after that we're going to be able to you know reset the views that it says record again and green
text blah blah blah.
But you guys get the idea we are now successfully starting recording updating our UI on the main thread
instead of a background thread.
And we're getting so so close to being able to stop and save our recording so let's do that in the very
next video.
I'll see you there.