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 of all, we need to add LibVLC dependency in the module's build.gradle
file.
app/build.gradle
dependencies {
// Other 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, RTSP stream from 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 set network-caching
to low then stream capture can freeze. So try use various values and adjust this option to your mobile device.
When the activity enters stopped state, media player is stopped too and a video layout is detached from player. Resources are released when the activity is destroyed.
app/src/main/java/com/example/app/MainActivity.java
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();
}
}
app/src/main/java/com/example/app/MainActivity.kt
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
class MainActivity : AppCompatActivity()
{
private var url: String = "rtsp://user:pass@192.168.0.9:554/h264Preview_01_main"
private lateinit var libVlc: LibVLC
private lateinit var mediaPlayer: MediaPlayer
private lateinit var videoLayout: VLCVideoLayout
override fun onCreate(savedInstanceState: Bundle?)
{
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
libVlc = LibVLC(this)
mediaPlayer = MediaPlayer(libVlc)
videoLayout = findViewById(R.id.videoLayout)
}
override fun onStart()
{
super.onStart()
mediaPlayer.attachViews(videoLayout, null, false, false)
val media = Media(libVlc, Uri.parse(url))
media.setHWDecoderEnabled(true, false)
media.addOption(":network-caching=600")
mediaPlayer.media = media
media.release()
mediaPlayer.play()
}
override fun onStop()
{
super.onStop()
mediaPlayer.stop()
mediaPlayer.detachViews()
}
override fun onDestroy()
{
super.onDestroy()
mediaPlayer.release()
libVlc.release()
}
}
The 12 Comments Found
Hi, this tutorial is great. I'm Android Studio beginner, could you please teach me how to write this tutorial in Java? Thank you.
Hi, Matniss
I written a
MainActivity
class in Java and added to the post.Alright, thanks for your reply, thank you so much.
Best library and best explanation I've ever seen Thank you from the bottom of my heart
Hi, me again lol, I have a question, is it possible to add a progress dialog with showing message "Video is buffering... Please wait" when the video streaming is buffering? If possible could u please teach me how to do it?
Hi, Matniss
I created separate post how display a progress dialog when the RTSP stream is buffering in VLC player.
Hi!
Is there any way to change the source of the video in runtime?
Hi, Gaston
I created a new post how dynamically change RTSP URL of IP camera in VLC Player.
Hi, nice tutorial. Could you explain me how can I use the library for playing/recording video? Thank you for you attention
Hi, Davide
I written a new post how play video file using LibVLC library.
Hi, nice tutorial! I have a question, When i generated signed apk the size is 160mb aprox. Is too much, what can i do for to lose weight?
Hi, Juan
LibVLC for Android library uses code that is written in C++. As a result, APK contains the native libraries such as
libvlc.so
(~36-46 MB) for each Application Binary Interface (ABI) such as armeabi-v7a, arm64-v8a, x86, and x86_64. To minimize APK size, you can specify ABIs you want to support in theapp/build.gradle
file:Leave a Comment
Cancel reply