Android Video Streamimg with Android VideoView and MediaController

In this tutorial, we are going to learn how to create android video streaming application with android VideoView and MediaController.

Android VideoView is used to display a video file. The VideoView class can load images from various sources (such as resources or content providers), takes care of computing its measurement from the video so that it can be used in any layout manager, and provides various display options such as scaling and tinting.

Note: VideoView does not retain its full state when going into the background. In particular, it does not restore the current play state, play position, selected tracks, or any subtitle tracks added via addSubtitleSource(). Applications should save and restore these on their own in onSaveInstanceState(Bundle) and onRestoreInstanceState(Bundle).

The following are media supported formats for android VideoView – H.263, H.264 AVC, MPEG-4 SP, VP8.

You can only use the VideoView to display video without any playback controls. To add playback controls, we are going to use the MediaController class.

The MediaController is a view containing controls for a MediaPlayer. Typically contains the buttons like “Play/Pause”, “Rewind”, “Fast Forward” and a progress slider. It takes care of synchronizing the controls with the state of the MediaPlayer.

The way to use this class is to instantiate it programmatically. The MediaController will create a default set of controls and put them in a window floating above your application. Specifically, the controls will float above the view specified with setAnchorView(). The window will disappear if left idle for three seconds and reappear when the user touches the anchor view.

There is any option available in android for display video file. If you want learn more about Android Media Player, I will suggest you read my android tutorial on Android Media Player.

Before we dive into more details, it is important for us to understand what we are planning to achieve. Below is the screen-shot of the application we will be creating.

android videoview

Lets start to soil our hands in code. Start up your IDE. For this tutorial, I am using the following tools and environment, feel free to use what works for you.

Windows 10

Android Studio

Sony Xperia ZL

Min SDK 14

Target SDK 23

To create a new android application project, follow the steps as stipulated below.

Go to File menu

Click on New menu

Click on Android Application

Enter Project name: AndroidVideoView

Package: com.inducesmile.androidvideoview

Select Blank Activity

Name your activity : MainActivity

Keep other default selections

Continue to click on next button until Finish button is active, then click on Finish Button.



We are going to update our project strings.xml file located in the values folder inside the res folder. Open the file and add the code below to it.

<string name="app_name">Android VideoView</string>
    <string name="app_name">Android VideoView</string>


Open the colors.xml file in the same location as the strings.xml file and add the code below to the file.

<?xml version="1.0" encoding="utf-8"?>
    <color name="colorPrimary">#3F51B5</color>
    <color name="colorPrimaryDark">#303F9F</color>
    <color name="colorAccent">#FF4081</color>
    <color name="colorBlack">#000000</color>
    <color name="colorDivider">#B6B6B6</color>



Since our video file path point to remote source, we are going to add user permission for INTERNET.

Open the Manifest.xml file located in Manifest folder and add the code below to the file.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android=""
    <uses-permission android:name="android.permission.INTERNET"/>
        <activity android:name=".MainActivity">
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />



We are going to add a VideoView control in the main layout file. Set the layout width and height to match_parent. Go to the layout folder inside the res folder, open the activity_main.xml and add the code below to the file.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android=""
        android:layout_height="match_parent" />

In the MainActivity class, we are going to create instances of the VideoView control and MediaController. The path to the video source id also add as a string.

The VideoView instance sets the path to the video with setVideoPath() method. In order to add the playback controls, the MediaController object calls it setAnchorView() method with the VideoView object as parameter.

To start the video, its start() method is call. Open the MainActivity file and paste the below code on the file.

import android.os.Bundle;
import android.widget.MediaController;
import android.widget.VideoView;
public class MainActivity extends AppCompatActivity {
    private VideoView videoView;
    private MediaController mediaController;
    private static final String VIDEO_PATH = "";
    protected void onCreate(Bundle savedInstanceState) {
        videoView = (VideoView)findViewById(;
        mediaController = new MediaController(MainActivity.this);

This brings us to the end of this tutorial. I hope that you have learn something. Run your app and see for yourself.

You can download the code for this tutorial below. If you are having hard time downloading the tutorial, kindly contact me.

Remember to subscribe with your email address so that you will be among the first to receive my new android blog post once it is published.

Please if you love this tutorial, kindly download my android app – Complete Mathematics – in Google Play Store and let me know what you think about it.

One Response

  1. Avatar

Add a Comment