All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

How To Play Video In Android From URL

Last Updated : Mar 11, 2024

How To Play Video In Android From URL

In this article we will show you the solution of how to play video in android from url, for showing the video in our android application we will use the widget named VideoView widget.

The VideoView widget is perfectly capable of playing media files, and the formats supported by the VideoView are 3GP and MP4.

With the help of VideoView you we can play media files from the local storage and also from the internet. We will try to implement this project using Java language.

Before moving to next, please follow every step from beginning to the end. if you don’t follow the steps then it will be difficult for you to learn “How to play video in android from URL.

Step By Step Guide On How To Play Video In Android From URL :-

Xml Code

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<!-- adding VideoView to the layout -->
<VideoView
android:id="@+id/videoView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true" />
</RelativeLayout>
MainActivity.java
import android.net.Uri;
import android.os.Bundle;
import android.widget.MediaController;
import android.widget.VideoView;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
// Your Video URL
String videoUrl = "https://media.geeksforgeeks.org/wp-content/uploads/20201217192146/Screenrecorder-2020-12-17-19-17-36-828.mp4?_=1";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// finding videoview by its id
VideoView videoView = findViewById(R.id.videoView);
// Uri object to refer the
// resource from the videoUrl
Uri uri = Uri.parse(videoUrl);
// sets the resource from the
// videoUrl to the videoView
videoView.setVideoURI(uri);
// creating object of
// media controller class
MediaController mediaController = new MediaController(this);
// sets the anchor view
// anchor view for the videoView
mediaController.setAnchorView(videoView);
// sets the media player to the videoView
mediaController.setMediaPlayer(videoView);
// sets the media controller to the videoView
videoView.setMediaController(mediaController);
// starts the video
videoView.start();
}
}
  1. In the first step we will create a new project, On the Welcome screen of Android Studio, just click on Create New Project and If by chance you have a project already opened, then kindly Go to File > New > New Project. Then select whatever Project Template window, select Empty Activity and then click Next. kindly enter your App Name in the Name field and then select Java from the Language drop-down menu. Adding the Internet Permission
  2. Kindly Navigate to app > manifest > AndroidManifest.xml and then internet permission to that file as I have done below. <uses-permission android:name="android.permission.INTERNET"/>
  3. Kindly Navigate to the app > res > layout > activity_main.xml and then add the below code to that file. Above is the code for the activity_main.xml file.
  4. Now Go to the MainActivity.java file and kindly refer to the following code. Above is the code for the MainActivity.java file. Comments are actually added inside the code to understand the code even more in detail.

Conclusion :-

That’s it. This is the most simple and easy way to play video on your android application with the help of a URL.

You can implement the code by yourself or by copying our source code.

I hope this article on how to play video in android from url helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Anjali

Experienced Computer Programmer with a broad range of experience in technology. Strengths in application development and Object Oriented architecture design, front end programming, usability and multimedia technology. Expert in coding languages such as C, C++ Java, JavaScript, PHP and more.

Follow Anjali On Linkedin 🡪