How to play the video directly from server or URL in VideoView Android

Play video using URL in android in just 5 min

CodingwithSaud
3 min readJul 18, 2020

Create a new project in your android studio as I created in the screenshot provided below.

How to play video from url in videoview Android

Design your activity, Activity design is provided below.

How to play video from url in videoview Android

Before moving to next, Please follow the every step from start to end. if you didn’t follow the steps then you didn’t able to learn “How to play video from url in videoview Android”. After creating the design, you have to open video_view.java file which is back-end file for the activity and you have to declare and initialize some fields variables like in the screenshot.

You have to initialize video view and progressbar like this

After it make the function named “initializePlayer” and pass argument of type Uri and set the uri to the video view using setVideoURI and pass your video path as a parameter in this function. You have to set prepared listener and you need to pass prepared listener object which is the part of MediaPlayer Class. After it onPrepared function will be override. In this function we can start playing the video by calling the start function with video view object.

If you want to close the player after completed the video then simply you can set on Completion listener like this

To stop the play back of the video you can call stop play back function like this.

For the best practice, you have to override onPause(), onStop() and onSaveInstanceState()

onPause

You should pause the video by calling the pause function with video view object.

onStop

In this function, you should call releasePlayer function to clear the memory.

onSaveInstanceState

In this function, you can save the state of the activity, So I putted the current play back position of the video.

In this I called the getIntent function for getting the URL of the video from other activity but you can direct put your URL instead of getting intent. After it, I set the progress bar visibility to visible then we can get the play back time and assign to mCurrentPossition variable. Now I created the media controller object to display the pause, play, stop, forward and backward buttons.

10) After it, We have to create handler for keep checking, is video is playing or not. I have implemented the logic that if video is playing progress visibility keep gone and either video is not playing the progress bar will be visible.

--

--