简体   繁体   中英

LibVLCSharp: how to stop the application when player closes

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

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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM