基於VLC庫C#開發可播放攝像頭及任意格式視頻的播放器


前言

本文主要講述,在WPF中,借助Vlc.DotNet調用VLC類庫,實現視頻播功能,下面我們先來做開發前的准備工作。

准備工作

首先,我們創建一個項目WpfVLC,然后,進入Neget搜索Vlc.DotNet,得到如下界面:

我們選擇Vlc.DotNet.Wpf,點擊安裝(這里我已經安裝了,所以圖中顯示為卸載)。

然后,我們去VLC官網,下載VLC播放器。

VLC官網:http://www.videolan.org/

因為我的電腦是64位的,所以我下載64位的VLC版本,如下圖:

 下載完成后,正常安裝即可,下載的文件截圖如下:

安裝完成后,我們找到安裝的具體位置並打開,如下圖:

在文件夾內我們找到文件libvlc.dll,libvlccore.dll和文件夾plugins,然后將他們復制出來。

現在我們回到我們剛剛創建的項目WpfVLC,進入文件目錄,打開debug文件夾,然后我們在其目錄下創建一個文件夾libvlc,如下:

然后,在在liblic下建立一個文件夾win-x64,如下:

再然后,我們將剛剛復制的vlc的三個文件,放到這個文件夾下,如下:

到此,我們的准備工作就完成了,現在開始編碼。

使用Vlc.DotNet播放視頻

現在,我們進入項目的代碼開發。

首先我們將項目設置為64位項目,因為我們使用的VLC是64的。

然后,我們打開MainWindow頁面。

在頁面命名空間引入的地方加入Vlc.DotNet的命名空間。

?
1
xmlns:vlc= "clr-namespace:Vlc.DotNet.Wpf;assembly=Vlc.DotNet.Wpf"

接着,我們在頁面布局中加入VlcControl控件和打開文件、播放、停止的按鈕,如下:

?
1
2
3
4
5
6
7
8
<DockPanel DockPanel.Dock= "Bottom" >
     <StackPanel Height= "50" DockPanel.Dock= "Bottom" Orientation= "Horizontal" >
         <Button Name= "btnOpen" Content= "打開文件" Click= "open_Click" Width= "80" ></Button>
         <Button Name= "btnPause" Content= "暫停" Click= "pause_Click" Width= "50" ></Button>
         <Button Name= "btnStop" Content= "停止" Click= "stop_Click" Width= "50" ></Button>
     </StackPanel>
</DockPanel>
<vlc:VlcControl x:Name= "VlcControl" />

然后,我們編寫xaml.cs文件的代碼,如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
public partial class MainWindow : Window
{
     private string filePath;
     public MainWindow()
     {
         InitializeComponent();
     }
     private void Window_Loaded( object sender, RoutedEventArgs e)
     {
         var currentAssembly = Assembly.GetEntryAssembly();
         var currentDirectory = new FileInfo(currentAssembly.Location).DirectoryName;
         var libDirectory = new DirectoryInfo(System.IO.Path.Combine(currentDirectory, "libvlc" , IntPtr.Size == 4 ? "win-x86" : "win-x64" ));
         this .VlcControl.SourceProvider.CreatePlayer(libDirectory);
     }
     private void open_Click( object sender, RoutedEventArgs e)
     {
         OpenFileDialog ofd = new OpenFileDialog();
         ofd.Multiselect = false ;
         ofd.Title = "請選擇視頻文件" ;
         var result = ofd.ShowDialog();
         if (result == System.Windows.Forms.DialogResult.OK)
         {
             filePath = ofd.FileName;
             try
             {
                 btnPause.Content = "暫停" ;
                 this .VlcControl.SourceProvider.MediaPlayer.Play( new Uri(filePath));
             }
             catch (Exception ex)
             {
                 
             }
         }
     }
     public void pause_Click( object sender, RoutedEventArgs e)
     {
         if (btnPause.Content.ToString() == "播放" )
         {
             btnPause.Content = "暫停" ;
             this .VlcControl.SourceProvider.MediaPlayer.Play();
         }
         else
         {
             btnPause.Content = "播放" ;
             this .VlcControl.SourceProvider.MediaPlayer.Pause();
         }
     }
     private void stop_Click( object sender, RoutedEventArgs e)
     {
         new Task(() =>
         {
             this .VlcControl.SourceProvider.MediaPlayer.Stop(); //這里要開線程處理,不然會阻塞播放
 
         }).Start();
     }
}

這樣,我們就完成了最基本的視頻播放、暫停、停止的功能。

可以看到,播放、暫停、停止的代碼非常簡單,就是調用控件的play,pause,stop函數即可。

因為VLC非常優秀,可以支持多種格式的文件播放,所以我們寫的這個播放器也就可以打開任意類型的視頻文件。

播放界面如下:

現在,加入Slider控制播放進度和音量。

Slider樣式,參考如下文章:

WPF依賴屬性的正確學習方法

WPF滑塊控件(Slider)的自定義樣式

VlcControl控制播放進度的方法很簡單,如下:

