如何使用 Vlc.DotNet 在 Windows Forms c# 中流式传输 RTSP 摄像头源
How to stream RTSP camera feed in Windows Forms c# using Vlc.DotNet
我已按照 link 的说明添加 Vlc.DotNet
库(.Core
、.Core.Interops
、.Forms
和 .Wpf
) 到我的项目的解决方案。
我还添加了 VideoLAN.LibVLC.Windows
库的 3.0.0 版本。
我在表单中添加了一个 vlcControl,结果是这样 Designer.cs
:
//
// vlcControl1
//
this.vlcControl1.BackColor = System.Drawing.Color.Black;
this.vlcControl1.Location = new System.Drawing.Point(384, 357);
this.vlcControl1.Name = "vlcControl1";
this.vlcControl1.Size = new System.Drawing.Size(75, 23);
this.vlcControl1.Spu = -1;
this.vlcControl1.TabIndex = 6;
this.vlcControl1.Text = "vlcControl1";
this.vlcControl1.VlcLibDirectory = ((System.IO.DirectoryInfo)(resources.GetObject("vlcControl1.VlcLibDirectory")));
this.vlcControl1.VlcMediaplayerOptions = null;
我在属性中添加了一个虚拟的 VlcLibDirectory,以便稍后更改它。
我的 vlcLib x86 版本的路径是:E:\testLouka\dansMaCamera2.0\dansMaCamera2.0\libvlc\win-x86
我尝试使用以下代码从 RTSP 流中获取视频源 url:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.vlcControl1 = new VlcControl()
{
Name = "vlc1",
Location = new Point(0, 0),
Dock = DockStyle.Fill,
VlcLibDirectory = new DirectoryInfo(Path.Combine("E:\testLouka\dansMaCamera2.0\dansMaCamera2.0", "libvlc", IntPtr.Size == 4 ? "win-x86" : "win-x64")),
Spu = -1,
VlcMediaplayerOptions = null,
Enabled = true
};
string[] options = { ":network-caching=500" };
vlcControl1.Play(new Uri(m_stream.URL), options);
}
}
m_stream.URL
变量返回一个 RTSP link 看起来像“rtsp://admin:admin123@190.19.191.19/Stream0
”
我的表单出现了,但是我的 vlcController 没有显示任何东西...
我查看了 https://github.com/ZeBobo5/Vlc.DotNet 的 wiki,但我卡住了...
我做错了什么?
您只需在表单中添加一个 vlcControl
并在其 VlcLibDirectoryNeeded
事件中添加一些代码。
/// <summary>
/// Looks for the vlc directory on the opening of the app
/// Opens a dialog if the libvlc folder is not found for the user to pick the good one
/// Folder for 32bits should be "libvlc\win-x86\" and "libvlc\win-x64\" for 64 bits
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void myVlcControl_VlcLibDirectoryNeeded(object sender, VlcLibDirectoryNeededEventArgs e)
{
var currentAssembly = Assembly.GetEntryAssembly();
var currentDirectory = new FileInfo(currentAssembly.Location).DirectoryName;
if (currentDirectory == null)
return;
if (IntPtr.Size == 4)
e.VlcLibDirectory = new DirectoryInfo(Path.GetFullPath(@".\libvlc\win-x86\"));
else
e.VlcLibDirectory = new DirectoryInfo(Path.GetFullPath(@".\libvlc\win-x64\"));
if (!e.VlcLibDirectory.Exists)
{
var folderBrowserDialog = new System.Windows.Forms.FolderBrowserDialog();
folderBrowserDialog.Description = "Select Vlc libraries folder.";
folderBrowserDialog.RootFolder = Environment.SpecialFolder.Desktop;
folderBrowserDialog.ShowNewFolderButton = true;
if (folderBrowserDialog.ShowDialog() == DialogResult.OK)
{
e.VlcLibDirectory = new DirectoryInfo(folderBrowserDialog.SelectedPath);
}
}
}
然后,您可以在表单中添加某种播放按钮并播放任何想要的 rtsp 流!
private void btnPlay_Click(object sender, EventArgs e)
{
myVlcControl.Play(MyStream.URL);//can test with rtsp://184.72.239.149/vod/mp4:BigBuckBunny_115k.mov
}
如果流 link 没问题,您应该可以在 Media->Open Network Stream...
下的 VLC 应用程序上进行流式传输
我已按照 link 的说明添加 Vlc.DotNet
库(.Core
、.Core.Interops
、.Forms
和 .Wpf
) 到我的项目的解决方案。
我还添加了 VideoLAN.LibVLC.Windows
库的 3.0.0 版本。
我在表单中添加了一个 vlcControl,结果是这样 Designer.cs
:
//
// vlcControl1
//
this.vlcControl1.BackColor = System.Drawing.Color.Black;
this.vlcControl1.Location = new System.Drawing.Point(384, 357);
this.vlcControl1.Name = "vlcControl1";
this.vlcControl1.Size = new System.Drawing.Size(75, 23);
this.vlcControl1.Spu = -1;
this.vlcControl1.TabIndex = 6;
this.vlcControl1.Text = "vlcControl1";
this.vlcControl1.VlcLibDirectory = ((System.IO.DirectoryInfo)(resources.GetObject("vlcControl1.VlcLibDirectory")));
this.vlcControl1.VlcMediaplayerOptions = null;
我在属性中添加了一个虚拟的 VlcLibDirectory,以便稍后更改它。
我的 vlcLib x86 版本的路径是:E:\testLouka\dansMaCamera2.0\dansMaCamera2.0\libvlc\win-x86
我尝试使用以下代码从 RTSP 流中获取视频源 url:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.vlcControl1 = new VlcControl()
{
Name = "vlc1",
Location = new Point(0, 0),
Dock = DockStyle.Fill,
VlcLibDirectory = new DirectoryInfo(Path.Combine("E:\testLouka\dansMaCamera2.0\dansMaCamera2.0", "libvlc", IntPtr.Size == 4 ? "win-x86" : "win-x64")),
Spu = -1,
VlcMediaplayerOptions = null,
Enabled = true
};
string[] options = { ":network-caching=500" };
vlcControl1.Play(new Uri(m_stream.URL), options);
}
}
m_stream.URL
变量返回一个 RTSP link 看起来像“rtsp://admin:admin123@190.19.191.19/Stream0
”
我的表单出现了,但是我的 vlcController 没有显示任何东西...
我查看了 https://github.com/ZeBobo5/Vlc.DotNet 的 wiki,但我卡住了...
我做错了什么?
您只需在表单中添加一个 vlcControl
并在其 VlcLibDirectoryNeeded
事件中添加一些代码。
/// <summary>
/// Looks for the vlc directory on the opening of the app
/// Opens a dialog if the libvlc folder is not found for the user to pick the good one
/// Folder for 32bits should be "libvlc\win-x86\" and "libvlc\win-x64\" for 64 bits
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void myVlcControl_VlcLibDirectoryNeeded(object sender, VlcLibDirectoryNeededEventArgs e)
{
var currentAssembly = Assembly.GetEntryAssembly();
var currentDirectory = new FileInfo(currentAssembly.Location).DirectoryName;
if (currentDirectory == null)
return;
if (IntPtr.Size == 4)
e.VlcLibDirectory = new DirectoryInfo(Path.GetFullPath(@".\libvlc\win-x86\"));
else
e.VlcLibDirectory = new DirectoryInfo(Path.GetFullPath(@".\libvlc\win-x64\"));
if (!e.VlcLibDirectory.Exists)
{
var folderBrowserDialog = new System.Windows.Forms.FolderBrowserDialog();
folderBrowserDialog.Description = "Select Vlc libraries folder.";
folderBrowserDialog.RootFolder = Environment.SpecialFolder.Desktop;
folderBrowserDialog.ShowNewFolderButton = true;
if (folderBrowserDialog.ShowDialog() == DialogResult.OK)
{
e.VlcLibDirectory = new DirectoryInfo(folderBrowserDialog.SelectedPath);
}
}
}
然后,您可以在表单中添加某种播放按钮并播放任何想要的 rtsp 流!
private void btnPlay_Click(object sender, EventArgs e)
{
myVlcControl.Play(MyStream.URL);//can test with rtsp://184.72.239.149/vod/mp4:BigBuckBunny_115k.mov
}
如果流 link 没问题,您应该可以在 Media->Open Network Stream...