Playing video in an Android application


          Android's MediaPlayer class works with video files the same way it does with plain audio/music files. But there is a small difference. We need to create a surface to play the video. We can use start() or stop() methods to control the play.
We have already seen the use of MediaPlayer in audio example here. So to play the video we are going with different option, the VideoView class.
This is the simplest way of playing a video file. I have placed a sample 3gp video file inside /res/raw directory. We need to <VideoView> in the layout xml file and use it in the activity class.
VideoView class provides bunch of methods that can be used in video play.

setVideoPath() method is used to specify the path of the video file to be played. This can either be a local path or a remote URL.
setVideoURI() method is same as above method except the parameter. This taker URI as parameter.
start() method is used to start the video playback.
stop() method is used to stop the video playback.
pause() method pauses the video.
To check whether the video is playing or not isPlaying() method can be used. This will return a boolean value.
getCurrentPosition() method will return the current position of the video playback.

Sample application:
In this sample application we have a video file inside /res/raw directory and we are going to play this using VideoView class.

Project structure:




To use the full screen view we have used FullScreen theme in this application. This should be specified in the manifest xml file.


android:theme="@android:style/Theme.NoTitleBar.Fullscreen"

To get the URI of the video file placed inside the /res/raw directory below statement is used;


android.resource://" + getPackageName() + "/" + R.raw.video


AndroidManifest.xml file:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.playvideo"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="16" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.NoTitleBar.Fullscreen" >
        <activity
            android:name="com.example.playvideo.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>


Layout xml file:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <VideoView
        android:id="@+id/video"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center" />

</RelativeLayout>


Activity class:

package com.example.playvideo;

import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.widget.VideoView;

public class MainActivity extends Activity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_main);
   VideoView video = (VideoView) findViewById(R.id.video);
   // Load and start the movie
   Uri video1 = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.video);
   video.setVideoURI(video1);
   video.start();
 }

 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
 // Inflate the menu; this adds items to the action bar if it is present.
   getMenuInflater().inflate(R.menu.activity_main, menu);
   return true;
 }
}


Output screenshot:




Source code of this application:
PlayVideo.zip

 



Reactions:

1 comment :

  1. Bro Pls Tell me what is the fonction of this methode :/ @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
    }

    ReplyDelete