使用 Vlc.DotNet 在 C# (WPF) 项目中集成 VLC 播放器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40723674/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 13:57:08  来源:igfitidea点击:

Integrate VLC player in C# (WPF) project using Vlc.DotNet

wpfxamlvlc

提问by Tchebourachka

I want to integrate to my project a VLC player to display video cameras streams. For that, I try to use Vlc.DotNet (2.1.126 version) in my WPF project.

我想将 VLC 播放器集成到我的项目中以显示摄像机流。为此,我尝试在我的 WPF 项目中使用 Vlc.DotNet(2.1.126 版本)。

My tests are done in the following XAML file (I'm a bit a beginner at XAML/WPF):

我的测试是在以下 XAML 文件中完成的(我是 XAML/WPF 的初学者):

<UserControl x:Class="TVSCS_View.VideoDisplay.VideoPlayerControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:ctrl="clr-namespace:TVSCS_View.VideoDisplay"
             xmlns:wpf="clr-namespace:Vlc.DotNet.Wpf;assembly=Vlc.DotNet.Wpf"
             mc:Ignorable="d" 
             d:DesignHeight="300" 
             d:DesignWidth="300"
             x:Name="controlVideoDisplay"
             DataContext="{Binding ElementName=controlVideoDisplay}">
    <Border BorderBrush="Black"
            BorderThickness="1">
        <Grid x:Name="videoDisplayLayoutRoot"
              Margin="5,5,5,5">
            <Image Source="{Binding ElementName=myVlcControl}" 
                   HorizontalAlignment="Stretch" 
                   VerticalAlignment="Stretch"/>
            <ctrl:VideoCommandsControl x:Name="videoPlayerControl"
                                       VerticalAlignment="Bottom"
                                       Height="25"
                                       Width="175"
                                       Visibility="Visible"
                                       Margin="10,0,10,20" />
            <ctrl:VideoTimeLineControl x:Name="timeLineControl"
                                       VerticalAlignment="Bottom"
                                       Margin="0,0,0,0"/>
        </Grid>
    </Border>
</UserControl>

And the associated .cs file is:

相关的 .cs 文件是:

using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Vlc.DotNet.Wpf;

namespace TVSCS_View.VideoDisplay
{
    /// <summary>
    /// Logique d'interaction pour VideoPlayerControl.xaml
    /// </summary>
    public partial class VideoPlayerControl : UserControl
    {
        public VlcControl myVlcControl;

        public VideoPlayerControl()
        {
            InitializeComponent();

            MediaPlayer media = new MediaPlayer();

            myVlcControl = new VlcControl();
            var currentAssembly = Assembly.GetEntryAssembly();
            var currentDirectory = new FileInfo(currentAssembly.Location).DirectoryName;

            if (Environment.Is64BitOperatingSystem)
            {
                myVlcControl.MediaPlayer.VlcLibDirectory = new DirectoryInfo(System.IO.Path.Combine(currentDirectory, @"C:\Users\t0115019\Documents\Visual Studio 2015\Projects\tvscs_display\packages\VLC\"));
            }

            myVlcControl.MediaPlayer.EndInit();
            myVlcControl.MediaPlayer.Play(new Uri("C:/Users/Documents/WP_20160908_11_16_53_Pro.mp4"));
        }
    }
}

Currently, I have an exception "FillNotFOundException" linked to "myVlcControl.MediaPlayer.EndInit()" line when I execute the application. If I delete this line, nothing is displayed in the UserControl.

目前,当我执行应用程序时,我有一个异常“FillNotFoundException”链接到“myVlcControl.MediaPlayer.EndInit()”行。如果我删除此行,则 UserControl 中不会显示任何内容。

Nota: I try to integrate the VlcControl using the following method:

注意:我尝试使用以下方法集成 VlcControl:

<UserControl x:Class="TVSCS_View.VideoDisplay.VideoPlayerControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:ctrl="clr-namespace:TVSCS_View.VideoDisplay"
             xmlns:wpf="clr-namespace:Vlc.DotNet.Wpf;assembly=Vlc.DotNet.Wpf"
             mc:Ignorable="d" 
             d:DesignHeight="300" 
             d:DesignWidth="300"
             x:Name="controlVideoDisplay"
             DataContext="{Binding ElementName=controlVideoDisplay}">
    <Border BorderBrush="Black"
            BorderThickness="1">
        <Grid x:Name="videoDisplayLayoutRoot"
              Margin="5,5,5,5">
            <wpf:VlcControl x:Name="myVlcControl" />
        </Grid>
    </Border>
</UserControl>

But in this case, i have the following messsage: the value of type "VlcControl" cannot be added to a collection or dictionary of type 'UIElementCollection'.

但在这种情况下,我有以下消息: “VlcControl”类型的值不能添加到“UIElementCollection”类型的集合或字典中

Do you have any solution for my little problem? Thanks

你对我的小问题有什么解决办法吗?谢谢

采纳答案by Grx70

The WPFversion of the VlcControlis just a WindowsFormsHostcontrol hosting the Windows Formsversion of the VlcControl. Judging by the error message (The value of type "VlcControl" cannot be added to a collection or dictionary of type 'UIElementCollection') you're simply missing a reference to the WindowsFormsIntegrationassembly, in which the WindowsFormsHostis defined (it can be found under Assemblies → Frameworkin the reference manager).

WPF版本VlcControl只是一个WindowsFormsHost托管Windows 窗体版本的VlcControl. 根据错误消息(“VlcControl”类型的值不能添加到“UIElementCollection”类型的集合或字典中)判断,您只是缺少对WindowsFormsIntegration程序集的引用,其中WindowsFormsHost定义了(可以在Assemblies下找到)参考管理器中的框架)。



Here's a fully working example of a WPFwindow hosting the VLCplayer. You need to install the Vlc.DotNet.Wpf NuGet package(and its dependencies) and reference the WindowsFormsIntegrationassembly.

这是托管VLC播放器的WPF窗口的完整工作示例。您需要安装Vlc.DotNet.Wpf NuGet 包(及其依赖项)并引用程序集。WindowsFormsIntegration

MainWindow.xaml

主窗口.xaml

<Window x:Class="HelloVlc.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:vlc="clr-namespace:Vlc.DotNet.Wpf;assembly=Vlc.DotNet.Wpf">
    <vlc:VlcControl x:Name="vlcPlayer" />
</Window>

MainWindow.xaml.cs

主窗口.xaml.cs

public partial class MainWindow
{
    public MainWindow()
    {
        InitializeComponent();
        vlcPlayer.MediaPlayer.VlcLibDirectory =
            //replace this path with an appropriate one
            new DirectoryInfo(@"c:\Program Files (x86)\VideoLAN\VLC\");
        vlcPlayer.MediaPlayer.EndInit();
        vlcPlayer.MediaPlayer.Play(new Uri("http://download.blender.org/peach/" +
            "bigbuckbunny_movies/big_buck_bunny_480p_surround-fix.avi"));
    }
}