In this tutorial you will learn:

  1. Play Videos using VideoView

Example 1: AndroidPlay Different Videos with VideoViews

A simple example written in Java that teaches you how to play various videos using videoView and play them.

The videos are played locally as raw videos using the setVideoURI method:

    videoView.setVideoURI(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.video));

Step 1: Create Project

The first step is to create a Android Project in Android Studio.

Step 2: Add Dependencies

No external dependencies are needed for this project. Feel free to migrate this project to androidx.

Step 3: Design Layouts

Add a VideoView and ListView in your layout as follows:

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="com.blogspot.programer27android.playvideo.MainActivity">

    <VideoView
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:id="@+id/videoview"
        android:layout_centerHorizontal="true"
        android:layout_alignParentTop="true"/>

    <ListView
        android:id="@+id/lvideo"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/videoview">
    </ListView>

</RelativeLayout>

Step 4: Add Videos

Add Videos as raw files in your project:

  1. Create a folder known as raw inside the res directory of your project.
  2. In there add the mp4 files you want played.

Step 5: Write Code

In your MainActivity, reference the VideoView and ListView:

        videoView=findViewById(R.id.videoview);
        listView=findViewById(R.id.lvideo);

Then create an arraylist and add dummy video names:

        videoList= new ArrayList<>();
        videoList.add("video");
        videoList.add("vedeo2");
        videoList.add("video3");
        videoList.add("video4");
        videoList.add("video5");

Bind those video names to a Listview using an arrayadapter:


        adapter= new ArrayAdapter(this,android.R.layout.simple_list_item_1,videoList);
        listView.setAdapter(adapter);

Listen to ListView itemClicks and play the appropriate video:

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                switch (position) {
                    case 0:
                        videoView.setVideoURI(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.video));
                        break;
                    case 1:
                        videoView.setVideoURI(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.vedeo2));
                        break;
                    case 2:
                        videoView.setVideoURI(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.video3));
                        break;
                    case 3:
                        videoView.setVideoURI(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.video4));
                        break;
                    case 4:
                        videoView.setVideoURI(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.video5));
                        break;
                    case 5:
                        videoView.setVideoURI(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.videom));
                        break;
                    default:
                        break;
                }
                videoView.setMediaController(new MediaController(MainActivity.this));
                videoView.requestFocus();
                videoView.start();
            }
        });

Here is the full code:

MainActivity.java

public class MainActivity extends AppCompatActivity {
    VideoView videoView;
    ListView listView;
    ArrayList<String>videoList;
    ArrayAdapter adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        videoView=findViewById(R.id.videoview);
        listView=findViewById(R.id.lvideo);
        videoList= new ArrayList<>();
        videoList.add("video");
        videoList.add("vedeo2");
        videoList.add("video3");
        videoList.add("video4");
        videoList.add("video5");
        videoList.add("videom");

        adapter= new ArrayAdapter(this,android.R.layout.simple_list_item_1,videoList);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                switch (position) {
                    case 0:
                        videoView.setVideoURI(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.video));
                        break;
                    case 1:
                        videoView.setVideoURI(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.vedeo2));
                        break;
                    case 2:
                        videoView.setVideoURI(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.video3));
                        break;
                    case 3:
                        videoView.setVideoURI(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.video4));
                        break;
                    case 4:
                        videoView.setVideoURI(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.video5));
                        break;
                    case 5:
                        videoView.setVideoURI(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.videom));
                        break;
                    default:
                        break;
                }
                videoView.setMediaController(new MediaController(MainActivity.this));
                videoView.requestFocus();
                videoView.start();
            }
        });
    }
}

Run

Copy the code into your project or download the code in the reference links below.

Reference

Find the reference links below:

Number Link
1. Download code
2. Follow code author