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.

23 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. hi friends I find the new example of Play Video File - Android Example
    http://androidexample.com/Play_Video_File_-_Android_Example
    /index.php?view=article_discription&aid=124&aaid=144

    ReplyDelete
  7. i geting error in "Sorry, this Video cannot played."

    ReplyDelete
  8. i am trying to play youtube video it gives error message as Cant't play this video dialoge

    ReplyDelete

  9. We’ve been stumbling around the internet and found your blog along the way.

    We love your work! What a great corner of the internet :)

    beds
    bed
    mattress
    mattresses

    ReplyDelete
  10. It is amazing and wonderful to visit your site.Thanks for sharing this information,this is useful to me...

    Java Training in Pune

    ReplyDelete
  11. Thanks for sharing this information...
    AngularJS Training in Bangalore | AngularJS Course Fees | AngularJS 6 - i Digital Academy - AngularJS Training in Bangalore - Learn AngularJS 6 from Expert Real-time Trainers
    at i Digital Academy with Live Projects and Placement Assistance. Book a Free Demo Today.

    ReplyDelete
  12. Thanks for Sharing This Article.It is very so much valuable content. I hope these Commenting lists will help to my website
    angularjs online training
    best angularjs online training
    top angularjs online training

    ReplyDelete
  13. Your article looks really adorable, here's a site link i dropped for you which you may like.경마

    ReplyDelete
  14. I enjoy reading all of your post. I wanted to write
    a little comment to support you. 온라인카지노

    ReplyDelete


  15. I'm writing on this topic these days,
    블랙잭사이트

    This post is really magnificent.
    블랙잭사이트

    ReplyDelete
  16. agyan nyo nalang ng number.

    sundan nyo to:

    65. Santos, San Dioniso
    50. Brabante- San Dionisio
    60. Contemplacion - BF Homes
    71. Asadon- San dionisio
    14. Biongan- San Isidro
    20. Bion-San Dionisio
    54. Galman- Moonwalk
    17.Hermita- San Dionisio
    kung ano po ung number nyo sa taas
    64. Condino -SAN DIONISIO
    57. Suano- San isidro
    62. Mugar - Marcelo Green
    63. Corpuz - Moonwalk
    66. Yongzon Jr. - Don Bosco
    6. Del Rosario - San Antonio

    ReplyDelete