Open In App

How to Play Video from URL in Android?

Last Updated : 07 Aug, 2024
Summarize
Comments
Improve
Suggest changes
Share
Like Article
Like
Report

In this article, you will see how to play a video from a URL on Android. For showing the video in our Android application we will use the VideoView widget. The VideoView widget is capable of playing media files, and the formats supported by the VideoView are 3gp and MP4. By using VideoView you can play media files from the local storage and also from the internet. A sample GIF is given below to get an idea about what we are going to do in this article.

Note: that we are going to implement this project using the Java language. 


Now let's see the step-by-step implementation of the above application shown in the gif.

Step By Step Implementation

Step 1: Create a new project

So the first step is to create a new project, On the Welcome screen of Android Studio, click on Create New Project and If you have a project already opened, Go to File > New > New Project. Then select a Project Template window, select Empty Activity and click Next. Enter your App Name in the Name field and select Java from the Language drop-down menu.

Step 2: Add the Internet Permission

Navigate to app > manifest > AndroidManifest.xml and the internet permission to that file as shown below.

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

Step 3: Working with the activity_main.xml

Navigate to the app > res > layout > activity_main.xml and add the below code to that file.

Below is the code for the activity_main.xml file. 

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

Step 4: Working with the MainActivity.java

Go to the MainActivity.java file and refer to the following code. Below is the code for the MainActivity.java file. Comments are added inside the code to understand the code in more detail.

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 {

    // URL of the video to be played
    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 the VideoView by its ID
        VideoView videoView = findViewById(R.id.videoView);

        // Creating a Uri object to refer to the resource from the videoUrl
        Uri uri = Uri.parse(videoUrl);
        
        // Setting the video URI to the VideoView
        videoView.setVideoURI(uri);
        
        // Creating an object of MediaController class
        MediaController mediaController = new MediaController(this);
        
        // Setting the anchor view for the MediaController
        mediaController.setAnchorView(videoView);
        
        // Associating the MediaController with the VideoView
        videoView.setMediaController(mediaController);
        
        // Starting the video playback
        videoView.start();
    }

    @Override
    protected void onPause() {
        super.onPause();
        VideoView videoView = findViewById(R.id.videoView);
        videoView.pause();
    }

    @Override
    protected void onResume() {
        super.onResume();
        VideoView videoView = findViewById(R.id.videoView);
        videoView.start();
    }
}

Output:


Article Tags :

Similar Reads