Record video and Capture photos in app — Unity Android Contd.
This is the continuation (second section) of this project. You can find the first section here
Now since we will be continuing the previous project, I would not be creating a new project. If you want to know how to get things set up, please refer to the first part of this project.
Record Videos
First, we will start coding on the ‘camera.cs’ script we previously created.
Now we need to add two more private members to are class as below:
private MP4Recorder recorder;private Coroutine recordVideoCoroutine;
Then let’s create a method to start recording. In this method, we will create a new MP4Recorder from NatSuite and initialize it.
Also, I have included code to start the recording() coroutine in this method which will be implemented later.
public async void startRecording(){// Create a recorderrecorder = new MP4Recorder(640, 480, 30, bitrate: 6_000_000, // bits per secondkeyframeInterval: 3);//Start recordingrecordVideoCoroutine = StartCoroutine(recording());}
Note that when passing values to the MP4Recorder’s constructor, the first two values are the resolution of the video, so you can change them according to your needs and according to your camera’s capabilities. If an error occurs while running the app (or if it crashes) try changing the resolution to different standard resolution values. You can know more about the MP4Recorder here.
Now lets create the recording() method. This will be an IEnumerator method which will continuously commit frames to the MP4Recorder. I referred NatCorder: Video Recording Made Easy | by Lanre Olokoba | NatSuite Framework when coding this section.
Our recording() method will be as follows:
private IEnumerator recording(){// Create a clock for generating recording timestampsvar clock = new RealtimeClock();for (int i = 0; ; i++){// Commit the frame to NatCorder for encodingrecorder.CommitFrame(webcamTexture.GetPixels32(), clock.timestamp);// Wait till end of frameyield return new WaitForEndOfFrame();}}
Now let’s create the stopRecording() method to stop the coroutine. This is pretty simple.
public async void stopRecording(){//Stop CoroutineStopCoroutine(recordVideoCoroutine);// Finish writingvar recordingPath = await recorder.FinishWriting();}
Now we are all done with coding. Lets save this script and move on to Unity.
In the unity editor, create two new buttons in the panel and name them Start and Stop respectively. Also, rename the button text to ‘Start’ and ‘Stop’ and then align them as you wish. Given below is my structure.
Next click on the Start button and in the inspector, click the ‘+’ icon under On Click() and add a click event. Then drag the empty gameobject we create in the previous section into this click event and select the startRecording() function from the camera script. Repeat the same process for the Stop button and instead select the stopRecording() method.
This is how the Start and Stop button inspectors should look.
Now you should be able to test this by running in the unity editor and using the webcam to capture videos. Click on start and stop and record a video and you should find the video in your project folder. Play the video and check if everything is fine. If the video is distorted, change the resolution of the MP4Recorder and try again.
You can also build the project and run this in your android mobile but note that the recorded video does not show up in the gallery. You can find the recorded video in the Android data folder of the app but some file explorer applications does not show it either (but you can access this by connecting the phone to your computer).
So, let’s take this one more step further and make our video visible in the gallery. Doing this is quite easy thanks to the Native Gallery plugin. You just have to add one line of code.
Add this below line at the end of the stopRecording method() we previously created.
NativeGallery.Permission permission = NativeGallery.SaveVideoToGallery(recordingPath, "CameraTest", "testVideo.mp4", (success, path) => Debug.Log("Media save result: " + success + " " + path));
Now your stopRecording() method should look like this:
public async void stopRecording(){//Stop CoroutineStopCoroutine(recordVideoCoroutine);// Finish writingvar recordingPath = await recorder.FinishWriting();//save video to galleryNativeGallery.Permission permission = NativeGallery.SaveVideoToGallery(recordingPath, "CameraTest", "testVideo.mp4", (success, path) => Debug.Log("Media save result: " + success + " " + path));}
Cheers! We have completed building our application. Now you can run this application on your android device and find the videos recorded in your gallery.
If you completed the previous section as well, you can see that we can capture photos even while recording a video (which even native camera applications doesn’t provide). 😀
Please leave your feedback and suggestions and also please tolerate if there is any mistake as I am not an expert in Unity. Thank you!