Twitter LogoFacebook Logo
Displaying Videos using the VideoView
Hello, in this video, I will show you how to use the VideoView to display video.
By: King

Download project here:

https://codeible.com/coursefiles/androidvideoview

Hello, in this tutorial, I will show you how to use the VideoView to display video. 


We’ll begin by displaying a video using a local video file in the project, then we’ll move on and use video file on the device, and lastly, we’ll use a Uniform Resource Locator or URL for an online video.

To begin, create a new empty Android project. Go to the activity_main.xml file and add a VideoView and button to the layout. 

Go to the MainActivity file and create a reference for the VideoView and Button inside the onCreate() method and attach a onClickListener to the button.

Playing the video

To play a video using a file in the project, create a new Android Resource Directory call raw for our video files. Then drag and drop a video into the raw directory that you want to display. 

Image from Codeible.com

Once the video file is added into the project, add it to the VideoView so it can display it. 

Declare a string call videoPath in the onClick() method and assign it with the path to the video. 

Use "android.resource://" + getPackageName() + "/" and then the reference to the video file in the raw directory.

String videoPath = "android.resource://" + getPackageName() + "/" + R.raw.intro;

Create a Uniform Resource ID or Uri with the path and call setVideoUri() from the VideoView object to add the video to it.

Uri uri = Uri.parse(videoPath);
videdoView.setVideoUri(uri);

Now that the video is added to the VideoView, take the VideoView object and call the start() method to play the video.

videoView.start();

If we run the app and click on the button, the video will start.

Displaying a video on the device

Now let’s see how we can play a video from the device. Begin by dragging a video file into the emulator so there will be a file for us to work with.

Image from Codeible.com

In Android Studio, register a file chooser intent for the app using registerForActivityResult() inside the onCreate() method, before the setOnClickListener code. 


It takes 2 parameters. 

1) The first parameter asks for a ActivityResultContract. 

Use the GetContent() contract from ActivityResultContracts for the file chooser intent. 

2) The second parameter asks for a ActivityResultCallback. 

When a file is selected from file chooser, it’ll bring us back to our app and execute the code inside this callback method. We can use the Uri object returned to add the video to the VideoView. 

Grab the videoView and call setVideoUri to add the video. Then call start() to play the video. 

registerForActivityResult(
        new ActivityResultContracts.GetContent(),
        new ActivityResultCallback<Uri>() {
             @Override
             public void onActivityResult(Uri result) {
                        videoView.setVideoUri(uri);
                        videoView.start();
             }
        }
);

Launching the File Chooser Intent

Now that we registered a file chooser intent on our app, we want to launch it when we click on the button.


The registerForActivityResult() method returns a ActivityResultLauncher that we can use to launch the file chooser. 

Create a ActivityResultLauncher variable call fileChooser  and assign the launcher to it. 

Remove the code inside the onClick() method, then grab the fileChooser launcher, and call the launch() method. 


It takes one parameter and it is for the type of file we can choose.

 For videos, use video / and then the asterisk symbol. 

button.setOnClickListener(new View.OnClickListener() {
       @Override
             public void onClick(View view) {
                     fileChooser.launch("video/*");
             }
       });

If we run the app and click on the button, it’ll bring us to the file chooser activity. If we select the video, it’ll bring us back to the app and play it on the VideoView.

Playing an Online Video

Now let’s see how we can display an online video. 


In the manifests file of the project, add the INTERNET permission above application code.

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

Go to the MainActivity file and delete the code inside the onClick() method. 

button.setOnClickListener(new View.OnClickListener() {
       @Override
             public void onClick(View view) {

             }
       });

The way we put an online video into the VideoView is the same as how displayed the video in the project.

Create a String variable call videoPath. Instead of using the path we defined earlier, use the Web Address for the video online.

Then create a Uri object using the path and add it to the VideoView by calling setVideoUri().

Lastly, call start() to play the video.

If we run the app and click on the button, it’ll play the video that was retrieved online.


Sign In