首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在C#项目中使用Vlc.DotNet集成VLC播放器

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

Stack Overflow用户
提问于 2016-11-21 15:26:43
回答 1查看 20.3K关注 0票数 2

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

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

代码语言:javascript
复制
<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文件是:

代码语言:javascript
复制
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中不会显示任何内容。

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

代码语言:javascript
复制
<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‘类型的集合或字典中。

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-11-21 20:27:11

VlcControl的WPF版本只是一个WindowsFormsHost控件,托管了VlcControl的Windows版本。从错误消息(类型"VlcControl“的值不能添加到‘UIElementCollection’类型的集合或字典中)判断,您只是忽略了对WindowsFormsIntegration程序集的引用,其中定义了WindowsFormsHost (可以在参考管理器中的Assemblies→框架下找到它)。

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

MainWindow.xaml

代码语言:javascript
复制
<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

代码语言:javascript
复制
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"));
    }
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40723674

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档