Tuesday 8 July 2014

VideoView Example in Android

Hi Friends,

Here i am going to explain you how to run .mp4 video in Android programatically, follow the below code sample 




First of all you need to create a folder with "raw" name under res folder and put your .mp4 video there.




Code:

activity_main

<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/videoView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center" />

</LinearLayout>


MainActivity.java

package in.androiddevelopmentanddiscussion.videoplayerexample;

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 MainActivity extends Activity {  
  
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_main);  
          
        VideoView videoView =(VideoView)findViewById(R.id.videoView1);  
          
                //Creating MediaController  
        MediaController mediaController= new MediaController(this);  
            mediaController.setAnchorView(videoView);          
         
              //specify the location of media file  
            String uriPath = "android.resource://in.androiddevelopmentanddiscussion.videoplayerexample/"+R.raw.memory_card_creation;
           Uri uri=Uri.parse(uriPath);          
                
              //Setting MediaController and URI, then starting the videoView  
           videoView.setMediaController(mediaController);  
           videoView.setVideoURI(uri);          
           videoView.requestFocus();  
           videoView.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.main, menu);  
        return true;  
    }  
  




AndroidManifest

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

    <uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="19" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="in.androiddevelopmentanddiscussion.videoplayerexample.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>


Enjoy Coding.......



No comments:

Post a Comment