MrBool
You must be logged in to give feedback. Click here to login
[Close]

You must be logged to download.

Click here to login

[Close]

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

[Close]

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

How to play video formats in Android using VideoView

In this tutorial we will learn how to play most of the video formats in android by using VideoView. We will use absolute file path for the playback of the video with full screen support.

[close]

You didn't like the quality of this content?

Would you like to comment what you didn't like?

In android, .mp4 extension is the most common video format with is compatible for the default playback. We will also learn about the SurfaceView which holds the video playback. In this tutorial MediaController will also come under consideration.

Objective:

  • Implemention of VideoView for the playback of Videos.
  • Usage of MediaController for the playback controls.
  • SurfaceView which will hold the playback of Videos.

Classes that are used in the project:

  • VideoView
  • MediaController
  • SurfaceView
  • DisplayMetrics

Introduction to Classes used:

VideoView: This class is basically used to for the display of Video File. VideoView can also be used to load images for the various sources, having information about their measurements computation from the video so that images can be used in any kind of layout manager. Here are below important methods of this class:

  • void setVideoPath(String pth): This method will set the absolute path of the video file which is going to be play.
  • void setMediaController(MediaController cntlor): This method is used to set the controller for the controls of playback .
  • void start(): This method is used to start the playback of video file.
  • void pause(): This method is used to pause the current playback.
  • boolean canPause(): This method will tell whether video view is able to pause.
  • boolean canSeekForward(): This method will return true value if video view is able to seek forward.
  • int getDuration(): This method is used to get the total duration of video view.
  • boolean isPlaying(): This method will return true value if the current video view is in play state.
  • void resume(): This method is used to play the resumed file.

MediaController: MediaController class is used to provide the controls for the video playback. It is used with the VideoView most of. MediaController contains the PAUSE, PLAY, FORWARD, REWIND, SEEKBAR which will sync with state of MediaPlayer.

Here are the below methods of this class which are mostly used:

  • void hide(): This method is use to hide the controls from the screen.
  • Boolean onTouchEvent(MotionEvent evnt): This method is used to handle the touch screen events.
  • void setEnabled(boolean value): This method is used to set the Enable state of this view.
  • void show(int timeOut): This method is used to set the time to show the controller on the screen of current activity.
  • void show(): This method is used to show the controller on the screeen of current activity.

SurfaceView: SurfaceView class is used to provide the surface to the VideoView so that playback of video file remains smooth and lag free.

Here are below the methods of SurfaceView class which is commoly used:

  • void draw(Canvas canvas): It will manually render the view to the given Canvas.
  • SurfaceHolder getHolder(): This method will return the SurfaceHolder which will provide the access and control over the SurfaceView’s underlined surface.
  • void setVisibility(int visiblity): This method will decide whether to show this view or not.
  • void dispatchDraw(Canvas canvas): This method wil be called by draw to draw the child views.
  • void onMeasure(int widthmes , int heightmes): This method will measure the view and its holding content to determine the measured height and width.

Display Metrics class: This class is used to get and provide the dimensions to the views as per need of the application. Here are below the methods of this class which are commonly used .

  • int heightPixels() : This method is used to get the height of current displaying window on device in pixels.
  • int widthPixels(): This method is used to get the width of current displaying window on device in pixels.

Steps to be used to complete this project:

Create a new project in eclipse and name it “VideoPlayer” and package name should be “com.jmd.videoplayer.

Project name should be VideoView as shown above

Figure 1: Project name should be VideoView as shown above.

Package name should be com.jmd.videoplayer as shown above.

Figure 2: Package name should be com.jmd.videoplayer as shown above.

Listing 1: Paste the following code into the main.xml.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <FrameLayout
        android:id="@+id/video_frame"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

        <VideoView
            android:id="@+id/video_player_view"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" />
    </FrameLayout>

</RelativeLayout>

Listing 2: Paste the following code into the VideoPlayerActivity.class.

package com.jmd.videoplayer;

import java.util.ResourceBundle.Control;

import android.app.Activity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.SurfaceView;
import android.widget.MediaController;
import android.widget.VideoView;

