Working With Video Player In Android Application

Introduction

 
Android is one of the most popular operating systems for mobile. A lot of people use Android for multimedia purposes and entertainment. Video player is the most important element in multimedia. So, I will show you how to integrate video in your Android application using Android Studio. Android is the kernel-based operating system. In Android, the user can modify the GUI components and source code.
 
Requirements
Steps to be followed
 
Carefully follow the below steps to integrate video in your Android application using Android Studio. I have included the source code too.
 
Step 1
 
Open the Android Studio and start a new project.
 
Android
 
Step 2
 
Put the application name and company domain. If you wish to use C++ for coding the project. Mark the "Include C++ support", and click Next.
 
Android
 
Step 3
 
Select the Android minimum SDK. After that, click Next. 
 
Android
 
Step 4
 
Choose "Basic Activity" and click Next.
 
Android
 
Step 5
 
Put the activity name and layout name. Android Studio basically takes the java class name the same as the activity name you provided. Now, click Finish.
 
Android
 
Step 6
 
Go to activity_main.xml and click the text button. This XML file contains the designing code for the Android app. In the activity_main.xml file, copy and paste the below code.
 
Activity_main.xml code
  1. <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:app="http://schemas.android.com/apk/res-auto"  
  3.     xmlns:tools="http://schemas.android.com/tools"  
  4.     android:layout_width="match_parent"  
  5.     android:layout_height="match_parent"  
  6.     android:orientation="vertical"  
  7.     tools:context="ganeshannt.playvideo.MainActivity">  
  8.   
  9.     <Button  
  10.         android:id="@+id/button"  
  11.         android:layout_width="0dp"  
  12.         android:layout_height="wrap_content"  
  13.         android:layout_marginEnd="8dp"  
  14.         android:layout_marginStart="8dp"  
  15.         android:onClick="videoplay"  
  16.         android:text="Play video"  
  17.         app:layout_constraintLeft_toLeftOf="parent"  
  18.         app:layout_constraintRight_toRightOf="parent"  
  19.         app:layout_constraintTop_toTopOf="parent"  
  20.         tools:layout_constraintLeft_creator="1"  
  21.         tools:layout_constraintRight_creator="1"  
  22.         tools:layout_constraintTop_creator="1" />  
  23.   
  24.     <VideoView  
  25.         android:id="@+id/videoView"  
  26.         android:layout_width="0dp"  
  27.         android:layout_height="wrap_content"  
  28.         tools:layout_constraintTop_creator="1"  
  29.         tools:layout_constraintRight_creator="1"  
  30.         android:layout_marginStart="8dp"  
  31.         android:layout_marginEnd="8dp"  
  32.         app:layout_constraintRight_toRightOf="parent"  
  33.         app:layout_constraintTop_toBottomOf="@+id/button"  
  34.         tools:layout_constraintLeft_creator="1"  
  35.         app:layout_constraintLeft_toLeftOf="parent" />  
  36. </android.support.constraint.ConstraintLayout>  
Android
 
Step 7
 
In the MainActivity.java file, copy and paste the below code. Java is the back-end language for Android. Do not replace your package name, otherwise, the app will not run.
 
MainActivity.java code
  1. package ganeshannt.playvideo;  
  2. import android.net.Uri;  
  3. import android.os.Bundle;  
  4. import android.support.v7.app.AppCompatActivity;  
  5. import android.view.View;  
  6. import android.widget.Button;  
  7. import android.widget.MediaController;  
  8. import android.widget.VideoView;  
  9.   
  10. public class MainActivity extends AppCompatActivity{  
  11.   
  12.     Button clk;  
  13.     VideoView videov;  
  14.     MediaController mediac;  
  15.   
  16.     @Override  
  17.     protected void onCreate(Bundle savedInstanceState) {  
  18.         super.onCreate(savedInstanceState);  
  19.         setContentView(R.layout.activity_main);  
  20.   
  21.         clk = (Button) findViewById(R.id.button);  
  22.         videov = (VideoView) findViewById(R.id.videoView);  
  23.         mediac = new MediaController(this);  
  24.   
  25.     }  
  26.   
  27.     public void videoplay(View v)  
  28.     {  
  29.        String videopath = "android.resource://ganeshannt.playvideo/"+R.raw.google;  
  30.         Uri uri = Uri.parse(videopath);  
  31.         videov.setVideoURI(uri);  
  32.         videov.setMediaController(mediac);  
  33.         mediac.setAnchorView(videov);  
  34.         videov.start();  
  35.     }  
  36.   
  37. }  
Step 8
 
Create Resources folder for pasting your video file as you play (res->>new->>folder->>res folder).
 
Android
 
Change the folder path.
 
Android
 
Step 9
 
Click the "Make Project" option and run it.
 
Android
 
Step 10
 
Run the application >> choose Virtual Machine >> click OK.
 
Android
 
Deliverables
 
Here, we have been successful in the integration of video in the Android app.
 
Android
 
Android
 
Android
 

Summary

 
In this article, we learned about Working With Video Player In Android applications.
 
Don’t forget to like and follow me. If you have any doubts, just comment below.