Embedding Video In An Android App

Introduction

 
Adding video to an Android app can be done just by adding a few lines of code. In this article, I will demonstrate how to embed a video to an Android app. This article will also explain how to control the video by playing, pausing and by seeking back and forth. This is really easy to do.
 

Steps to embed video in your Android Application

 
Step 1 
 
Open your Android project and create a new Android project  
 
Embedding Video In An Android Application 
 
Step 2 
 
Create a new folder named "raw" in your Android project's "res" folder and place your video file inside the "raw" folder.
 
Embedding Video In An Android Application 
 
Embedding Video In An Android Application
 
You can now see the video content is successfully added into your Android Studio project by viewing the "raw" subfolder under "res" folder.
 
Embedding Video In An Android Application 
 
Step 3
 
To add a video player in the activity_main.xml layout, go to "Design" tab of your activity_mail.xml. Goto "Widgets" under palette and drag the "VideoView" into your Android Aplication Layout. I have named my VideoView's id as "videoView". 
 
Embedding Video In An Android Application 
 
Step 4 
 
Write code to attach video to the VideoView.
 
On the onCreate() method add the following code,
  1. VideoView videoView = (VideoView) findViewById(R.id.videoView);  //casting to VideoView is not Strictly required above API level 26
  2. videoView.setVideoPath("android.resource://" + getPackageName() + "/" + R.raw.videoclip); //set the path of the video that we need to use in our VideoView
  3. videoView.start();  //start() method of the VideoView class will start the video to play
Step 5
 
Run your Application on your physical device or emulator. But you have noticed that there is no controls on that video.
So to add controls on your video, use "MediaController" class. 
 
Add the following lines of code, after the code of setting up the Path to your video.
  1. MediaController mediaController = new MediaController(this);  
  2. //link mediaController to videoView  
  3. mediaController.setAnchorView(videoView);  
  4. //allow mediaController to control our videoView  
  5. videoView.setMediaController(mediaController);  
Run your application. Now, you can see that your video is playing, along with controls to play, pause, seek back and forth.
 
Here I have provided my source code, for your reference.
 
activity_mail.xml 
  1. <?xml version="1.0" encoding="utf-8"?>    
  2. <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"    
  3.     xmlns:app="http://schemas.android.com/apk/res-auto"    
  4.     xmlns:tools="http://schemas.android.com/tools"    
  5.     android:layout_width="match_parent"    
  6.     android:layout_height="match_parent"    
  7.     tools:context=".MainActivity">    
  8.     
  9.     <VideoView    
  10.         android:id="@+id/videoView"    
  11.         android:layout_width="wrap_content"    
  12.         android:layout_height="wrap_content"    
  13.         app:layout_constraintBottom_toBottomOf="parent"    
  14.         app:layout_constraintEnd_toEndOf="parent"    
  15.         app:layout_constraintStart_toStartOf="parent"    
  16.         app:layout_constraintTop_toTopOf="parent" />    
  17. </androidx.constraintlayout.widget.ConstraintLayout>     
MainActivity.java
  1. package com.thetechiechum.videodemo;  
  2.   
  3. import androidx.appcompat.app.AppCompatActivity;  
  4.   
  5. import android.os.Bundle;  
  6. import android.widget.MediaController;  
  7. import android.widget.VideoView;  
  8.   
  9. public class MainActivity extends AppCompatActivity {  
  10.   
  11.     @Override  
  12.     protected void onCreate(Bundle savedInstanceState) {  
  13.         super.onCreate(savedInstanceState);  
  14.         setContentView(R.layout.activity_main);  
  15.   
  16.         VideoView videoView = (VideoView) findViewById(R.id.videoView);  
  17.         videoView.setVideoPath("android.resource://" + getPackageName() + "/" + R.raw.videoclip);  
  18.   
  19.         MediaController mediaController = new MediaController(this);  
  20.         //link mediaController to videoView  
  21.         mediaController.setAnchorView(videoView);  
  22.         //allow mediaController to control our videoView  
  23.         videoView.setMediaController(mediaController);  
  24.         videoView.start();  
  25.     }  
  26. }  

Summary

 
In this article, you have seen the steps to embed the video file into your Android Application. I hope you have understoond the usage of MediaController class while embedding video into your Android Application. 


Similar Articles