【问题标题】:Integrate VLC player in C# (WPF) project using Vlc.DotNet使用 Vlc.DotNet 在 C# (WPF) 项目中集成 VLC 播放器
【发布时间】:2016-11-21 15:26:43
【问题描述】:

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

我的测试是在以下 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>

而关联的.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"));
        }
    }
}

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

注意: 我尝试使用以下方法集成 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>

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

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

【问题讨论】:

    标签: wpf xaml vlc


    【解决方案1】:

    WPF 版本的VlcControl 只是一个WindowsFormsHost 控件,它承载了VlcControlWindows 窗体 版本。从错误消息来看(“VlcControl”类型的值不能添加到“UIElementCollection”类型的集合或字典中),您只是缺少对WindowsFormsIntegration 程序集的引用,其中WindowsFormsHost 已定义(可以在参考管理器中的 Assemblies → Framework 下找到)。


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

    MainWindow.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

    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"));
        }
    }
    

    【讨论】:

    • 非常感谢您的回答。这是工作!可悲的是,WPF 视频似乎总是在 Vlc.DotNet 中名列前茅。
    • @Tchebourachka 不幸的是,这正是 Windows 窗体主机的工作方式。
    • 还有其他选择。似乎 meta.vlc (github.com/higankanshi/Meta.Vlc) 没有空域问题。旧版本的 vlcdotnet 也会发生同样的情况
    • 谢谢,我尝试使用 Meta.Vlc。我在我的项目引用中添加了 Meta.Vlc 和 Metal.Vlc.Wpf,并修改了 AssemblyInfo.cs 以添加“VlcSettings”行。但是如果我在我的 XAML 文件中添加 (使用 xmlns:wpf="clr-namespace:Meta.Vlc.Wpf;assembly=Meta.Vlc.Wpf"),它不会'不起作用:我有“名称 VlcPlayer 不存在于命名空间“clr-namespace:Meta.Vlc.Wpf;assembly=Meta.Vlc.Wpf”。有什么想法吗?再次感谢。
    • 迟到但仍然有效。你需要在你的 xml `xmlns:vlc="clr-namespace:Vlc.DotNet.Wpf;assembly=Vlc.DotNet.Wpf">`
    猜你喜欢
    • 1970-01-01
    • 2020-08-21
    • 2011-08-18
    • 2014-01-19
    • 2020-11-13
    • 1970-01-01
    • 1970-01-01
    • 2013-01-25
    • 1970-01-01
    相关资源
    最近更新 更多