Thursday, 21 June 2012

Play Online Video in Android Device



Put Below Code into your Application.

MainActivity.java File:-

public class MainActivity extends Activity {
public static String url = "rtsp://v3.cache8.c.youtube.com/CiILENy73wIaGQmXovF6e-Rf-BMYDSANFEgGUgZ2aWRlb3MM/0/0/0/video.3gp";
private VideoView videoView = null;
private ProgressBar prog = null;
private Context ctx = null;
private MediaController mediaController = null;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().setFormat(PixelFormat.TRANSLUCENT);
setContentView(R.layout.main);
ctx = this;
prog = (ProgressBar) findViewById(R.id.prog);
videoView = (VideoView) findViewById(R.id.video);
Uri video = Uri.parse(url);
mediaController = new MediaController(this);
  mediaController.setAnchorView(videoView);
videoView.setMediaController(mediaController);
videoView.setVideoURI(video);

videoView.setOnErrorListener(new OnErrorListener() {

@Override
public boolean onError(MediaPlayer mp, int what, int extra) {
// TODO Auto-generated method stub
Toast.makeText(ctx, "Error occured", 500).show();
return false;
}
});

videoView.setOnPreparedListener(new OnPreparedListener() {

public void onPrepared(MediaPlayer arg0) {
prog.setVisibility(View.GONE);
videoView.start();
}
});
}

@Override
protected void onDestroy() {
try {
videoView.stopPlayback();
} catch (Exception e) {
//
}
super.onDestroy();
}
}


Main.xml File:-

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

    <FrameLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

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

        <ProgressBar
            android:id="@+id/prog"
            android:layout_width="70dp"
            android:layout_height="70dp"
            android:layout_gravity="center" />
    </FrameLayout>

</LinearLayout>


And Give Below Permission into your Manifest.xml file.

<uses-permission android:name="android.permission.INTERNET"/>

Enjoy:--)

Don’t forget to provide feedback or follow this blog, if you find this blog is useful.

8 comments:

  1. Hi dipak,
    nice tutorial, but when we rotate the device from potrait to landscape the video is playing from starting onwards.. when in landscape the videoview is not covering complete screen...
    Please help me in this regards.
    thank u

    ReplyDelete
  2. not working in setonerror() showing error

    ReplyDelete
  3. i have tried this code but showing video coudn't not played..

    ReplyDelete
    Replies
    1. this code when you intall it on emultor it will not work but try it on adevice it works ok

      Delete
  4. nice friend,keep it up....

    ReplyDelete
  5. How can we get the rtsp link can you please help i'm not getting correct solution
    Contact Me:kirankumar667.2015@gmail.com



    Thanks for your code.

    ReplyDelete
  6. Thanks for sharing such a nice example...
    You can also check for important android interview questions @ http://skillgun.com

    ReplyDelete