How to play video file using VideoView in Android

VideoView : Displays a video file. The VideoView class can load images from various sources (such as resources or content providers), takes care of computing its measurement from the video so that it can be used in any layout manager, and provides various display options such as scaling and tinting.

MediaController: A view containing controls for a MediaPlayer. Typically contains the buttons like “Play/Pause”, “Rewind”, “Fast Forward” and a progress slider. It takes care of synchronizing the controls with the state of the MediaPlayer.

The way to use this class is to instantiate it programatically. The MediaController will create a default set of controls and put them in a window floating above your application. Specifically, the controls will float above the view specified with setAnchorView(). The window will disappear if left idle for three seconds and reappear when the user touches the anchor view.

Functions like show() and hide() have no effect when MediaController is created in an xml layout. MediaController will hide and show the buttons according to these rules.

  • The “previous” and “next” buttons are hidden until setPrevNextListeners() has been called
  • The “previous” and “next” buttons are visible but disabled if setPrevNextListeners() was called with null listeners
  • The “rewind” and “fastforward” buttons are shown unless requested otherwise by using the MediaController(Context, boolean) constructor with the boolean set to false.

Now we Start our project VideoViewDemo  in API DEMO is 2.2 and Level versions 8 so first of all.

  • Import appropriate packages.
  • VideoView use class of Displays a video file.
  • MdediaController use class of  view containing controls for a MediaPlayer
  • To force focus to a specific view, call requestFocus() .
  • The start() method should be called to actually execute the new Thread.

Open res/layout/main.xml

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

<VideoView

android:id=”@+id/ViewVideo”
android:layout_width=”320px”
android:layout_height=”240px”/>

</LinearLayout>

VideoViewDemo/src/com/VideoViewDemoActivity/VideoViewDemoActivity.java

Code:
 package com.theandroid.in;import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.widget.MediaController;
import android.widget.VideoView;

public class VideoViewDemoActivity extends Activity {

/** Called when the activity is first created. */

private VideoView videoView;

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.main);

videoView = (VideoView)findViewById(R.id.ViewVideo);
videoView.setVideoURI(Uri.parse(“android.resource://” + getPackageName() +”/”+R.raw.video));
videoView.setMediaController(new MediaController(this));

videoView.requestFocus();
videoView.start();

}
}

Now run this Applicaton.

   

I hope this application may be useful.

Previous articles

State Persistence Demo using SharedPreferences

Please write review

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>