1.修改项目build.gradle,增加maven库地址
allprojects {
repositories {
google()
jcenter()
maven { url "https://dl.bintray.com/videolan/Android" }
}
}
2.修改模块build.gradle,增加依赖
// VLC 最新版本 implementation 'org.videolan.android:libvlc-all:3.2.1'
3.布局文件中加入VLCVideoLayout
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
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/video_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
4.增加VLCManager
import android.content.Context;
import android.net.Uri;
import org.videolan.libvlc.LibVLC;
import org.videolan.libvlc.Media;
import org.videolan.libvlc.MediaPlayer;
import org.videolan.libvlc.util.VLCVideoLayout;
import java.util.ArrayList;
public class VLCManager {
private static final boolean USE_TEXTURE_VIEW = false;
private static final boolean ENABLE_SUBTITLES = true;
private VLCVideoLayout mVideoLayout;
private LibVLC mLibVLC;
private MediaPlayer mMediaPlayer;
public VLCManager(Context context, VLCVideoLayout videoLayout) {
ArrayList<String> options= new ArrayList<>();
options.add("-vvv");
options.add("--h264-fps=20");
options.add("--hevc-fps=20");
mLibVLC = new LibVLC(context, options);
mMediaPlayer = new MediaPlayer(mLibVLC);
mVideoLayout = videoLayout;
}
public void play(String url) {
mMediaPlayer.attachViews(mVideoLayout, null, ENABLE_SUBTITLES, USE_TEXTURE_VIEW);
Media media = new Media(mLibVLC, Uri.parse(url));
media.addOption(":network-caching=300");
mMediaPlayer.setMedia(media);
media.release();
mMediaPlayer.play();
}
public void onStop() {
if (mMediaPlayer != null) {
mMediaPlayer.stop();
mMediaPlayer.detachViews();
}
}
public void onDestroy() {
if (mMediaPlayer != null) {
mMediaPlayer.release();
}
if (mLibVLC != null) {
mLibVLC.release();
}
}
}
5.修改MainActivity
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import org.videolan.libvlc.util.VLCVideoLayout;
public class MainActivity extends AppCompatActivity {
private VLCManager mVLCManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mVLCManager = new VLCManager(this, (VLCVideoLayout) findViewById(R.id.video_layout));
mVLCManager.play("rtsp://192.168.1.1:554/user=admin&password=&channel=1&stream=0.sdp?real_stream");
}
@Override
protected void onStop() {
super.onStop();
if (mVLCManager != null) {
mVLCManager.onStop();
}
}
@Override
protected void onDestroy() {
super.onDestroy();
if (mVLCManager != null) {
mVLCManager.onDestroy();
}
}
}
参考链接: