Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Complete guide on How to build a Video Player in Android

  • Difficulty Level : Hard
  • Last Updated : 16 Jul, 2021

This article explains the stepwise process as to how to build a Video Player using Android Studio
For viewing videos in android, there is a special class called “MediaPlayer“. In this article, we will be having 2 videos which are connected by the “Dialog box“, i.e a dialog box will come after completion of the first video which will ask the user whether he wants to replay or play next video.
To insert videos in Android, we put in raw folder. “raw” folder is present in 
 

"app"--> "res" --> "raw"

In this folder, you just need to paste the videos whichever you want to play.
Steps to build a Video Player:
 

Want a more fast-paced & competitive environment to learn the fundamentals of Android?
Click here to head to a guide uniquely curated by our experts with the aim to make you industry ready in no time!
  1. In creating Frontend we just need one component, i.e VideoView.
  2. The icons like play, rewind, forward will only come when we touch on VideoView and they will only come for just 3 seconds and then they will disappear. It is provided by Google and it is its default behaviour.
     
  3. Coming to back-end part i.e Java coding, we are getting media controls by:

    vw.setMediaController(new MediaController(this));

  4. Then, adding the videos of the raw folder in ArrayList and making a call to a method called setVideo() by giving an argument to it of the first video.

    // big video songs are not running 
    videolist.add(R.raw.faded); 
    videolist.add(R.raw.aeroplane); 
    setVideo(videolist.get(0));



  5. Now in setVideo() defining, we need an Uri object so as to pass to a method called as setVideoURI(). Therefore,

    String uriPath = “android.resource://” + getPackageName() +”/” + id ; 
    Uri uri = Uri.parse(uriPath); 
    vw.setVideoURI(uri); 
    vw.start();

    Note: First video will start playing as soon as application gets launch. This is because we are giving call to setVideo() from inside onCreate() and then inside setVideo(), it is calling vw.start(), where vw is VideoView.

  6. Now, code of generating a dialog box is done inside the method called onCompletion(). Please refer to this article for how to generate Dialog Box

    // It is creating object of AlertDialog 
    AlertDialog.Builder obj = new AlertDialog.Builder(this);

  7. At last, we have handled the coding of user’s action, i.e what the user has click (Replay or next). The simple logic is used such as increment and decrement.
    public void onClick(DialogInterface dialog, int which) {
      if (which == -1) {
        vw.seekTo(0);
        vw.start();
      }
      else {
        ++currvideo;
        if (currvideo == videolist.size())
          currvideo = 0;
        setVideo(videolist.get(currvideo));
      }
    }

The complete code (activity_main and MainActivity) for the above discussed program is given below:

activity_main.xml




<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
  
   <VideoView
       android:id="@+id/vidvw"
       android:layout_marginTop="10dp"
      android:layout_width="match_parent"
       android:layout_height="match_parent"
       />
</RelativeLayout>

MainActivity.java




package com.example.videoapp_demo;
  
import android.content.DialogInterface;
import android.media.MediaPlayer;
import android.net.Uri;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.MediaController;
import android.widget.VideoView;
  
import java.util.ArrayList;
  
public class MainActivity
    extends AppCompatActivity
    implements MediaPlayer.OnCompletionListener {
  
    VideoView vw;
    ArrayList<Integer> videolist = new ArrayList<>();
    int currvideo = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        vw = (VideoView)findViewById(R.id.vidvw);
        vw.setMediaController(new MediaController(this));
        vw.setOnCompletionListener(this);
  
        // video name should be in lower case alphabet.
        videolist.add(R.raw.middle);
        videolist.add(R.raw.faded);
        videolist.add(R.raw.aeroplane);
        setVideo(videolist.get(0));
    }
  
    public void setVideo(int id)
    {
        String uriPath
            = "android.resource://"
              + getPackageName() + "/" + id;
        Uri uri = Uri.parse(uriPath);
        vw.setVideoURI(uri);
        vw.start();
    }
  
    public void onCompletion(MediaPlayer mediapalyer)
    {
        AlertDialog.Builder obj = new AlertDialog.Builder(this);
        obj.setTitle("Playback Finished!");
        obj.setIcon(R.mipmap.ic_launcher);
        MyListener m = new MyListener();
        obj.setPositiveButton("Replay", m);
        obj.setNegativeButton("Next", m);
        obj.setMessage("Want to replay or play next video?");
        obj.show();
    }
  
    class MyListener implements DialogInterface.OnClickListener {
        public void onClick(DialogInterface dialog, int which)
        {
            if (which == -1) {
                vw.seekTo(0);
                vw.start();
            }
            else {
                ++currvideo;
                if (currvideo == videolist.size())
                    currvideo = 0;
                setVideo(videolist.get(currvideo));
            }
        }
    }
}

Output: 
 

  • Playing the first video: 
     
    first song "faded"

    first song “faded”

  • Dialogue box after the first video:
    After completion of first song,dialog box is getting generated

    After completion of first song,dialog box is getting generated

  • Playing the second video:
    When we click on "NEXT",then the second video starts running

    When we click on “NEXT”,then the second video starts running




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!