LibVLC库允许在 Android 应用程序上显示来自 IP 摄像机的 RTSP 流。可能有这样一种情况,我们有多个 IP 摄像头,并且希望在用户从下拉列表中选择特定摄像头时显示来自特定摄像头的 RTSP 流。
本教程提供了如何在 Android 应用程序的 VLC 播放器中动态更改 IP 摄像机的 RTSP URL 的示例。
首先,在模块build.gradle
文件中添加 LibVLC 库。
dependencies {
// Other dependencies
// ...
implementation 'org.videolan.android:libvlc-all:3.4.4'
}
请求INTERNET
清单文件中的权限,因为应用程序需要访问 Internet。
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>
在布局 XML 文件中,添加Spinner
用于显示 IP 摄像机下拉列表的内容。您还需要添加VLCVideoLayout
以显示来自所选摄像机的 RTSP 流。
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">
<Spinner
android:id="@+id/cameraDropdown"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<org.videolan.libvlc.util.VLCVideoLayout
android:id="@+id/videoLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/cameraDropdown"/>
</RelativeLayout>
我们创建了一个新VideoStreamPlayer
类,负责通过给定的 RTSP URL 显示来自 IP 摄像机的流。
app/src/main/java/com/example/app/VideoStreamPlayer.java
package com.example.app;
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;
public class VideoStreamPlayer
{
private final LibVLC libVlc;
private final MediaPlayer mediaPlayer;
private final VLCVideoLayout videoLayout;
public VideoStreamPlayer(Context context, VLCVideoLayout videoLayout)
{
libVlc = new LibVLC(context);
mediaPlayer = new MediaPlayer(libVlc);
this.videoLayout = videoLayout;
}
public void start(String url)
{
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();
}
public void stop()
{
mediaPlayer.stop();
mediaPlayer.detachViews();
}
public void release()
{
mediaPlayer.release();
libVlc.release();
}
}
在MainActivity
类中,我们定义了两个数组来存储 RTSP 地址和将用于在下拉列表中显示的摄像头名称。不要忘记根据您拥有的 IP 摄像机调整值。我们使用了两台 Reolink E1 Pro 相机进行测试。
我们将一个带有相机名称的数组适配器附加到Spinner
并注册一个回调,当用户从下拉列表中选择一个相机时调用该回调。选择IP摄像机后,先前捕获的流将停止,并且来自选定相机的新流开始显示。
app/src/main/java/com/example/app/MainActivity.java
package com.example.app;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import androidx.appcompat.app.AppCompatActivity;
import org.videolan.libvlc.util.VLCVideoLayout;
public class MainActivity extends AppCompatActivity
{
private static final String[] urlList = {
"rtsp://user:pass@192.168.0.8:554/h264Preview_01_main",
"rtsp://user:pass@192.168.0.9:554/h264Preview_01_main"
};
private static final String[] cameraList = {
"Camera 1",
"Camera 2"
};
private VideoStreamPlayer streamPlayer;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
VLCVideoLayout videoLayout = findViewById(R.id.videoLayout);
streamPlayer = new VideoStreamPlayer(this, videoLayout);
ArrayAdapter<String> adapter = new ArrayAdapter<>(
this,
android.R.layout.simple_spinner_dropdown_item,
cameraList
);
Spinner dropdown = findViewById(R.id.cameraDropdown);
dropdown.setAdapter(adapter);
dropdown.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View v, int pos, long id)
{
streamPlayer.stop();
streamPlayer.start(urlList[pos]);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {}
});
}
@Override
protected void onStop()
{
super.onStop();
streamPlayer.stop();
}
@Override
protected void onDestroy()
{
super.onDestroy();
streamPlayer.release();
}
}
原文链接:https://lindevs.com/dynamically-change-rtsp-url-of-ip-camera-in-vlc-player-on-android
本文来自作者投稿,版权归原作者所有。如需转载,请注明出处:https://www.nxrte.com/jishu/17497.html