专注于分享
分享好资源

C#的一个视频播放库的封装-Vlc.DotNet

VLC的强大是众所周知的,如果你不知道,那你现在就知道了。

这里介绍一下VLC.DotNet类库的使用。nuget里有这个,一搜就可以了 ,关键字VLC.DotNet。

1)Vlc.DotNet.Forms :是包含winform界面控件的库,使用的时候需要加入到工具箱,在拖拽到界面;是对VLC.DotNet.Core的再次封装。

2)Vlc.DotNet.Wpf : 包括wpf的,在hood方式下是使用WinForm控件;

3)Vlc.DotNet.Core : 基础依赖包,如果是转换、编解码、做服务不需要界面的,可以考虑用这个来实现。

安装Vlc.DotNet

nuget 里搜索:VLC.DotNet

安装这几个库:.Core .Core.Interops .Forms VideoLAN.LibVLC.Windows

安装完之后在工具箱可以看到

将VlcControl添加到窗体上

在VlcControl的VlcLibDirectoryNeeded事件中添加如下代码(必须)

/// <summary>
/// Looks for the vlc directory on the opening of the app
/// Opens a dialog if the libvlc folder is not found for the user to pick the good one
/// Folder for 32bits should be "libvlc\win-x86\" and "libvlc\win-x64\" for 64 bits
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void myVlcControl_VlcLibDirectoryNeeded(object sender, VlcLibDirectoryNeededEventArgs e)
{
    var currentAssembly = Assembly.GetEntryAssembly();
    var currentDirectory = new FileInfo(currentAssembly.Location).DirectoryName;

    if (currentDirectory == null)
        return;
    if (IntPtr.Size == 4)
        e.VlcLibDirectory = new DirectoryInfo(Path.GetFullPath(@".\libvlc\win-x86\"));
    else
        e.VlcLibDirectory = new DirectoryInfo(Path.GetFullPath(@".\libvlc\win-x64\"));

    if (!e.VlcLibDirectory.Exists)
    {
        var folderBrowserDialog = new System.Windows.Forms.FolderBrowserDialog();
        folderBrowserDialog.Description = "Select Vlc libraries folder.";
        folderBrowserDialog.RootFolder = Environment.SpecialFolder.Desktop;
        folderBrowserDialog.ShowNewFolderButton = true;
        if (folderBrowserDialog.ShowDialog() == DialogResult.OK)
        {
            e.VlcLibDirectory = new DirectoryInfo(folderBrowserDialog.SelectedPath);
        }
    }
}

然后你可以添加并播放任何流:

vlcControl1.Play("http://**************/******.flv"); //只能播放网络流视频
vlcControl1.SetMedia(new System.IO.FileInfo(@"f:\1.flv")); //播放本地视频
vlcControl1.Play();

运行结果

赞(0)

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址