Hello People, How are you? Hope you are doing Safe. So In this blog, we are going to learn about Android Video Player or Android Media player. I have seen so many people are asking about Playing a video from a URL in Video Player using Android Studio, So as mentioned above we are going to prepare a Video player app that can play videos through a given URL.

Refer to Official Doc for VideoView

Without getting late Let’s go to Coding to build Android Video Player App or Android Media player

Note: This is a Sample App. Using this Sample Video Player You’ll get an Idea about Playing a video in Android Video Player from a URL.

Open AndroidStudio and Start a New Project for your Video Player App

First we will write Layout code for our Video Player in activity_main.xml

activity_main.xml
<?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">

    <androidx.cardview.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <VideoView
            android:id="@+id/video_view"
            android:layout_width="match_parent"
            android:layout_height="230dp"
            android:layout_gravity="center" />

        <ProgressBar
            android:id="@+id/video_loading"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:indeterminate="true" />

    </androidx.cardview.widget.CardView>


</RelativeLayout>

In the above Layout, I have taken the Relative layout as the parent layout. Inside parent layout, I have taken Cardview and inside card view, I placed two layouts one is Video view and the second is Progressbar. In the Video view, the video from the given URL will be Played. and the Progress bar is used to display when the video starts buffering.

I’m just using the normal Progress bar to tell users that the video is Buffering but In a real-time application, you have to add some good graphics to Look UI great for the User.

Now Let’s write Java code for the functionality of the Player.

Inside MainActivity First, we are going to set Media Controller for our Player in which user controls Play/pause, fast-forward/rewind actions.

//Set MediaController
        MediaController controller = new MediaController( MainActivity.this );
        controller.setMediaPlayer( videoView );
        controller.setAnchorView(videoView);
        videoView.setMediaController( controller );

Here we Set the Source Video URL from which our Video will be Played.

videourl = "https://firebasestorage.googleapis.com/v0/b/android-video-player-bc76d.appspot.com/o/BigBuckBunny.mp4?alt=media&token=a19034bc-004c-4577-a06e-e07139c6800c";
        Uri uri = Uri.parse( videourl );
//set video url
        videoView.setVideoURI( uri );
        videoView.requestFocus();

What has Remained? Just Starting our Video. So here we are going to set OnPreparedListener for video view. Now in the OnPreparedListener, we are going to start our video playing when it is prepared for playing.

//Set OnPreparedListener. Do Something When video is ready to play
        videoView.setOnPreparedListener( new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mediaPlayer) {

                videoView.start();
                progressBar.setVisibility( View.GONE );


            }
        } );

We have to write OncompleteListener to do something when the video is completed playing, As this is an example I’m just setting to seek the video timer to 0 sec.

//This is OnCompleteListener. Do Something when Video is Completed
        videoView.setOnCompletionListener( new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mediaPlayer) {


                videoView.seekTo( 0 );

            }
        } );

If video Buffers what are we going to do. As I already said we are going to display Progress Bar when Video starts buffering and hide the Progress bar when Buffering Ends and To know the Buffering State of the Video we are setting OnInfoListener. OnInfoListener gets the info of the video like buffering, Clicks, and some other actions.

//Set OnInfoListener for the VideoView
        videoView.setOnInfoListener( new MediaPlayer.OnInfoListener() {
            @Override
            public boolean onInfo(MediaPlayer mediaPlayer, int i, int i1) {

                //Check if Video is Buffering
                //Condition if video Buffering starts
                if (i == mediaPlayer.MEDIA_INFO_BUFFERING_START){

                    progressBar.setVisibility( View.VISIBLE );

                } //Condition if Video Buffering Ends
                else if (i == mediaPlayer.MEDIA_INFO_BUFFERING_END){

                    progressBar.setVisibility( View.GONE );

                }

                return false;
            }
        } );

Here it is How Full code of MainActivity Looks Like.

MainActivity.java

package in.cyberworldtechnologies.androidvideoplayer;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;

import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.MediaController;
import android.widget.ProgressBar;
import android.widget.VideoView;

public class MainActivity extends AppCompatActivity {

    private VideoView videoView;
    private ProgressBar progressBar;
    private String videourl;

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate( savedInstanceState );
        setContentView( R.layout.activity_main );

        videoView = findViewById( R.id.video_view );
        progressBar = findViewById( R.id.video_loading );

        videourl = "https://firebasestorage.googleapis.com/v0/b/android-video-player-bc76d.appspot.com/o/BigBuckBunny.mp4?alt=media&token=a19034bc-004c-4577-a06e-e07139c6800c";
        Uri uri = Uri.parse( videourl );

        //Set MediaController
        MediaController controller = new MediaController( MainActivity.this );
        controller.setMediaPlayer( videoView );
        controller.setAnchorView(videoView);
        videoView.setMediaController( controller );

        //set video url
        videoView.setVideoURI( uri );
        videoView.requestFocus();

        //Set OnInfoListener for the VideoView
        videoView.setOnInfoListener( new MediaPlayer.OnInfoListener() {
            @Override
            public boolean onInfo(MediaPlayer mediaPlayer, int i, int i1) {

                //Check if Video is Buffering
                //Condition if video Buffering starts
                if (i == mediaPlayer.MEDIA_INFO_BUFFERING_START){

                    progressBar.setVisibility( View.VISIBLE );

                } //Condition if Video Buffering Ends
                else if (i == mediaPlayer.MEDIA_INFO_BUFFERING_END){

                    progressBar.setVisibility( View.GONE );

                }

                return false;
            }
        } );


        //Set OnPreparedListener. Do Something When video is ready to play
        videoView.setOnPreparedListener( new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mediaPlayer) {

                videoView.start();
                progressBar.setVisibility( View.GONE );


            }
        } );


        //This is OnCompleteListener. Do Something when Video is Completed
        videoView.setOnCompletionListener( new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mediaPlayer) {


                videoView.seekTo( 0 );

            }
        } );


    }
}

Yes, We did it. But Before Testing add Internet Permission in Manifest File.

<uses-permission android:name="android.permission.INTERNET" />

And To allow traffic using HTTPS add this line in the application tag of your Manifest File.

android:usesCleartextTraffic="true"

Now Test it. Hope you’ll get the result of the Android Video Player. If you have any doubts regarding Android Video Player Please Let me in Comment Section or Send a Message through Contact Page.

Leave a Reply

Your email address will not be published. Required fields are marked *