c# - 找不到媒体和设备对象或命名空间
问题描述
我对 c# 相当陌生,正在尝试编写一个基本程序来通过 Onvif 相机设备流式传输视频。我能够大部分地遵循和理解这个 8 分钟的教程。
但有几件事我想不通。即使添加了 Vlc 引用和两个 Onvif 服务引用,也有一些代码无法编译。该教程很难遵循,因为他跳过了主要步骤。我正在尝试确定以下代码中的Device
& Media
&代表什么(根据他的教程)。EndPointAddress
public partial class MainWindow : Window
{
UriBuilder deviceUri;
Media.Media2Client media;
Media.MediaProfile[] profiles;
public MainWindow()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//Constructor for the custom Uri requires the onvif service address.
deviceUri = new UriBuilder("http:/onvif/device_service");
//Sting manipulation: Need to check if our address contains a port. (Input validation)
string[] addr = Adress.Text.Split(':');
deviceUri.Host = addr[0];
if (addr.Length == 2)
deviceUri.Port = Convert.ToInt16(addr[1]);
System.ServiceModel.Channels.Binding binding;
HttpTransportBindingElement httpTransport = new HttpTransportBindingElement();
httpTransport.AuthenticationScheme = System.Net.AuthenticationSchemes.Digest;
binding = new CustomBinding(new TextMessageEncodingBindingElement(MessageVersion.Soap12WSAddressing10, Encoding.UTF8), httpTransport);
Device.DeviceClient device = new Device.DeviceClient(binding, new EndpointAddress(deviceUri.ToString()));
Device.Service[] services = device.GetServices(false);
Device.Service xmedia = services.FirstOrDefault(s => s.Namespace == "http://www.onvif.org/ver20.media/wsdl");
if (xmedia != null)
{
media = new Media.Media2Client(binding, new EndPointAddress(deviceUri.ToString()));
media.ClientCredentials.HttpDigest.ToString.ClientCredential.UserName = Login.Text;
media.ClientCredentials.HttpDigest.ToString.ClientCredential.UserName = Login.Text;
media.ClientCredentials.HttpDigest.AllowedImpresonationLevel = System.Security.Principal.TokenImpersonationLevel.Impersonation;
profiles = media.GetProfiles(null, null);
if (profiles != null)
foreach (var p in profiles)
listBox1.Items.Add(p.Name);
}
listBox1.SelectionChanged += OnSelectionChanged;
video.MediaPlayer.VlcLibDirectoryneeded += MediaPlayer_VlcLibDirectoryNeeded;
video.MediaPlayer.EndInit();
}
private void MediaPlayer_VlcLibDirectoryNeeded(object sender, Vlc.DotNet.Forms.VlcLibDirectoryNeededEventArgs e)
{
if (IntPtr.Size == 4)
{
e.VlcLibDirectory = new System.IO.DirectoryInfo(@"C:\Program Files\VideoLAN\VLC");
}
}
private void OnSelectionChanged(object sender, RoutedEventArgs e)
{
if (profiles != null && listBox1.SelectedIndex >=)
{
UriBuilder uri = new UriBuilder(media.GetStreamUri("RtspOverHttp", profiles[listBox1.SelectedIndex].token));
uri.Host = deviceUri.Host;
uri.Port = deviceUri.Port;
uri.Scheme = "rtsp";
information.Text = uri.Path; ;
string[] options = { ":rtsp-http", ":rtps-http-port=" + uri.Port, ":rtsp-user=" + Login.Text, ":rtsp-pwd=" + passwordBox.Password };
video.MediaPlayer.Play(uri.Uri, options);
}
}
}
解决方案
两者Device
和Media
都是项目中的命名空间,其类是从您引用的 Web 服务自动生成的。当您通过References > Add Service Reference 添加服务引用时,Visual Studio 将使用WCF Web 服务引用工具从 WSDL 元数据创建 WCF 客户端代理的源代码,以便可以访问该服务。
WSDL 或Web 服务描述语言是一种基于 XML 的语言,用于描述 Web 服务的接口,您可以在此处的教程中找到:
Device
接口 - 参见https://www.onvif.org/ver10/device/wsdl/devicemgmt.wsdl的定义Media
接口 - 参见https://www.onvif.org/ver20/media/wsdl/media.wsdl的定义
该类EndpointAddress
位于System.ServiceModel
程序集中,您可能在那里打错了字。但是,它还有一个 NuGet 包,称为System.Private.ServiceModel
,但您不需要它。从以下文档:EndpointAddress
提供客户端用来与服务端点通信的唯一网络地址。
推荐阅读
- calculated-columns - 计算列 if 语句“除法”
- linkedin - Linkedin - 共享实体无权执行此操作
- java - cucumber.runtime.CucumberException:java.lang.NoSuchMethodException
- java - 无法将对象从 Firebase 数据转换为 POJO
- c# - 如何生成五组随机数
- php - 年份日期 PHP 中 4 个期间之间的折扣文本
- java - Elasticsearch:将新元素附加到嵌套的对象数组
- racket - 我应该怎么做才能听这个列表?
- python-3.x - 如何修复:尝试使用 xml.etree 查找标签时出现类型错误
- azure - 有没有办法通过复制粘贴将分支添加到逻辑应用程序中?