public class VideoPlayerActivity extends Activity {
	VideoView video_player_view;
	DisplayMetrics dm;
	SurfaceView sur_View;
	MediaController media_Controller;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
	}

	public void getInit() {
		video_player_view = (VideoView) findViewById(R.id.video_player_view);
		media_Controller = new MediaController(this);
		dm = new DisplayMetrics();
		this.getWindowManager().getDefaultDisplay().getMetrics(dm);
		int height = dm.heightPixels;
		int width = dm.widthPixels;
		video_player_view.setMinimumWidth(width);
		video_player_view.setMinimumHeight(height);
		video_player_view.setMediaController(media_Controller);
		video_player_view.setVideoPath("");
		video_player_view.start();
	}
}

Listing 3: Add the following code in AndroidManifest.xml file inside the <activity> tag.

android:screenOrientation = “landscape”

Run your project, here is the output will come.

When Player is on playback.

Figure 3: When Player is on playback.

When player is in pause state.

Figure 4: When player is in pause state.

Explanation of above code:

FrameLayout with VideoView: This code is used to Add a frame layout which holds the video view so that additional view can also be placed over the video.

Listing 4: FrameLayout with VideoView

<FrameLayout
        android:id="@+id/video_frame"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

        <VideoView
            android:id="@+id/video_player_view"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" />
    </FrameLayout>

The public void getInit() method: This method is used to initialize the views and to set videoview on play state.

Listing 5: getInit () method

public void getInit() {
		video_player_view = (VideoView)  findViewById(R.id.video_player_view);
		media_Controller = new MediaController(this);
		dm = new DisplayMetrics();
		this.getWindowManager().getDefaultDisplay().getMetrics(dm);
		int height = dm.heightPixels;
		int width = dm.widthPixels;
		video_player_view.setMinimumWidth(width);
		video_player_view.setMinimumHeight(height);
		video_player_view.setMediaController(media_Controller);
		video_player_view.setVideoPath("/sdcard/video.mp4");
		video_player_view.start();
	}

The following code will get the dimensions of current displaying window of phone.

Listing 6: Dimensions

this.getWindowManager().getDefaultDisplay().getMetrics(dm);

The following code will get the height and width of displaying window on phone screen.

Listing 7: Height and Width

int height = dm.heightPixels;
int width = dm.widthPixels;

The following code is used to set the Minimum height and width of the videoview used.

Listing 8: Minimum height and width

video_player_view.setMinimumWidth(width);
video_player_view.setMinimumHeight(height);

The following code is used to set the MediaController to the videoview to get controls on the screen like PLAY/PAUSE, FORWARD, REWIND, SEEKBAR etc.

Listing 9: Set MediaController to videoview

video_player_view.setMediaController(media_Controller);

The following code is used to set the path of the video file to be played in String format.

We have used  ”/sdcard/video.mp4”.

Note: Be assure that you have video.mp4 in the root directory of sdcard

Listing 10: the path of the video file

video_player_view.setVideoPath("/sdcard/video.mp4");

The following code will start the playback of video.mp4 file.

Listing 11: start the playback

video_player_view.start();

The following code is used to set the activity orientation to landscape means activity will always open in landscape mode in phone.

Listing 12: set the activity orientation

android: screenOrientation=”landscape”;

What is covered ?

  • How to use VideoView for the playback of video formats in android with the help of SurfaceView and MediaController class.
  • Introduction to the MediaController class and its methods that are commonly used.
  • How to use DisplayMetrics class to get height and width of the current viewing window of device in pixels.
  • How to use SurfaceView to hold the video playback for smoothness.


Have good knowledge on Java, HTML, CSS and Android platform and is pursuing Masters in Computer Applications.

What did you think of this post?

Did you like the post?

Help us to keep publishing good contents like this.

SUPPORT US

funded

remaining

[Close]
To have full access to this post (or download the associated files) you must have MrBool Credits.

  See the prices for this post in Mr.Bool Credits System below:

Individually � in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits � in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!


> More info about MrBool Credits
[Close]
You must be logged to download.

Click here to login