Play Video From URL in Android Using Kotlin
Many applications display video within their application to display video content. The videos are huge in size so it is not practically possible to add each video to our project. This will increase the size of our application. So for tackling this many applications plays the video from the URL within their android application. In this video, we will take a look at How to Play video from URL in android application using Kotlin.
Note: If you are looking to play a video from a URL in an android application using Java. Check out the following article: How to play video from URL in android using Java
Step by Step Implementation
Step 1: Create a New Project in Android Studio
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Kotlin as the programming language.
Step 2: Add the Internet Permission
Navigate to app > manifest > AndroidManifest.xml and the internet permission to that file as shown below.
XML
< uses-permission android:name = "android.permission.INTERNET" />
< uses-permission android:name = "android.permission.ACCESS_NETWORK_STATE" />
|
Step 3: Working with the activity_main.xml file
Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file. Comments are added inside the code to understand the code in more detail.
XML
<? xml version = "1.0" encoding = "utf-8" ?>
< RelativeLayout
android:id = "@+id/container"
android:layout_width = "match_parent"
android:layout_height = "match_parent"
android:orientation = "vertical"
tools:context = ".MainActivity" >
< TextView
android:id = "@+id/idTVHeading"
android:layout_width = "match_parent"
android:layout_height = "wrap_content"
android:layout_margin = "8dp"
android:gravity = "center"
android:padding = "4dp"
android:text = "Play Video from URL"
android:textAlignment = "center"
android:textColor = "@color/purple_200"
android:textSize = "18sp"
android:textStyle = "bold" />
< VideoView
android:id = "@+id/videoView"
android:layout_width = "match_parent"
android:layout_height = "wrap_content"
android:layout_below = "@id/idTVHeading"
android:layout_centerInParent = "true" />
</ RelativeLayout >
|
Step 4: Working with the MainActivity.kt file
Go to the MainActivity.kt file and refer to the following code. Below is the code for the MainActivity.kt file. Comments are added inside the code to understand the code in more detail.
Kotlin
package com.gtappdevelopers.kotlingfgproject
import android.net.Uri
import android.os.Bundle
import android.widget.MediaController
import android.widget.VideoView
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
lateinit var videoView: VideoView
var videoUrl =
override fun onCreate(savedInstanceState: Bundle?) {
super .onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
videoView = findViewById(R.id.videoView);
val uri: Uri = Uri.parse(videoUrl)
videoView.setVideoURI(uri)
val mediaController = MediaController( this )
mediaController.setAnchorView(videoView)
mediaController.setMediaPlayer(videoView)
videoView.setMediaController(mediaController)
videoView.start()
}
}
|
Now run your application to see the output of it.
Output:
Last Updated :
19 Jun, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...