Home > database >  Playing video yeild black screen with audio(Vlc.DotNet and libvlcsharp)
Playing video yeild black screen with audio(Vlc.DotNet and libvlcsharp)

Time:11-10

I have a simple WinForms solution with 2 video players one is Vlc.DotNet and the second is libvlcsharp

and besides that, I have a simple button that plays both videos.

for libvlcsharp i followed this example

and for Vlc.DotNet i used this example

further more im attaching both logFiles Loglibvlcsharp.txt and LogVlcDotNet

this is my code

public partial class QueueDisplayVideoView : Form
{
    private string[] paths, files;
    private int videoIndex = 0;

    public LibVLC _libVLC;
    public MediaPlayer _mp;
    public LibVLCSharp.Shared.Media media;

    public QueueDisplayVideoView()
    {
        InitializeComponent();
        Core.Initialize();
        _libVLC = new LibVLC(enableDebugLogs:true);
        _libVLC.SetLogFile(@"C:\Temp\LogLib.txt");
        _mp = new MediaPlayer(_libVLC);
        videoView.MediaPlayer = _mp;

    }


    protected override void OnLoad(EventArgs e)
    {           
        _presenter.OnViewReady();
        base.OnLoad(e);

        LoadVideoFiles();
        SetPlayerAspectRatio();
        //StartAutoPlay();
    }

    private void StartAutoPlay()
    {
        vlcControl.Play(new Uri(paths[videoIndex]));
        _mp.Play(new LibVLCSharp.Shared.Media(_libVLC, new Uri(paths[videoIndex])));
    }

    private void LoadVideoFiles()
    {     
        var path = some path
        paths = Directory.GetFiles(path, "*", SearchOption.AllDirectories);
        files = new string[paths.Length];
        for (int i = 0; i < paths.Length; i  )
        {
            files[i] = Path.GetFileName(paths[i]);
        }
    
    }

    private void SetPlayerAspectRatio()
    {
        var videoWidth = vlcControl.Width.ToString();
        var videoHieght = vlcControl.Height.ToString();
        vlcControl.Video.AspectRatio = videoWidth   ":"   videoHieght;
    }

    private void vlcControl_VlcLibDirectoryNeeded(object sender, Vlc.DotNet.Forms.VlcLibDirectoryNeededEventArgs e)
    {
        // var currentAssembly = System.Reflection.Assembly.GetEntryAssembly();
        var currentDirectory = @"Q:\bin";  //new FileInfo(currentAssembly.Location).DirectoryName;
        // Default installation path of VideoLAN.LibVLC.Windows
        var fullPath = Path.Combine(currentDirectory, "libvlc", IntPtr.Size == 4 ? "win-x86" : "win-x64");
        e.VlcLibDirectory =  new DirectoryInfo(fullPath);
    }


    private void vlcControl_EndReached(object sender, Vlc.DotNet.Core.VlcMediaPlayerEndReachedEventArgs e)
    {
        GetNextVideoIndex();
        ThreadPool.QueueUserWorkItem(_ => vlcControl.Play(new Uri(paths[videoIndex])));
    }

    private void button1_Click(object sender, EventArgs e)
    {
        StartAutoPlay();
    }

    private void GetNextVideoIndex()
    {
        if (videoIndex < files.Length - 1)
        {
            videoIndex = videoIndex   1;
        }
        else
        {
            videoIndex = 0;
        }
    }
}

CodePudding user response:

Have you tried using this?
it should be native to winforms,
all you have to do choose the right .dll at toolbox-> choose items-> com compoents-> windows media player.

  • Related