VLC engine on Android application can be embedded using LibVLC library. This tutorial provides example how to play a video file using LibVLC on Android application.
Firstly, add the LibVLC library as dependency in the module's build.gradle
file.
dependencies {
// Other dependencies
// ...
implementation 'org.videolan.android:libvlc-all:3.4.4'
}
To display video, we should add a VLCVideoLayout
in the layout XML file. Also add the button for opening a file browser.
<?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">
<Button
android:id="@+id/openButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="Open" />
<org.videolan.libvlc.util.VLCVideoLayout
android:id="@+id/videoLayout"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
Make sure you have a video file in your mobile device. For example, you can download the following video for testing:
https://docs.evostream.com/sample_content/assets/bun33s.mp4
We attached the click listener to open a file browser when the button is clicked. When the user selected a video file, the playVideo
method is called. It opens a file with the read mode. After that, an instance of the Media
class is created and video begin to play.
When the activity is stopped, the media player is stopped too and the video layout is detached from the player. When the activity is destroyed, resources are released.
package com.example.app;
import android.net.Uri;
import android.os.Bundle;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts.GetContent;
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;
import java.io.FileDescriptor;
import java.io.FileNotFoundException;
public class MainActivity extends AppCompatActivity
{
private LibVLC libVlc;
private MediaPlayer mediaPlayer;
private VLCVideoLayout videoLayout;
ActivityResultLauncher<String> resultLauncher = registerForActivityResult(
new GetContent(),
this::playVideo
);
@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);
findViewById(R.id.openButton).setOnClickListener(
v -> resultLauncher.launch("video/*")
);
}
@Override
protected void onStop()
{
super.onStop();
mediaPlayer.stop();
mediaPlayer.detachViews();
}
@Override
protected void onDestroy()
{
super.onDestroy();
mediaPlayer.release();
libVlc.release();
}
private void playVideo(Uri uri)
{
if (uri == null) {
return;
}
try {
FileDescriptor fd = getContentResolver()
.openFileDescriptor(uri, "r")
.getFileDescriptor();
mediaPlayer.attachViews(videoLayout, null, false, false);
Media media = new Media(libVlc, fd);
media.setHWDecoderEnabled(true, false);
mediaPlayer.setMedia(media);
media.release();
mediaPlayer.play();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
package com.example.app
import android.net.Uri
import android.os.Bundle
import android.widget.Button
import androidx.activity.result.contract.ActivityResultContracts.GetContent
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 lateinit var libVlc: LibVLC
private lateinit var mediaPlayer: MediaPlayer
private lateinit var videoLayout: VLCVideoLayout
private val resultLauncher = registerForActivityResult(GetContent()) { uri: Uri? ->
playVideo(uri)
}
override fun onCreate(savedInstanceState: Bundle?)
{
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
libVlc = LibVLC(this)
mediaPlayer = MediaPlayer(libVlc)
videoLayout = findViewById(R.id.videoLayout)
val button: Button = findViewById(R.id.openButton)
button.setOnClickListener {
resultLauncher.launch("video/*")
}
}
override fun onStop()
{
super.onStop()
mediaPlayer.stop()
mediaPlayer.detachViews()
}
override fun onDestroy()
{
super.onDestroy()
mediaPlayer.release()
libVlc.release()
}
private fun playVideo(uri: Uri?)
{
if (uri === null) {
return
}
val fd = contentResolver.openFileDescriptor(uri, "r")
mediaPlayer.attachViews(videoLayout, null, false, false)
val media = Media(libVlc, fd!!.fileDescriptor)
media.setHWDecoderEnabled(true, false)
media.addOption(":network-caching=600")
mediaPlayer.media = media
media.release()
mediaPlayer.play()
}
}
The 2 Comments Found
Hi, nice tutorial.
It worked perfectly.
Thank you very much
A very clear example - bravo!
Leave a Comment
Cancel reply