RSS

Android: How to play RTSP or HLS video streams

19 Sep

Playing rtsp video streams seems to be a complex task in Android, but actually it is insane easy. Here is how.

  • Create a new project using Android Studio
  • Drag a VideoView control to the UI or add below code in the UI xml file:
<VideoView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/videoView" />

 

image   image

Open the activity java file, add below code:

    VideoView  videoView;
    String  videoRtspUrl=rtsp://wowzaec2demo.streamlock.net/vod/mp4:BigBuckBunny_115k.mov;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        videoView = (VideoView) this.findViewById(R.id.videoView);
        videoView.setVideoPath(videoRtspUrl);
        videoView.requestFocus();
        videoView.start
();
     }

If you build and run the app, you might encounter an error:

“Can’t play this video”

Add the permission in AndroidManifest before <Application> node:

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

Rebuild, yeah! Success! That is just that simple!

Yes, this approach also works for HLS video streams, e.g.

http://qthttp.apple.com.edgesuite.net/1010qwoeiuryfg/sl.m3u8

In short, the built-in VideoPlayer control from Android SDK makes it easy to play RTSP or HLS streams with little efforts!

Advertisements
 
2 Comments

Posted by on September 19, 2015 in Android, Programming

 

Tags: , , , , , , , , , ,

2 responses to “Android: How to play RTSP or HLS video streams

  1. Zetla

    February 2, 2017 at 10:50 am

    Thx you very much for this sample! I was looking for a lib to do this and finally a simple videoView do the job !!

    Do you have any sample to do the reverse thing ? I want to send hls or rtmp video stream from my phone camera to my personnal server (working with nginx)

    Bye!

     

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: