How to show ProgressBar while loading a video in Android VideoView

Author: | Posted in Android, Quick Tips No comments

Tutorial for how to play video in Android VideoView and how to show ProgressBar while loading a video in VideoView.

Android provides a widget VideoView to play video files.

Here is the link for supported video formats for Android.

http://developer.android.com/guide/appendix/media-formats.html#core

In this tutorial we will learn how to play video using android VideoView.
We can play video from our app resource folder, or we can play video from phone memory/sd card, or we can play video which is on server(live stream also) and how to show ProgressBar untill video play in VideoView.

Create a new Android project see here for insturction.
After that add VideoView and ProgressBar in layout(activity_main.xml) as shown below.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    <VideoView
        android:id="@+id/videoview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true" />
    <ProgressBar
        android:id="@+id/progressbar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:visibility="gone" />
</RelativeLayout>

and in your activity file(MainActivity.java) use/define this VideoView, ProgressBar like this

public class MainActivity extends Activity {
    ProgressBar progressBar = null;
    VideoView videoView = null;
    String videoUrl = "video path here";
    Context context = null;
    @Override
    public void onCreate(Bundle iclic) {
        super.onCreate(iclic);
        context = null;
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        videoView = (VideoView) findViewById(R.id.videoview);
        progressBar = (ProgressBar) findViewById(R.id.progressbar);
        Uri videoUri = Uri.parse(videoUrl);
        videoView.setVideoURI(videoUri);
        videoView.start();
        progressBar.setVisibility(View.VISIBLE);
        videoView.setOnPreparedListener(new OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                // TODO Auto-generated method stub
                mp.start();
                mp.setOnVideoSizeChangedListener(new OnVideoSizeChangedListener() {
                    @Override
                    public void onVideoSizeChanged(MediaPlayer mp, int arg1,
                            int arg2) {
                        // TODO Auto-generated method stub
                        progressBar.setVisibility(View.GONE);
                        mp.start();
                    }
                });
            }
        });
    }
}

Set video uri in VideoView and start playing video in VideoView using start(); method, and set ProgressBar to visible so it look like that VideoView is loading.

VideoView takes some time to play video then set a OnPreparedListener() for VideoView so we can know when VideoView is being prepared.
The key-trick is place a OnVideoSizeChangedListener() in OnPreparedListener() to check the start moment of VideoView and hide your ProgressBar.