Hey everybody what's going on this is Caleb with slopes dot com.
And in this video we're going to write the function that allows us to stop recording in the previous
video we started a recording.
Now we need to stop it and actually do something with the file that was stopped so let's go ahead and
let's dive right in.
Pull open your project and go ahead and go all the way down to the bottom of your class make some room
and write a function funk stop re courting OK.
And to begin we need to actually just go ahead and call recorder which is our instance of our peace
screen recorder.
And in order to stop a recording we just call recorder recorder dot stop recording.
And as you can see there's only one option.
It has a handler and an optional error.
So go ahead and click on that and then in the handler hear press Enter name this parameter preview name
the error error and now what we need to do is we need to basically say hey if the preview comes back
with a value meaning if it's not nil we're going to continue.
And then we're going to go ahead and set up a UI alert controller to either just delete the recording
or edit the one that we made.
So let's go ahead and use guard and type guard preview.
So if preview comes back with a value meaning if it's not equal to nil.
OK.
We're going to move on otherwise or else if it does come back Nell we're going to print.
Hey pre-view controller is not available.
This is just for our knowledge.
And basically the preview view controller is going to present the screen recording that we just presented
or recorded I mean and it's going to be presented in a nice little video editing view controller that
we just get out of the box from iOS.
Now of course after this we're going to go ahead and return thus preventing our app from crashing but
assuming that preview is has a value that it is not nil.
We're going to go ahead and now create our UI alert controller to present.
So we're going to stop the recording and present an alert.
So go ahead and type let alert equals UI alert controller.
OK.
And we're going to choose the option with a title message and preferred style.
So give it a title saying recording finished.
And the message this is what we're going to ask.
We're going to say hey would you like to edit or delete your recording question mark and that's what's
going to pop up and the alert style is going to be alert k not action.
So now what we need to do is we want to have two buttons on this you Isler controller we want to have
delete and edit.
So let's go ahead and create the delete action which is going to add a delete button.
So go ahead and type let delete action equals UI alert action.
And instantiate it with a title stylin handler.
So the title of course is going to be delete the action style.
Check it out.
We have a few of them.
We're gonna choose destructive.
OK because it indicates that the action might change or delete data and all it's going to do is just
say delete unread text which indicates that we're going to delete it for the handler.
Press enter and name this parameter action.
We're not going to use it but we do need to name it.
And then inside this what we're going to do is we're going to call another function from our recorder
called discard recording and what that's going to do is it's just going to delete the file that was
recorded so call self data recorder discard a recording and as you can see there is a closure at the
end.
So if we push enter we need to do something here let's just print a statement so that we know that the
recording was discarded properly so type print and then just say recording successfully deleted or discarded
whichever you want to say.
And so now if we were to add this to our alert we would get a button that says delete and if we tapped
it the recorder would discard the recording.
Very cool but now we need to say what if we want to actually are recording and then save it.
So we need to create an edit action so type let edit action equals UI alert action.
Instantiate it with a title stylin handler and this one is going to say edit of course the style is
going to be default and that's just going to give us the default blue text for a button and we're going
to go ahead and push enter and the handler name it action and in our edit action.
This is where we're going to go ahead and present our preview view controllers.
We can see the video we just recorded.
Now what we need to do before we do this we need to set the delegate count of our preview view controller
so go ahead and type preview.
That's the preview view controller dot preview controller delegate.
All righty.
Go ahead and set that to be equal to self.
But watch what happens if I try to build this.
It's going to say that it can't assign it to type.
Arpey preview control a delegate.
That's because we have to conform to it at the top of our view controller.
So Type A comma then type Arpey preview view controller delegate.
And if we try to build and run it now check it out.
It's not going to give us an error because we are now successfully conforming to the delegate.
So our view controller is now set as the delegate for our preview view controller meaning it's going
to get the data from it.
So that's cool but now what we need to do is we need to actually present our preview controller so to
do that simply call self present.
And the reason I'm doing that is because we are in a closure and we need to make a reference to self.
So if we're in our action we're going to go ahead and present our preview controller.
Animated will be true of course and the completion will be nil.
We don't need to do anything afterwards.
Now in order to actually get this to present Oh sorry this is optional right because it comes in as
an optional but at this point we definitely have a preview controller.
We're going to go ahead and just force on rapid.
OK so at this point we now have a delete action and an edit action but they're not yet linked to our
UI alert controller so we need to call you your call alert dot add action and pass in the delete action
then call the same thing.
Alert.
Action and passing the action.
OK then of course we're really trying to present this UI alert controller.
The action after that's presented presents the preview so go ahead and type self.
Present wups present and we're going to pass in.
Alert.
Animated is true and completion is nil.
OK so at this point we have now stopped the recording.
We've presented our alert and we can either tap delete or edit k.
Now of course if we stop our recording we should set our recording variable to be false.
So go ahead and type self is recording and set that speak to false and after that we have one less thing
to do and that is actually to reset the view controller to look the way it did before recording so green
button at the bottom that says record label up at the top with black text saying ready to record etc.
etc..
Now to do that we're going to write a little function called view reset.
So go ahead and type that phunk view reset K and what we need to do.
Let's think about the mike toggle button is disabled.
So type Mike toggle is enabled and set it to true so that it's enabled then go ahead and set status
label text that's the label up at the top of the controller set it to say ready to record again.
Then we're going to set the font color back to black.
So Type A status label text color and use a color literal here to set it to black.
Next up we're going to go ahead and change our record button at the bottom to be green again and to
say record so type record button dot set title and make it say record and the control state is normal
then we're going to call record button dot set.
Title color pasand a color literal and choose a nice green color like mine and the control state is
going to be normal.
OK so this is going to reset our view back to the way it was and we're going to just call that when
we stop our recording.
So go ahead and call self view reset and this is all we need to do to stop our recording.
Now you're probably wondering well what if I present my view controller.
How do I control what's actually going on in that editing or preview view controller.
Great question.
There is actually a delegate method that we can call because we have conformed to the Arpey preview.
You control a delegate that basically sets what happens after we're done editing our video when we push
the save button and that delegate method is pre-view preview.
Controller did finish.
OK now you know what I actually want you to read the description there.
Preview controller did finish.
It indicates that the preview controller is ready to be dismissed so when we push save or even cancel
what we want to happen is it to dismiss right.
We want that view controller to dismiss so-call true for animated and the completion can simply be nil.
OK.
And that's going to set our view controller to dismiss whenever we're ready so we have written a lot
of code without testing it.
So I think that we need to go in and test it.
Let's do that now.
Click build and run.
Let it run on your device and open up.
Here we go.
It's going to go ahead and pop open here.
Let's check it out guys.
I'm really excited so here we go it looks great.
Microphone access is then enabled I push record and it tells me that recording is not available at this
time.
And that's because I am currently recording the screen using screen flow so I'm going to stop the recording
here and I'm going to go ahead and retry it.
And you know what I was having so much trouble with recording my screen and recording my audio and everything.
You can actually do both very easily.
You will not have a problem with this because you're not recording while simultaneously using your phone.
But I can show you my screen as you can see the interfaces here.
And if I push the green record button it's going to start recording.
It says stop and it says Recording up at the top.
Now I can go ahead and I can use our little input here to change the images.
I can do all that fun stuff.
And then finally I can push the stop button and upon pushing that look what it says.
It says Would you like to edit or delete your recording.
Now if I tap delete it's going to go ahead and delete it.
But if I push at it.
Look what happens I get a nice little video pop up and listen let me turn on my volume.
This is
change.
OK very cool.
Now you can also notice that you can see a little slider at the bottom and you can even edit and scrub
on the video there to save at a certain length.
Now up at the top it says cancell and done so push done to save your changes and it changes to say save
after that push save and it goes back to our main app UI.
Now if I go to my photos here let me go over there really fast I can go in here and the video that is
on my phone is right there.
That's the video I just saved and created in my app.
So guys it appears to be working.
We are now stopping our recording.
We're able to edit it.
We're able to save our videos and all of that awesome awesome stuff.
This app is finished guys fantastic work.
Implementing replay kit into your app.
This is literally how easy it is and there are of course more ways that you can expand this with live
broadcasting with replay kit but that goes beyond the scope of this target topic so we can talk about
that in the future in another target topic.
But thank you guys so much for going through this target topic and learning about replay kit.
I'll see in the next target topic.
This is Calleigh with Deb's slopes.
Dot.com.