Introduction
Android is one of the most popular operating systems for mobile. A lot of people use Android for multimedia purposes and entertainment. Video player is the most important element in multimedia. So, I will show you how to integrate video in your Android application using Android Studio. Android is the kernel based operating system. In Android, the user can modify the GUI components and source code.
Requirements
Steps to be followed
Carefully follow the below steps to integrat video in your Android application using Android Studio. I have included the source code too.
Step 1
Open the Android Studio and start a new project.
Step 2
Put the application name and company domain. If you wish to use C++ for coding the project. Mark the "Include C++ support", and click Next.
Step 3
Select the Android minimum SDK. After that, click Next.
Step 4
Choose "Basic Activity" and click Next.
Step 5
Put the activity name and layout name. Android Studio basically takes the java class name same as the activity name you provided. Now, click Finish.
Step 6
Go to activity_main.xml and click the text botton. This xml file contains the designing code for Android app. In the activity_main.xml file, copy and paste the below code.
Activity_main.xml code
- <android.support.constraint.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"
- android:orientation="vertical"
- tools:context="ganeshannt.playvideo.MainActivity">
-
- <Button
- android:id="@+id/button"
- android:layout_width="0dp"
- android:layout_height="wrap_content"
- android:layout_marginEnd="8dp"
- android:layout_marginStart="8dp"
- android:onClick="videoplay"
- android:text="Play video"
- app:layout_constraintLeft_toLeftOf="parent"
- app:layout_constraintRight_toRightOf="parent"
- app:layout_constraintTop_toTopOf="parent"
- tools:layout_constraintLeft_creator="1"
- tools:layout_constraintRight_creator="1"
- tools:layout_constraintTop_creator="1" />
-
- <VideoView
- android:id="@+id/videoView"
- android:layout_width="0dp"
- android:layout_height="wrap_content"
- tools:layout_constraintTop_creator="1"
- tools:layout_constraintRight_creator="1"
- android:layout_marginStart="8dp"
- android:layout_marginEnd="8dp"
- app:layout_constraintRight_toRightOf="parent"
- app:layout_constraintTop_toBottomOf="@+id/button"
- tools:layout_constraintLeft_creator="1"
- app:layout_constraintLeft_toLeftOf="parent" />
- </android.support.constraint.ConstraintLayout>
Step 7
In the MainActivity.java file, copy and paste the below code. Java is the back-end language for Android. Do not replace your package name, otherwise the app will not run.
MainActivity.java code
- package ganeshannt.playvideo;
- import android.net.Uri;
- import android.os.Bundle;
- import android.support.v7.app.AppCompatActivity;
- import android.view.View;
- import android.widget.Button;
- import android.widget.MediaController;
- import android.widget.VideoView;
-
- public class MainActivity extends AppCompatActivity{
-
- Button clk;
- VideoView videov;
- MediaController mediac;
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
-
- clk = (Button) findViewById(R.id.button);
- videov = (VideoView) findViewById(R.id.videoView);
- mediac = new MediaController(this);
-
- }
-
- public void videoplay(View v)
- {
- String videopath = "android.resource://ganeshannt.playvideo/"+R.raw.google;
- Uri uri = Uri.parse(videopath);
- videov.setVideoURI(uri);
- videov.setMediaController(mediac);
- mediac.setAnchorView(videov);
- videov.start();
- }
-
- }
Step 8Create Resources folder for pasting your video file as you play (res->>new->>folder->>res folder).
Change the folder path.
Step 9
Click the "Make Project" option and run it.
Step 10
Run the application >> choose Virtual Machine >> click OK.
Deliverables
Here, we have been successful in integration of video in Android app.
Don’t forgot to like and follow me. If you have any doubts, just comment below.