1

I'm using the LibVLCSharp.WPF to play a video using the VideoView control. I've binded MediaPlayer and Visibility in my ViewModel. I can get it to play the video but rather than be embedded inside the Grid control it creates and opens a new window and plays the video there. I've searched and saw there is an airspace issue but maybe there is something I'm doing wrong to keep the video from playing inside the grid like when you place an image in an image control.

The player is inside a UserControl that is inside the MainWindow.

XAML

<Grid>    
    <media:VideoView x:Name="MediaPlayer"
        VerticalAlignment="Center"
        HorizontalAlignment="Center"
        MediaPlayer="{Binding MyMediaPlayer}"
        Visibility="{Binding IsMediaPlayerVisible, Converter={StaticResource VisConverter}}"/>
</Grid>

ViewModel

void StartVideo()
{
    IsMediaPlayerVisible = true;

    Core.Initialize();
    using (_libVLC = new LibVLC())
    {
        MyMediaPlayer = new MediaPlayer(_libVLC);
        MyMediaPlayer.Media = new Media(_libVLC, new Uri(@"path\to\media\file"));
        MyMediaPlayer.Play();
    }
}

Can I embed the VideoView like a Image control inside a Grid?

Jackdaw
  • 7,626
  • 5
  • 15
  • 33
Mmedi005
  • 35
  • 7

1 Answers1

0

Please always share a full minimal sample. Your Q is missing most of the code.

LibVLC will create a new Windows when the Window provided (e.g. the VideoView here) isn't ready or fully initialized.

As a rule of thumb, always start with the official libvlcsharp samples. https://code.videolan.org/videolan/LibVLCSharp/-/blob/3.x/samples/LibVLCSharp.WPF.Sample/Controls.xaml.cs

public Controls(Example1 Parent)
{
    parent = Parent;

    InitializeComponent();

    // we need the VideoView to be fully loaded before setting a MediaPlayer on it.
    parent.VideoView.Loaded += VideoView_Loaded;
    PlayButton.Click += PlayButton_Click;
    StopButton.Click += StopButton_Click;
    Unloaded += Controls_Unloaded;
}

private void VideoView_Loaded(object sender, RoutedEventArgs e)
{
    _libVLC = new LibVLC(enableDebugLogs: true);
    _mediaPlayer = new MediaPlayer(_libVLC);

    parent.VideoView.MediaPlayer = _mediaPlayer;
}
mfkl
  • 1,914
  • 1
  • 11
  • 21
  • Can the VideoView control be inside the UserControl? In the example I see its in a Example Window. – Mmedi005 Mar 09 '21 at 04:00
  • Try it. The VideoView is a `ContentControl` – mfkl Mar 09 '21 at 05:42
  • You can use a User control, but you need to make sure the control is loaded before you call Play() – cube45 Mar 09 '21 at 06:43
  • Ive hooked up his Loaded event to the VideoView and used the same lines of code as he the examples showed and no luck. Is there something in the load that I am missing? Im also calling Core.Initialize() in the App file like in the example provided in the samples of his site. Should load be fired somewhere else or what are the key lines of code to fully load the VideoView before play other than the ones provided above? – Mmedi005 Mar 09 '21 at 11:29
  • thank you @mfkl and cube45 I got it to work. VideoView does not like the binding of Visibility and was causing it to create a new window. Then I saw that I needed to specify a Width and a Height to get it to show. I manipulate the Width and Height from 0 to a specific number to hide and show player as a workaround. Thank you again – Mmedi005 Mar 09 '21 at 15:38