27

Today for one of my app (Android 2.1), I wanted to stream a video from an URL.

As far as I explored Android SDK it's quite good and I loved almost every piece of it. But now that it comes to video stream I am kind of lost.

For any information you need about Android SDK you have thousands of blogs telling you how to do it. When it comes to video streaming, it's different. Informations is that abundant.

Everyone did it it's way tricking here and there.

Is there any well-know procedure that allows one to stream a video?

Did google think of making it easier for its developers?

Spredzy
  • 4,982
  • 13
  • 53
  • 69

1 Answers1

36

If you want to just have the OS play a video using the default player you would use an intent like this:

String videoUrl = "insert url to video here";
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(videoUrl));
startActivity(i);

However if you want to create a view yourself and stream video to it, one approach is to create a videoview in your layout and use the mediaplayer to stream video to it. Here's the videoview in xml:

<VideoView android:id="@+id/your_video_view"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_gravity="center"
/>

Then in onCreate in your activity you find the view and start the media player.

    VideoView videoView = (VideoView)findViewById(R.id.your_video_view);
    MediaController mc = new MediaController(this);
    videoView.setMediaController(mc);

    String str = "the url to your video";
    Uri uri = Uri.parse(str);

    videoView.setVideoURI(uri);

    videoView.requestFocus();
    videoView.start();

Check out the videoview listeners for being notified when the video is done playing or an error occurs (VideoView.setOnCompletionListener, VideoView.setOnErrorListener, etc).

Ryan Reeves
  • 10,209
  • 3
  • 42
  • 26
  • 1
    Thank you for you answer. I am actually interested in the first option. However when I deploy it on the phone, android asks me Internet Or MediaPlayer do you know how to get rid of it and go mediap player by default ? – Spredzy Nov 17 '10 at 10:20
  • Sorry I don't know a generic way to force Android to go directly to the phone's video player. I do know that if videos are set to progressive download, android will pop up that dialog asking you if you'd like to use the browser or video player. Videos set to stream RTSP will just open in the video player. – Ryan Reeves Nov 17 '10 at 17:13
  • Hey, @rreeverb, is this method fast enough to play video on slow devices? (e.g. it uses default player that uses DSP..?) – kagali-san Dec 16 '10 at 23:48
  • It works with a video url, But when I give the live video url, it is not playing, Is there any way to stream live video from web to android. Currently playing video should be recordable. – ABI Sep 21 '15 at 10:40