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
 
5 Comments

Posted by on September 19, 2015 in Android, Programming

 

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

5 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!

     
  2. lunot

    March 30, 2017 at 8:51 am

    i tryed this code and its just running hls stream 😦

     
  3. Divya

    October 19, 2017 at 3:16 pm

    Hi
    I tried this code but am getting “Can’t play this video”
    Here is the logcat logs
    10-19 12:43:45.965 5158-5158/? I/art: Not late-enabling -Xcheck:jni (already on)
    10-19 12:43:45.965 5158-5158/? W/art: Unexpected CPU variant for X86 using defaults: x86
    10-19 12:43:46.261 5158-5158/com.example.divyadharan.myapplication W/System: ClassLoader referenced unknown path: /data/app/com.example.divyadharan.myapplication-2/lib/x86
    10-19 12:43:46.279 5158-5158/com.example.divyadharan.myapplication I/InstantRun: starting instant run server: is main process
    10-19 12:43:46.353 5158-5158/com.example.divyadharan.myapplication W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
    10-19 12:43:46.670 5158-5196/com.example.divyadharan.myapplication I/OpenGLRenderer: Initialized EGL, version 1.4
    10-19 12:43:46.670 5158-5196/com.example.divyadharan.myapplication D/OpenGLRenderer: Swap behavior 1
    10-19 12:43:46.670 5158-5196/com.example.divyadharan.myapplication W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without…
    10-19 12:43:46.670 5158-5196/com.example.divyadharan.myapplication D/OpenGLRenderer: Swap behavior 0
    10-19 12:43:46.683 5158-5196/com.example.divyadharan.myapplication D/EGL_emulation: eglCreateContext: 0x9e05d160: maj 2 min 0 rcv 2
    10-19 12:43:46.691 5158-5196/com.example.divyadharan.myapplication D/EGL_emulation: eglMakeCurrent: 0x9e05d160: ver 2 0 (tinfo 0x9e053a40)
    10-19 12:43:46.811 5158-5158/com.example.divyadharan.myapplication W/MediaPlayer: Couldn’t open rtsp://wowzaec2demo.streamlock.net/vod/mp4:BigBuckBunny_115k.mov: java.io.FileNotFoundException: No content provider: rtsp://wowzaec2demo.streamlock.net/vod/mp4:BigBuckBunny_115k.mov
    10-19 12:43:46.812 5158-5158/com.example.divyadharan.myapplication E/MediaPlayer: Unable to create media player
    10-19 12:43:46.813 5158-5158/com.example.divyadharan.myapplication W/VideoView: Unable to open content: rtsp://wowzaec2demo.streamlock.net/vod/mp4:BigBuckBunny_115k.mov
    java.io.IOException: setDataSource failed.: status=0x80000000
    at android.media.MediaPlayer.nativeSetDataSource(Native Method)
    at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1078)
    at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1067)
    at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1009)
    at android.widget.VideoView.openVideo(VideoView.java:346)
    at android.widget.VideoView.-wrap0(VideoView.java)
    at android.widget.VideoView$7.surfaceCreated(VideoView.java:622)
    at android.view.SurfaceView.updateWindow(SurfaceView.java:618)
    at android.view.SurfaceView$3.onPreDraw(SurfaceView.java:161)
    at android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:944)
    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2205)
    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1254)
    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6337)
    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:874)
    at android.view.Choreographer.doCallbacks(Choreographer.java:686)
    at android.view.Choreographer.doFrame(Choreographer.java:621)
    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:860)
    at android.os.Handler.handleCallback(Handler.java:751)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6119)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
    10-19 12:43:46.813 5158-5158/com.example.divyadharan.myapplication D/VideoView: Error: 1,0
    10-19 12:43:46.859 5158-5158/com.example.divyadharan.myapplication W/art: Before Android 4.1, method int android.support.v7.widget.ListViewCompat.lookForSelectablePosition(int, boolean) would have incorrectly overridden the package-private method in android.widget.ListView
    10-19 12:43:46.900 5158-5196/com.example.divyadharan.myapplication D/EGL_emulation: eglMakeCurrent: 0x9e05d160: ver 2 0 (tinfo 0x9e053a40)
    10-19 12:43:47.146 5158-5196/com.example.divyadharan.myapplication D/EGL_emulation: eglMakeCurrent: 0x9e05d160: ver 2 0 (tinfo 0x9e053a40)
    10-19 12:43:47.242 5158-5196/com.example.divyadharan.myapplication D/EGL_emulation: eglMakeCurrent: 0x9e05d160: ver 2 0 (tinfo 0x9e053a40)
    10-19 12:43:47.271 5158-5196/com.example.divyadharan.myapplication D/EGL_emulation: eglMakeCurrent: 0x9e05d160: ver 2 0 (tinfo 0x9e053a40)
    10-19 12:44:08.363 5158-5196/com.example.divyadharan.myapplication D/EGL_emulation: eglMakeCurrent: 0x9e05d160: ver 2 0 (tinfo 0x9e053a40)

     
  4. Vinod Yadav

    November 15, 2017 at 2:12 pm

    Thank you very much for giving good example of rtsp video with android,
    But when i use rtsp with authentication then it gives a message “Can’t play this video”.
    how can i deal with this problem.
    thanks in advance

     

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: