How can I record a Video in my Android app?


Introduction

Video Recording is a most common feature which we can currently get to see in many android applications. This feature is mostly seen used in payments applications where the application has to verify the identity of the user for authentication as well as doing their KYC. In this article we will take a look on How to record a video within our android application.

We will be building a simple android application in which we will be displaying a button and a video view in which when a user clicks on a button we will record a video and once the video recording is done we will be playing that video in our video view.

Implementation

We will be creating a simple application in which we will be displaying a listview. Inside this listview we will be displaying different programming languages and adding on click listener for our items inside our list view. On clicking on the item of listview we will be displaying a toast message with the item name to our user. We will be following a step by step guide to implement addition of two numbers in Android.

Step 1 : Creating a new project in Android Studio

Navigate to Android studio as shown in below screen. In the below screen click on New Project to create a new Android Studio Project.

After clicking on New Project you will get to see the below screen.

Inside this screen we have to simply select Empty Activity and click on Next. After clicking on next you will get to see the screen below.

Inside this screen we have to simply specify the project name. Then the package name will be generated automatically.

Note: Make sure to select the Language as Java.

After specifying all the details click on Finish to create a new Android studio project.

Once our project has been created we will get to see 2 files which are open i.e activity_main.xml and MainActivity.java file.

Step 2: Working with activity_main.xml

Navigate to activity_main.xml. If this file is not visible. To open this file. In the left pane navigate to app>res>layout>activity_main.xml to open this file. After opening this file. Add the below code to it. Comments are added in the code to get to know in detail.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:app="http://schemas.android.com/apk/res-auto"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   tools:context=".MainActivity">
   <!-- creating a video view on below line -->
   <VideoView
       android:id="@+id/videoView"
       android:layout_centerInParent="true"
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:layout_above="@id/idBtnRecordVideo"
       android:layout_margin="5dp" />
   <!-- creating a button to record a video on below line -->
   <Button
       android:id="@+id/idBtnRecordVideo"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:layout_alignParentBottom="true"
       android:layout_centerInParent="true"
       android:layout_margin="10dp"
       android:text="Record Video"
       android:textAllCaps="false" />
</RelativeLayout>

Explanation: In the above code we are creating a root layout as a Relative Layout. Inside this layout we are creating a video view which we will be using to display the video within our application. After creating this video view we will be creating a button which we will be using to record a video. Then we are adding a closing tag for our Relative Layout.

Step 3: Adding permission to record a video

Navigate to AndroidManifest.xml file and add below permission to it in the manifest tag to record a video.

<!-- adding permissions on below line -->
<uses-feature android:name="android.hardware.camera" android:required="true"/>

Step 4: Working with MainActivity.java file

Navigate to MainActivity.java. If this file is not visible. To open this file. In the left pane navigate to app>res>layout>MainActivity.java to open this file. After opening this file. Add the below code to it. Comments are added in the code to get to know in detail.

package com.example.java_test_application;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.VideoView;
public class MainActivity extends AppCompatActivity {

   // creating variables on below line.
   private Button recordVideoBtn;
   private VideoView videoView;
   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);

       // initializing variables on below line.
       recordVideoBtn = findViewById(R.id.idBtnRecordVideo);
       videoView = findViewById(R.id.videoView);

       // adding click listener for recording button.
       recordVideoBtn.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
               // on below line opening an intent to capture a video.
               Intent i = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
               // on below line starting an activity for result.
               startActivityForResult(i, 1);
           }
       });
   }
   @Override
   protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
       super.onActivityResult(requestCode, resultCode, data);
       if (resultCode == RESULT_OK && requestCode == 1) {
           // on below line setting video uri for our video view.
           videoView.setVideoURI(data.getData());
           // on below line starting a video view
           videoView.start();
       }
   }
}

Explanation: In the above code firstly we are creating variables for our video view and button. Now we will get to see the onCreate method. This is the default method of every android application. This method is called when the application view is created. Inside this method we are setting the content view i.e the layout file named activity_main.xml to set the UI from that file. Inside the onCreate method we are initializing the video view and button variable with the id which we have given in our activity_main.xml file. After that we are adding an on click listener for our record video button. Inside the on click listener we are opening a new intent and setting action for it as video capture and starting that activity.

Now we are creating an onActivity result method, inside this method we are checking if the request code is the same as we are passing and the result code is OK, then we are setting the video uri for our video view and starting our video view to display the video which we have captured.

After adding the above code now we have to simply click on the green icon in the top bar to run our application on a mobile device.

Note − Make sure you are connected to your real device or emulator.

Output

Conclusion

In the above tutorial we have taken a look on How to record a video from our android application and display the recorded video within our android applications video view.

Updated on: 08-May-2023

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements