0

Yesterday I posted thread about VLC player and it's controls.

Adding xaml elements for VLC player to WPF application

After some time, I found a possible solution here:

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

I changed it just a little to fir my specific case:

Xaml:

 <Window x:Class="WpfApp1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:vlc="clr-namespace:Vlc.DotNet.Wpf;assembly=Vlc.DotNet.Wpf"
    xmlns:local="clr-namespace:WpfApp1"
    mc:Ignorable="d"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <vlc:VlcControl x:Name="vlcPlayer" />
    <MediaElement x:Name="movOld" HorizontalAlignment="Left" Height="100" Margin="57,74,0,0" VerticalAlignment="Top" Width="100" Source="Resources/Global_Large_crate_opens.mp3"/>
</Grid>

Code:

    public MainWindow()
    {
        InitializeComponent();
        vlcPlayer.MediaPlayer.VlcLibDirectory = new DirectoryInfo(@"D:\VideoLAN\VLC\");
        vlcPlayer.MediaPlayer.EndInit();                       

        Uri thisUri = new Uri(@"C:\Users\User\Documents\Visual Studio 2017\Projects\WpfApp1\WpfApp1\Resources\VideoFallingFromTable.ogv", UriKind.RelativeOrAbsolute);
        vlcPlayer.MediaPlayer.Play(thisUri);
    }

This code works perfectly, when I give a full path from my computer. However, for my game, I need to take the path from MediaElements, like movOld in this case.

However, when I try to do this:

        Uri thisUri = new Uri(movOld.Source.ToString(), UriKind.RelativeOrAbsolute);
        vlcPlayer.MediaPlayer.Play(thisUri);

I get this exception:

{"This operation is not supported for a relative URI."}

All my attempts to get file name, or file path as a string, or to use "ToAbsolute()" brought same extension.

Can someone please help me find where the error is, and how to fix it?

Since the game is to be used on many computers, with different possible installation places, it is of vital importance to load the new source for existing MediaElements.

Kiran Joshi
  • 1,758
  • 2
  • 11
  • 34
EvgenieT
  • 209
  • 1
  • 4
  • 16

1 Answers1

0

I'm not sure why you are using Media Element when you have VLC control. Anyways, make sure that the Media Element source path "Resources/Global_Large_crate_opens.mp3" exists. Are you sure that its a forward slash there? usually in Windows, paths use a backslash. Also if your path is absolute then you need to define it like below:

Uri thisUri = new Uri(movOld.Source.ToString(), UriKind.Absolute);
vlcPlayer.MediaPlayer.Play(thisUri);

The problem is only with your Media Element Source path. Just make sure whatever you put there exists and accessible.

Update:

I recently worked with VLC control, where I built my own User Control to hide VLC library logic and exposed File Path & other dependency properties to specify them from XAML. You can try using the minimal version of that control in your app.

public partial class VLCControl : UserControl
{
    public VLCControl()
    {
        InitializeComponent();
        var currentDirectory = AppDomain.CurrentDomain.BaseDirectory;
        string platform = Environment.Is64BitProcess ? "x64" : "x86";
        var dirPath = System.IO.Path.Combine(currentDirectory, "vlclib", platform);
        vlc.MediaPlayer.VlcLibDirectory = new DirectoryInfo(dirPath);
        vlc.MediaPlayer.EndInit();
    }

    public void Play()
    {
        if (vlc.IsInitialized && !String.IsNullOrWhiteSpace(FilePath))
        {
            var options = new string[]
            {
                String.Format("input-repeat={0}", Repeat)
            };
            vlc.MediaPlayer.SetMedia(new Uri(FilePath), options);

            if (!String.IsNullOrWhiteSpace(AspectRatio))
                vlc.MediaPlayer.Video.AspectRatio = AspectRatio;

            vlc.MediaPlayer.Play();
        }
    }

    public void Stop()
    {
        vlc.MediaPlayer.Stop();
    }

    public string AspectRatio
    {
        get { return (string)GetValue(AspectRatioProperty); }
        set { SetValue(AspectRatioProperty, value); }
    }

    public static readonly DependencyProperty AspectRatioProperty =
        DependencyProperty.Register("AspectRatio", typeof(string), typeof(VLCControl));


    public string FilePath
    {
        get { return (string)GetValue(FilePathProperty); }
        set { SetValue(FilePathProperty, value); }
    }

    public static readonly DependencyProperty FilePathProperty =
        DependencyProperty.Register("FilePath", typeof(string), typeof(VLCControl));


    public int Repeat
    {
        get { return (int)GetValue(RepeatProperty); }
        set { SetValue(RepeatProperty, value); }
    }

    public static readonly DependencyProperty RepeatProperty =
        DependencyProperty.Register("Repeat", typeof(int), typeof(VLCControl), new PropertyMetadata(-1));

    private void vlc_Loaded(object sender, RoutedEventArgs e)
    {
        Play();
    }
}

The XAML file code is:

<UserControl x:Class="VLCControl"
             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" 
             mc:Ignorable="d" 
             xmlns:vlc="clr-namespace:Vlc.DotNet.Wpf;assembly=Vlc.DotNet.Wpf"
             d:DesignHeight="300" d:DesignWidth="300" >
    <vlc:VlcControl x:Name="vlc" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Loaded="vlc_Loaded"/>
</UserControl>

If it helped you then don't forget to accept the answer :)

Saad
  • 198
  • 2
  • 12
  • I have an earlier version, with MediaElements and their sources. I didn't find a way to put a source VLC control in xaml, so trying to take relevant path from existing MediaElements. Of course, if there is a way to put path to vlc control directly, that'll be better. – EvgenieT Jun 30 '18 at 14:03
  • This is not the right way of doing things. Check my updated answer to use your own user control for this purpose. – Saad Jun 30 '18 at 14:23