0

I am trying to write a simple player application using LibVLCSharp and I am wondering how to stop the app when player closes. Currently, it just freezes and doesn't stop the app even though I added SetExitHandler callback.

using System;
using System.Threading;
using LibVLCSharp.Shared;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            Core.Initialize();

            using var libVlc = new LibVLC();
            using var mediaPlayer = new MediaPlayer(libVlc);
            libVlc.SetExitHandler(() =>
            {
                Environment.Exit(1);
            });

            var media = new Media(libVlc, "v4l2:///dev/video0", FromType.FromLocation);
            media.AddOption(":v4l2-standard=ALL :live-caching=300");

            mediaPlayer.Play(media);
            Thread.Sleep(TimeSpan.FromSeconds(10));
        }
    }
}

Log after I close the window:

[00007fa164004790] gl gl: Initialized libplacebo v2.72.0 (API v72)
[00007fa17400a7c0] main decoder error: buffer deadlock prevented
[00007fa1600429a0] xcb_window window error: X server failure
Node.JS
  • 1,042
  • 6
  • 44
  • 114
  • Does the `MediaPlayer.EndReached` event help you? – Jon Skeet Oct 07 '21 at 05:48
  • @JonSkeet just tried it. No luck, same behavior. I even removed my `SetExitHandler` callback. BTW that you so much for all your help with C# and your book is awesome! – Node.JS Oct 07 '21 at 05:50
  • Glad I could help on that front - let's see if I can help on the VLC front too. (I'm using this library myself for something at the moment.) Now... in terms of other events you might want to look at, are `EncounteredError` or `Stopped` raised? The fact that you're seeing errors in the log is slightly alarming, as if LibVLC isn't handling the closure entirely properly :( – Jon Skeet Oct 07 '21 at 05:54
  • It's kind of works. It sometimes stops the application after 30 seconds of closing the window. Sometimes doesn't. I think some kind of deadlock is happening. – Node.JS Oct 07 '21 at 06:04
  • @NodeJS I tried your code on my machine and it seems the MediaPlayer.Play(Media) function is non-blocking. Meaning, the whole application just holds until the Thread.Sleep(...) finishes. Which in turn will kill VLC. When extending the Thead.Sleep(...) to longer than the video, the SetExitHandler event isn't called though. I haven't used LibVLCSharp before, but is your example actually valid? – sbecker Oct 07 '21 at 06:19
  • I am not sure if it's valid. You are right, `Thread.Sleep` is probably messing up everything. I copied most of the code from https://stackoverflow.com/a/60386481/1834787 If `Thread.Sleep` is a bad approach, then I don't know what to replace the spin loop without `Thread.Sleep`. – Node.JS Oct 07 '21 at 06:26
  • 1
    @Node.JS Something else I noticed is that you use the ExitHandler of LibVLV. But the library doesn't exit when the video stops. You can use MediaPlayer.EndReached event. This will trigger when the video ends. – sbecker Oct 07 '21 at 06:28
  • You can use Console.ReadKey(); instead of Thread.Sleep(...) – sbecker Oct 07 '21 at 06:28

1 Answers1

1

The following code example from LibVLCSharp GitHub Page shows how to play the video in a console application.

Core.Initialize();

using var libvlc = new LibVLC(enableDebugLogs: true);
using var media = new Media(libvlc, new Uri(@"C:\tmp\big_buck_bunny.mp4"));
using var mediaplayer = new MediaPlayer(media);

mediaplayer.Play();

Console.ReadKey();

Note the use of Console.ReadKey() to wait for the user to press a key before closing the application and subsequently closing the player.

To exit the application automatically when the video ends, you can use MediaPlayer.EndReached event as shown here:

Core.Initialize();

using var libvlc = new LibVLC(enableDebugLogs: true);
using var media = new Media(libvlc, new Uri(@"C:\tmp\big_buck_bunny.mp4"));
using var mediaplayer = new MediaPlayer(media);
mediaplayer.EndReached += (s, e) => Environment.Exit(0);

mediaplayer.Play();

Console.ReadKey();
sbecker
  • 553
  • 4
  • 12
  • We know `EndReached` should work, but there is a bigger deadlock problem. – Node.JS Oct 07 '21 at 06:36
  • @Node.JS can you describe the issue a little more? From your original question, it seems that using Thread.Sleep and LibVLC.SetExitHandler were causing the issues. What exactly is the behavior when using MediaPlayer.EndReached? How long is the video you're testing with? What happens with the above code when you press a key while the video is playing? Does the VLC window close and the app stop? – sbecker Oct 07 '21 at 06:47
  • I confirm that the code above is accurate. The exit handler is not for what you think it is https://videolan.videolan.me/vlc/group__libvlc__core.html#gad3039ac5188e8f11eebc1ab0491252ee – mfkl Oct 07 '21 at 09:49