Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
922 views
in Technique[技术] by (71.8m points)

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

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.

My tests are done in the following XAML file (I'm a bit a beginner at 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:

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:Users0115019DocumentsVisual Studio 2015Projectsvscs_displaypackagesVLC"));
            }

            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.

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

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

Do you have any solution for my little problem? Thanks

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The WPF version of the VlcControl is just a WindowsFormsHost control hosting the Windows Forms version 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 WindowsFormsIntegration assembly, in which the WindowsFormsHost is defined (it can be found under Assemblies → Framework in the reference manager).


Here's a fully working example of a WPF window hosting the VLC player. You need to install the Vlc.DotNet.Wpf NuGet package (and its dependencies) and reference the WindowsFormsIntegration assembly.

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)VideoLANVLC");
        vlcPlayer.MediaPlayer.EndInit();
        vlcPlayer.MediaPlayer.Play(new Uri("http://download.blender.org/peach/" +
            "bigbuckbunny_movies/big_buck_bunny_480p_surround-fix.avi"));
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...