C#調用VlcControl做一個播放器


開發環境:

Visual Studio 2015

.Net Framework 4.5

1.新建一個Windows窗體應用程序

修改框架為.Net Framework 4.5

 

2.管理NuGet包

下載安裝5個包

VideoLAN.LibVLC.Windows

Vlc.DotNet.Core 

Vlc.DotNet.Core.Interops 

Vlc.DotNet.Forms 

Vlc.DotNet.Wpf 

 

3.添加VlcControl

工具箱添加VlcControl,dll位於當前項目中

將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);
                }
            }
        }

4.播放視頻

使用VlcControl.Play()方法播放視頻

vlcControl1.Play("http://**************/******.flv");//只能播放網絡流視頻
vlcControl1.SetMedia(new System.IO.FileInfo(@"f:\1.flv"));//本地視頻
vlcControl1.Play();

運行結果:


參考:

https://blog.csdn.net/xuehuic/article/details/53914874

https://bbs.csdn.net/topics/390168224

https://cloud.tencent.com/developer/ask/148529

https://github.com/ZeBobo5/Vlc.DotNet/wiki/Using-Vlc.DotNet-in-WinForms

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM