Wednesday, 12 March 2014

How to Create VideoView Functionality and Implement in Android Apps


This blog describe how to create VideoView or video playing functionality in android applications with control functionality.

main.xml


<LinearLayout 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"
    android:orientation="vertical" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Static Video View"
        android:textColor="#336633"
        android:textSize="20dp"
        android:textStyle="bold"
        android:layout_marginTop="20dp"
        android:layout_gravity="center" />

// No need to write manually VideoView code in xml editor drag VideiView widget from left side Palette in // graphical view of xml editor



    <VideoView
        android:id="@+id/videoView1"
        android:layout_width="match_parent"
        android:layout_marginTop="20dp"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_height="300dp" />

</LinearLayout>





VideoMediaPlayerActivity.java


package com.rakesh.tiwari.videomediaplayer;

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

public class VideoMediaPlayerActivity extends Activity {
VideoView vView;
MediaController mController;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

//   Here create raw folder in res folder and keep video file and programmatic "android. resource://package name"  + image resource path.

Uri uriPath= Uri.parse("android.resource://com.rakesh.tiwari.videomediaplayer/"+R.raw.jiyare);
vView=(VideoView)findViewById(R.id.videoView1);
mController=new MediaController(this);
vView.setMediaController(mController);
vView.setVideoURI(uriPath);
vView.requestFocus();
vView.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.video_media_player, menu);
return true;
}

}


AndroidManifest.xml

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

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:screenOrientation="portrait"
            android:name="com.rakesh.tiwari.videomediaplayer.VideoMediaPlayerActivity"
            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>

VideoView Android App
VideoView Android App

VideoView Creation
VideoView Creation

Video Playing Android App
Video Playing Android App