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
<!--internet permissions and network state permission--> < 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 xmlns:android = "http://schemas.android.com/apk/res/android" xmlns:tools = "http://schemas.android.com/tools" android:id = "@+id/container" android:layout_width = "match_parent" android:layout_height = "match_parent" android:orientation = "vertical" tools:context = ".MainActivity" > <!--on below line we are creating a text for heading of our app--> < 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" /> <!-- adding VideoView to the layout --> < 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() { // on below line we are creating // a variable for our video view. lateinit var videoView: VideoView // on below line we are creating // a variable for our video url. var videoUrl = "https://media.w3wiki.net/wp-content/uploads/20201217192146/Screenrecorder-2020-12-17-19-17-36-828.mp4?_=1" override fun onCreate(savedInstanceState: Bundle?) { super .onCreate(savedInstanceState) setContentView(R.layout.activity_main) // on below line we are initializing // our buttons with id. videoView = findViewById(R.id.videoView); // on below line we are creating // uri for our video view. val uri: Uri = Uri.parse(videoUrl) // on below line we are setting // video uri for our video view. videoView.setVideoURI(uri) // on below line we are creating variable // for media controller and initializing it. val mediaController = MediaController( this ) // on below line we are setting anchor // view for our media controller. mediaController.setAnchorView(videoView) // on below line we are setting media player // for our media controller. mediaController.setMediaPlayer(videoView) // on below line we are setting media // controller for our video view. videoView.setMediaController(mediaController) // on below line we are // simply starting our video view. videoView.start() } } |
Now run your application to see the output of it.
Output:
Contact Us