LibVLC for Android is a library that allows to embed VLC engine on Android application. This tutorial provides example how to display RTSP stream from IP camera using LibVLC on Android application.
First, we need to add LibVLC dependency in the module's build.gradle file.
app/build.gradle
dependencies {
implementation 'org.videolan.android:libvlc-all:3.4.4'
}
Request the INTERNET permission in the manifest file because application should have Internet access.
app/src/main/AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.app">
<uses-permission android:name="android.permission.INTERNET" />
<application>
...
</application>
</manifest>
Open the layout XML file and add a VLCVideoLayout that will be used to display RTSP stream from IP camera.
app/src/main/res/layout/activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<org.videolan.libvlc.util.VLCVideoLayout
android:id="@+id/videoLayout"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
When the activity starts, the RTSP stream from the IP camera is captured and displayed using media player. Make sure you have changed RTSP URL of your IP camera. Manufacturers might use different RTSP URLs. We used Reolink E1 Pro camera for testing. The network-caching option can be minimized to reduce the delay of RTSP stream coming from an IP camera. If you're set network-caching to low, then stream capture can freeze. So try to use various values and adjust this option to your mobile device.
When the activity enters a stopped state, the media player is stopped too and a video layout is detached from the player. Resources are released when the activity is destroyed.
app/src/main/java/com/example/app/MainActivity.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package com.example.app;
import android.net.Uri;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import org.videolan.libvlc.LibVLC;
import org.videolan.libvlc.Media;
import org.videolan.libvlc.MediaPlayer;
import org.videolan.libvlc.util.VLCVideoLayout;
public class MainActivity extends AppCompatActivity
{
private static final String url = "rtsp://user:pass@192.168.0.9:554/h264Preview_01_main";
private LibVLC libVlc;
private MediaPlayer mediaPlayer;
private VLCVideoLayout videoLayout;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
libVlc = new LibVLC(this);
mediaPlayer = new MediaPlayer(libVlc);
videoLayout = findViewById(R.id.videoLayout);
}
@Override
protected void onStart()
{
super.onStart();
mediaPlayer.attachViews(videoLayout, null, false, false);
Media media = new Media(libVlc, Uri.parse(url));
media.setHWDecoderEnabled(true, false);
media.addOption(":network-caching=600");
mediaPlayer.setMedia(media);
media.release();
mediaPlayer.play();
}
@Override
protected void onStop()
{
super.onStop();
mediaPlayer.stop();
mediaPlayer.detachViews();
}
@Override
protected void onDestroy()
{
super.onDestroy();
mediaPlayer.release();
libVlc.release();
}
}