Introduction
Adding video to an Android app can be done just by adding a few lines of code. In this article, I will demonstrate how to embed a video to an Android app. This article will also explain how to control the video by playing, pausing and by seeking back and forth. This is really easy to do.
Steps to embed video in your Android Application
Step 1
Open your Android project and create a new Android project
Step 2
Create a new folder named "raw" in your Android project's "res" folder and place your video file inside the "raw" folder.
You can now see the video content is successfully added into your Android Studio project by viewing the "raw" subfolder under "res" folder.
Step 3
To add a video player in the activity_main.xml layout, go to "Design" tab of your activity_mail.xml. Goto "Widgets" under palette and drag the "VideoView" into your Android Aplication Layout. I have named my VideoView's id as "videoView".
Step 4
Write code to attach video to the VideoView.
On the onCreate() method add the following code,
- VideoView videoView = (VideoView) findViewById(R.id.videoView); //casting to VideoView is not Strictly required above API level 26
- videoView.setVideoPath("android.resource://" + getPackageName() + "/" + R.raw.videoclip); //set the path of the video that we need to use in our VideoView
- videoView.start(); //start() method of the VideoView class will start the video to play
Step 5
Run your Application on your physical device or emulator. But you have noticed that there is no controls on that video.
So to add controls on your video, use "MediaController" class.
Add the following lines of code, after the code of setting up the Path to your video.
- MediaController mediaController = new MediaController(this);
-
- mediaController.setAnchorView(videoView);
-
- videoView.setMediaController(mediaController);
Run your application. Now, you can see that your video is playing, along with controls to play, pause, seek back and forth.
Here I have provided my source code, for your reference.
activity_mail.xml
- <?xml version="1.0" encoding="utf-8"?>
- <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context=".MainActivity">
-
- <VideoView
- android:id="@+id/videoView"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- app:layout_constraintBottom_toBottomOf="parent"
- app:layout_constraintEnd_toEndOf="parent"
- app:layout_constraintStart_toStartOf="parent"
- app:layout_constraintTop_toTopOf="parent" />
- </androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java
- package com.thetechiechum.videodemo;
-
- import androidx.appcompat.app.AppCompatActivity;
-
- import android.os.Bundle;
- import android.widget.MediaController;
- import android.widget.VideoView;
-
- public class MainActivity extends AppCompatActivity {
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
-
- VideoView videoView = (VideoView) findViewById(R.id.videoView);
- videoView.setVideoPath("android.resource://" + getPackageName() + "/" + R.raw.videoclip);
-
- MediaController mediaController = new MediaController(this);
-
- mediaController.setAnchorView(videoView);
-
- videoView.setMediaController(mediaController);
- videoView.start();
- }
- }
Summary
In this article, you have seen the steps to embed the video file into your Android Application. I hope you have understoond the usage of MediaController class while embedding video into your Android Application.