Thursday, July 26, 2012

Play video in android




Project Name     :  Video

Package Name   :  selva.video

Version              :  1.5 ( Supports 1.5 and above versions)


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" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

    <VideoView
        android:id="@+id/videoView1"
        android:layout_width="fill_parent"
        android:layout_height="174dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:layout_marginLeft="117dp"
        android:text="Play" />

</RelativeLayout>



VideoActivity.class

package selva.video;

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

public class VideoActivity extends Activity
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button play=(Button) findViewById(R.id.button1);
     
       
        final VideoView vv = (VideoView) findViewById(R.id.videoView1);       
        Uri video = Uri.parse("android.resource://" + getPackageName() + "/" 
                + R.raw.video);  







  // here i used video.mp4.. You can add audio file under res/raw/
       // create raw folder under res folder then add video

        vv.setVideoURI(video);
       
        play.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                // TODO Auto-generated method stub
                   vv.start(); 
            }
        });
       
    
    }
}



video.mp4 






















OUTPUT : 























click play button. You will hear the audio but  video is not visible. 

You will check it out in real device.



 

No comments:

Post a Comment