Video Streaming in Android (Java & Kotlin)

Hari Bhosale

Harikrushna Bhosale

Aug 12, 2018

In this blog we will stream the video file and showing using VideoView component with a MediaController object to let the user control playback. We will also provide demonstration for the android app with sample source code for Java as well as Kotlin.

1. Create New Project

Create new project using eclipse / Android studio, give the name, configure the respective details and create MainActivity class and layout to use the code in this tutorial.

Let's first configure the project's manifest for streaming media. Open the manifest file of your project and switch to XML editing in your IDE. For streaming media, you need internet access, so add the following permission inside the manifest element:

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

2. VideoView

Step 1

The Android platform provides the VideoView class in which you can play video files. Let's add one to the main layout file:

<VideoView
 android:id="@+id/myVideo"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 android:layout_centerInParent="true" />

Alter the parent layout to suit your own app if necessary. We give the VideoView instance an id attribute so that we can refer to it later. You may need to adjust the other layout properties for your own design.

Step 2

Now let's retrieve a reference to the VideoView instance in code. Open your app's main Activity class and add the following additional imports:

import android.net.Uri; 
import android.widget.MediaController;
import android.widget.VideoView;

Your Activity class should already contain the onCreate method in which the content view is set:

Java Code

@Override
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
}

Kotlin Code

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
}

After the setContentView line, let's get a reference to the VideoView instance as follows, using the id we set in the XML layout:

Java Code

VideoView videoView = (VideoView)findViewById(R.id.myVideo);

Kotlin Code

val videoView = findViewById<VideoView>(R.id.myVideo) as VideoView

3. Stream/Play Video File

Step 1

Now we can stream/play a video file to the app. Prepare the URI for the endpoint as follows:

Java Code

String videoAddress = "http://harikrushnabhosale.com/images/Demo.MP4";
Uri videoUri = Uri.parse(videoAddress);

Kotlin Code

val videoAddress = "http://harikrushnabhosale.com/images/Demo.MP4"
val videoUri = Uri.parse(videoAddress)

You will of course need to use the remote address for the video file you want to stream. The example here is a public domain video file hosted on the Internet Archive. We parse the address string as a URI so that we can pass it to the VideoView object:

Java Code

videoView.setVideoURI(videoUri);

Kotlin Code

videoView.setVideoURI(videoUri)

Now you can simply start playback:

Java Code

videoView.start();

Kotlin Code

videoView.start()

The Android operating system supports a range of video, Audio, media formats, with each device often supporting additional formats on top of this.

The Android operating system supports a range of video, Audio, media formats, with each device often supporting additional formats on top of this.

As you can see in the Developer Guide, video file formats supported include 3GP, MP4, WEBM, and MKV, depending on the format used and on which platform level the user has installed.

4. Add Playback Controls

We've implemented video playback, but the user will expect and be accustomed to having control over it. Again, the Android platform provides resources for handling this using familiar interaction via the MediaController class.

In your Activity class's onCreate method, before the line in which you call start on the VideoView, create an instance of the class:

Java Code

MediaController videoControl = new MediaController(this);

Kotlin Code

val videoControl = MediaController(this))

Next, set it to use the VideoView instance as its anchor:

Java Code

videoControl.setAnchorView(videoView);

Kotlin Code

videoControl.setAnchorView(videoView)

And finally, set it as the media controller for the VideoView object:

Java Code

videoView.setMediaController(videoControl);

Kotlin Code

videoView.setMediaController(videoControl)

When you run the app now, the user should be able to control playback of the streaming video, including fast forward and rewind buttons, a play/pause button, and a seek bar control.

The seek bar control is accompanied by the length of the media file on the right and the current playback position on the left. As well as being able to tap along the seek bar to jump to a position in the file, the streaming status is indicated using the same type of display the user will be accustomed to from sites and apps like YouTube.

As you will see when you run the app, the default behavior is for the controls to disappear after a few moments, reappearing when the user touches the screen. You can configure the behavior of the MediaController object in various ways.

5. File wise source code

Java Code - MainActivity.java

import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.widget.MediaController;
import android.widget.VideoView;

class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        VideoView videoView = (VideoView)findViewById(R.id.myVideo);
        String videoAddress = "http://harikrushnabhosale.com/images/Demo.MP4";
        Uri videoUri = Uri.parse(videoAddress);
        videoView.setVideoURI(videoUri);
        videoView.start();
        MediaController videoControl = new MediaController(this);
        videoControl.setAnchorView(videoView);
        videoView.setMediaController(videoControl);
    }
}

Kotlin Code - MainActivity.kt

import android.app.Activity
import android.net.Uri
import android.os.Bundle
import android.view.View
import android.widget.MediaController
import android.widget.VideoView

class MainActivity : Activity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val videoView = findViewById<VideoView>(R.id.myVideo) as VideoView
        val videoAddress = "http://harikrushnabhosale.com/images/Demo.MP4"
        val videoUri = Uri.parse(videoAddress)
        videoView.setVideoURI(videoUri)
            videoView.start()
        val videoControl = MediaController(this)
        videoControl.setAnchorView(videoView)
        videoView.setMediaController(videoControl)
    }
}

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"
    android:paddingBottom="20dp"
    android:paddingLeft="20dp"
    android:paddingRight="20dp"
    android:paddingTop="20dp"
    android:background="#000000"
    tools:context=".MainActivity" >

    <VideoView
        android:id="@+id/myVideo"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_centerInParent="true" />

</RelativeLayout>

6. Video Demo

Thank You