VideoView in Android
Last Updated :
25 Jul, 2022
VideoView is a UI widget that is used to display video content to the users within android applications. We can add video in this video view from different resources such as a video stored on the user device, or a video from a server. In this article, we will take a look at How to use Video View in the android application. A sample video is given below to get an idea about what we are going to do in this article.
Note: This Android article covered in both Java and Kotlin languages.
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.
Step 2: Working with the activity_main.xml file
Navigate to app > res > layout > activity_main.xml and add the code below. Comments are added in the code to get to know in detail.
XML
<? xml version = "1.0" encoding = "utf-8" ?>
< RelativeLayout
android:id = "@+id/idRLContainer"
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 = "20dp"
android:gravity = "center"
android:padding = "10dp"
android:text = "Video View in Android"
android:textAlignment = "center"
android:textColor = "@color/black"
android:textSize = "20sp"
android:textStyle = "bold" />
< VideoView
android:id = "@+id/idVideoView"
android:layout_width = "match_parent"
android:layout_height = "wrap_content"
android:layout_below = "@id/idTVHeading"
android:layout_centerInParent = "true" />
</ RelativeLayout >
|
Step 3: Working with the MainActivity file
Navigate to app > java > your app’s package name > MainActivity file and add the below code to it. Comments are added in the code to get to know in 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
val videoUrl = "Paste Your Video URL Here"
override fun onCreate(savedInstanceState: Bundle?) {
super .onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
videoView = findViewById(R.id.idVideoView)
val uri = Uri.parse(videoUrl)
videoView.setVideoURI(uri)
val mediaController = MediaController( this )
mediaController.setAnchorView(videoView)
mediaController.setMediaPlayer(videoView)
videoView.setMediaController(mediaController);
videoView.start();
}
}
|
Java
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;
public class MainActivity extends AppCompatActivity {
private VideoView videoView;
String videoUrl = "Paste Your Video URL Here" ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super .onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
videoView = findViewById(R.id.idVideoView);
Uri uri = Uri.parse(videoUrl);
videoView.setVideoURI(uri);
MediaController mediaController = new MediaController( this );
mediaController.setAnchorView(videoView);
mediaController.setMediaPlayer(videoView);
videoView.setMediaController(mediaController);
videoView.start();
}
}
|
Now run your application to see the output of it.
Output:
Share your thoughts in the comments
Please Login to comment...