24 March 2015

Simple VideoView tutorial in Android

What we will do ?
-> We will make an app which has a VideoView and Buttons to play and stop the video.

What we will need ?
  • VideoView and Buttons in activity_main.xml
  • Code Additions in MainActivity.java
Step 1 : Create a New Application Project. Go to File->New->Android Application Project.

Step 2 : Give a name (VideoDemo) to your Application and a suitable package name (mia). Then click on Next continuously, then click Finish.

Step 3 : Make a new folder named raw in res. Right-click on res->New->Folder. Name it raw. Now copy the video file (video.mp4) you want to use for VideoView in raw folder.

 Step 4 : Drag VideoView and 2 Buttons in activity_main.xml OR else copy the code below in it.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    <VideoView
        android:id="@+id/videoview"
        android:layout_width="fill_parent"
        android:layout_height="318dp" />
    <LinearLayout
        android:layout_width="match_parent"
        android:gravity="center"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >
        <Button
            android:id="@+id/playvideoplayer"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:text="PLAY" />
        <Button
            android:id="@+id/stopvideoplayer"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:text="STOP" />
    </LinearLayout>
</LinearLayout>

Step 5 : Open MainActivity.java from src->com.mia.videodemo. Copy the code in it.

MainActivity.java
package com.mia.videodemo;

import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.*;

public class MainActivity extends Activity{

    VideoView mVideoView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        Button buttonPlayVideo = (Button)findViewById(R.id.playvideoplayer);
        Button buttonStopVideo = (Button)findViewById(R.id.stopvideoplayer);
       mVideoView = (VideoView)findViewById(R.id.videoview);
               
        String uriPath = "android.resource://com.mia.videodemo/"+R.raw.video;
        Uri uri = Uri.parse(uriPath);
        mVideoView.setVideoURI(uri);
        mVideoView.requestFocus();

        buttonPlayVideo.setOnClickListener(new Button.OnClickListener(){
            @Override
            public void onClick(View v) {
                    mVideoView.start();
            }});
        buttonStopVideo.setOnClickListener(new Button.OnClickListener(){
            @Override
            public void onClick(View v) {
                    mVideoView.stopPlayback();              
            }}); 
     }
}

Step 6 : Run the Project.

Explanation of the Code :

Explanation from activity_main.xml :

Explanation from MainActivity.java :
  • We connect the variables of Play Button (buttonPlayVideo), Stop Button (buttonStopVideo) and VideoView (mVideoView) with elements in layout.
Button buttonPlayVideo = (Button)findViewById(R.id.playvideoplayer);
Button buttonStopVideo = (Button)findViewById(R.id.stopvideoplayer);
mVideoView = (VideoView)findViewById(R.id.videoview);
  •  Store the path of the video in uriPath variable. Convert it into Uri type (It is converted to url)
String uriPath = "android.resource://com.mia.videodemo/"+R.raw.video;
Uri uri = Uri.parse(uriPath);
mVideoView.setVideoURI(uri);
mVideoView.requestFocus();
  • buttonPlayVideo uses start() command for starting video on clicking the button in onClick().
buttonPlayVideo.setOnClickListener(new Button.OnClickListener(){
@Override
   public void onClick(View v) {
       mVideoView.start();
            }});
buttonStopVideo.setOnClickListener(new Button.OnClickListener(){
     @Override
     public void onClick(View v) {
       mVideoView.stopPlayback();
          }});

Stay Tuned with Made In Android

Previous Page Next Page Home
Top