Load/Play Video From URL Android Kotlin VideoView Programmatically

load and play video from url android kotlin

In this load and play video from URL android kotlin tutorial example, we will learn to play or load video from URL or server.

You can load video from URL in android Videoview programmatically.

A small android app which include play, stop, play continuously and play once options.

You can implement this module any other android app as it includes only one class which is simple and easy.

It will be small video control app with kotlin and android studio.

First, check the output of this load and play video from URL Android and then we will implement it step by step.

Wait for one minute

Here, we will load or play video from URL. If you wish to load video from Android studio’s raw directory, then check here: Videoview android Kotlin example tutorial.

Step 1: Create a new project in the android studio.

Step 2: Updating AndroidManifest.xml file

add internet permission between <manifest>….</manifest> tag.

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

Step 3: Updating activity_main.xml file

Add below code in activity_main.xml file

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <Button
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:id="@+id/btnonce"
            android:text="Once"/>
        <Button
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:id="@+id/btnconti"
            android:text="continuously"/>
        <Button
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:id="@+id/btnstop"
            android:text="Stop"/>
        <Button
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:id="@+id/btnplay"
            android:text="play"/>

    </LinearLayout>
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <VideoView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_alignParentBottom="true"
            android:layout_alignParentEnd="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentStart="true"
            android:layout_alignParentTop="true"
            android:id="@+id/vv"/>
        <ProgressBar
            android:layout_width="200dp"
            android:layout_height="200dp"
            android:id="@+id/progrss"
            android:visibility="gone"
            android:layout_centerInParent="true"/>

    </RelativeLayout>

</LinearLayout>

Step 4: Updating MainActivity.kt class : 

Add below code to MainActivity.kt

import android.net.Uri
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.Button
import android.widget.MediaController
import android.widget.ProgressBar
import android.widget.VideoView

class MainActivity : AppCompatActivity() {

    private var btnonce: Button? = null
    private var btncontinuously: Button? = null
    private var btnstop: Button? = null
    private var btnplay: Button? = null
    private var vv: VideoView? = null
    private var mediacontroller: MediaController? = null
    private var uri: Uri? = null
    private var isContinuously = false
    private var progressBar: ProgressBar? = null

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

        progressBar = findViewById(R.id.progrss) as ProgressBar
        btnonce = findViewById(R.id.btnonce) as Button
        btncontinuously = findViewById(R.id.btnconti) as Button
        btnstop = findViewById(R.id.btnstop) as Button
        btnplay = findViewById(R.id.btnplay) as Button
        vv = findViewById(R.id.vv) as VideoView

        mediacontroller = MediaController(this)
        mediacontroller!!.setAnchorView(vv)
        val uriPath = "http://www.demonuts.com/Demonuts/smallvideo.mp4" //update package name
        uri = Uri.parse(uriPath)

        vv!!.setOnCompletionListener {
            if (isContinuously) {
                vv!!.start()
            }
        }

        btnstop!!.setOnClickListener { vv!!.pause() }

        btnplay!!.setOnClickListener { vv!!.start() }

        btnonce!!.setOnClickListener {
            isContinuously = false
            progressBar!!.visibility = View.VISIBLE
            vv!!.setMediaController(mediacontroller)
            vv!!.setVideoURI(uri)
            vv!!.requestFocus()
            vv!!.start()
        }

        btncontinuously!!.setOnClickListener {
            isContinuously = true
            progressBar!!.visibility = View.VISIBLE
            vv!!.setMediaController(mediacontroller)
            vv!!.setVideoURI(uri)
            vv!!.requestFocus()
            vv!!.start()
        }

        vv!!.setOnPreparedListener { progressBar!!.visibility = View.GONE }

    }
}

Step 5: Description of MainActivity.java

  • Below code
mediacontroller = MediaController(this)
mediacontroller!!.setAnchorView(vv)
  • will set mediacontrollers(previous, pause, play, next buttons) to videoview.
  • Below code
 val uriPath = "http://www.demonuts.com/Demonuts/smallvideo.mp4" //update package name
 uri = Uri.parse(uriPath)
  • Here you need to update your server URL to the video.
  vv!!.setOnCompletionListener {
            if (isContinuously) {
                vv!!.start()
            }
        }
  • As you can see we are using boolean isContinuously to detect which button is pressed, Either “ONCE” or “Continuously .
  • We are managing isContinuously and starting a video on both button’s click method as below.
 btnonce!!.setOnClickListener {
            isContinuously = false
            progressBar!!.visibility = View.VISIBLE
            vv!!.setMediaController(mediacontroller)
            vv!!.setVideoURI(uri)
            vv!!.requestFocus()
            vv!!.start()
        }

        btncontinuously!!.setOnClickListener {
            isContinuously = true
            progressBar!!.visibility = View.VISIBLE
            vv!!.setMediaController(mediacontroller)
            vv!!.setVideoURI(uri)
            vv!!.requestFocus()
            vv!!.start()
        }

We will remove progressBar when a video is prepared to run. See below code

vv!!.setOnPreparedListener { progressBar!!.visibility = View.GONE }

So that is all for this play video url android kotlin example tutorial. Thank you for your time, keep visiting for more tutorials.

Download Source Code for load and play video from url android kotlin

[sociallocker]Download Android Kotlin_VideoView URL Example[/sociallocker]