In this post you’ll learn how to build a video player android app using VideoView .By creating this app you’ll learn some basic concepts of VideoView ,MediaController.

The MediaController consists of components such as play video , pause video , forward and rewind etc.

Lets get started in building an app with video player.

First setup the project with an empty activity and give it any preferred name.
Copy this lines of code in activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<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"
    tools:context="com.example.studio.myvideoapp.MainActivity">

    <VideoView
        android:id="@+id/videoView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="16dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
  • This xml file basically consists of videoView which is used to stream the video.

Adding video path to resource file

Create a raw folder under resource file and insert the video file which you want to play video in app. If you don’t know how to create a raw folder in android studio then you could follow the steps to create raw in our other post and learn more in detail about raw in android studio.

Now copy these lines of code in MainActivity.java

package com.example.studio.myvideoapp;

import android.support.v7.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);
        //Basic of video
        //1. get video view
        VideoView videoView = (VideoView) findViewById(R.id.videoView);

        //2. point to video resource
        videoView.setVideoPath("android.resource://" + getPackageName() + "/" + R.raw.introvideo);

        //add media Control
        MediaController mediaController = new MediaController(this);

        mediaController.setAnchorView(videoView);

        videoView.setMediaController(mediaController);

        //run it
        videoView.start();

    }
}

Lets see what exactly happens in the MainActivity.java

First we get the videoView by findViewById() method.
Then we point that videoView to a videoPath bu=y using setVideoPath() method . We get the path from our raw folder.
Then we add a MediaController by instantiating the MediaController and set the AnchorView and lastly setMediaController on videoView.

How to build a video player android app in minutes. 1

Now we could start playing the video by videoView.Start() method.
NOTE: Now this video will play as soon as the app opens. That’s because we used start () method in onCreate().
The app would look something like this,

LEAVE A REPLY

Please enter your comment!
Please enter your name here