?
1
2
3
4
5
6
7
8
9
private void Slider1_DragCompleted( object sender, System.Windows.Controls.Primitives.DragCompletedEventArgs e)
{
     var position = ( float )(slider1.Value / slider1.Maximum);
     if (position == 1)
     {
         position = 0.99f;
     }
     this .VlcControl.SourceProvider.MediaPlayer.Position = position; //Position為百分比,要小於1,等於1會停止
}

控制播放聲音的方法如下:

?
1
2
3
4
5
private void Slider2_DragCompleted( object sender, System.Windows.Controls.Primitives.DragCompletedEventArgs e)
{
     //Audio.Volume:音量的百分比,值在0—200之間
     this .VlcControl.SourceProvider.MediaPlayer.Audio.Volume = ( int )slider2.Value;
}

這樣我們的播放器就開發完成了。

最終界面如下:

播放其他視頻源

播放RTSP

通過上面的代碼編寫,我們了解到了,在C#里使用VLC播放視頻的代碼非常簡單,只要在Play函數中寫入地址即可。

那么播放RTSP自然是同理,只要在Play中寫入RTSP的地址即可,如下:

?
1
this .VlcControl.SourceProvider.MediaPlayer.Play( new Uri(rtsp: //192.168.1.111));

播放攝像頭

播放攝像頭在這里也很簡單,只是Play的入參稍微要注意一下即可,如下:

?
1
2
3
4
5
string mrl = @"dshow://  " ;
string optVideo = @":dshow-vdev=攝像頭設備名" ;
//string optAudio = @":dshow-adev=音頻設備名";
string size = ":dshow-size=800" ;
this .VlcControl.SourceProvider.MediaPlayer.Play(mrl, optVideo, size);

----------------------------------------------------------------------------------------------------

到此C#開發可播放攝像頭及任意格式視頻的播放器完成了。

代碼已經傳到Github上了,歡迎大家下載。

Github地址:https://github.com/kiba518/WpfVLC

----------------------------------------------------------------------------------------------------

注:此文章為原創,任何形式的轉載都請聯系作者獲得授權並注明出處!
若您覺得這篇文章還不錯,請點擊下方的推薦】,非常感謝!
本文已獨家授權給腳本之家(ID:jb51net)公眾號發布!

 

 

出處:https://www.cnblogs.com/kiba/p/11303137.html

=======================================================================

根據上面的Github地址下載的文件,解壓后里面有個   Vlc.DotNet處理過WPF停止死鎖和擴張了Public函數的代碼.7z    的文件。

解壓后有如下文件夾:

Samples   --  示例Demo

Vlc.DotNet.Core   -- 封裝vlc的核心

Vlc.DotNet.Core.Interops  --封裝vlc的相關操作

Vlc.DotNet.Forms  -- 封裝vlc的Winform程序的控件

Vlc.DotNet.Wpf  --  封裝wpf項目控件

這幾個都是重要的,看名稱應該也知道都是干什么用的了。其實還是有人不明白,我今天還是稍微加點注釋吧

好了,注意到類庫我已經介紹完了,我們看看怎么開發出來一個播放器?

上面已經有了wpf的,我這邊就用winform創建試試,首先編譯整個解決方案,保證生成正常,沒有任何錯誤。

然后在當前解決方案中創建winform的桌面程序,我的開發環境用的是vs2017,framework我直接使用的默認的 4.6.1。

在NuGit管理界面,添加vlc庫的引用,搜索VideoLAN.LibVLC.Windows,現在的最新版本是3.0.8.1,后續有什么版本不知道,我們就用最新的吧

在程序主界面,從工具箱中拖一個vlcControl控件到界面上,在vlcControl控件的事件里找到VlcLibDirectoryNeeded事件,雙擊填寫代碼,我們看看如下:

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void VlcControl1_VlcLibDirectoryNeeded(object sender, Vlc.DotNet.Forms.VlcLibDirectoryNeededEventArgs e)
        {
            var currentAssembly = Assembly.GetEntryAssembly();
            var currentDirectory = new FileInfo(currentAssembly.Location).DirectoryName;
            // Default installation path of VideoLAN.LibVLC.Windows
            var v = IntPtr.Size == 4 ? "win-x86" : "win-x64";  //自己可以在項目屬性 -> 生成 -> 平台目標,選擇x86或者x64,注意勾選下面的首選32位
            e.VlcLibDirectory = new DirectoryInfo(Path.Combine(currentDirectory, "libvlc", v));
            vlcControl1.Dock = DockStyle.Fill;

            if (!e.VlcLibDirectory.Exists)
            {
                var folderBrowserDialog = new 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);
                }
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            vlcControl1.Play(new FileInfo(@"D:\Video\big_buck_bunny_480p_surround-fix.avi"));
        }
    }

代碼很簡單,大家都能看懂,就不說明了。

很多代碼可以參考示例文件夾中的項目,或者根據自己需要去實現。

好了,現在按F5試試看吧。

 

 美化和優化的事,你就自己看着辦吧


免責聲明!

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



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