Quantcast
Channel: Programming – Xinyustudio
Viewing all articles
Browse latest Browse all 284

Android: How to play RTSP or HLS video streams

$
0
0

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!


Filed under: Android, Programming Tagged: android, “Can’t play this video”, example, hls, how to, play, player, rtsp, stream, streaming, video

Viewing all articles
Browse latest Browse all 284

Trending Articles