Monday, September 2, 2013

How to play saved video in android?


  • Create android project named "savedVideoView".
  • Now create 'raw' folder in 'res' directory.Note: Do not change the folder name.
  • Add Video file (mp4) which you want to play in application.
  • Now just add following code in activity_main.xml file 

activity_main.xml



<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center"
    android:minWidth="854dip"
    android:minHeight="480dip"
    >
<VideoView android:id="@+id/welcome"
android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_gravity="center"
    android:fitsSystemWindows="true"
    android:minWidth="854dip"
    android:minHeight="480dip"
    >
</VideoView>
</LinearLayout>



  • Add following code in MainActivity.java file.

MainActivity.java



public class MainActivity extends Activity 
{

VideoView vv;

@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
// it is used to set full screen video
    requestWindowFeature(Window.FEATURE_NO_TITLE);  
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,   
                                WindowManager.LayoutParams.FLAG_FULLSCREEN);  

    setContentView(R.layout.play_intro);
    vv = (VideoView) findViewById(R.id.welcome);

       
    MediaController mc = new MediaController(this); 
    vv.setMediaController(mc); 
    mc.hide();
          // Here resource is 'video2' video file which is stored in 'raw' directory 
    vv.setVideoURI(Uri.parse("android.resource://com.tumbi/"+R.raw.video2));
   
    vv.start();
   
    vv.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
   
    @Override
    public void onCompletion(MediaPlayer mp) 
    {
    onPause();
                         // After completion of video it goes to CompleteVideoActivity automatically
    Intent i=new Intent(MainActivity.this,CompleteVideoActivity.class);
    startActivity(i);
    android.os.Process.killProcess(android.os.Process.myPid());
    }
    });
   
}

public void onPause ()
{
super.onPause();
vv.stopPlayback();
}
}

  • Done ! Now run the Project and get the O/P.

how to play youtube video in videoview android ?


No comments:

Post a Comment