The right to learn WPF slider controls (Slider) in C # developers can play any player, camera and video formats WPF dependency property of a custom style

Foreword

This paper describes, in WPF, with Vlc.DotNet call VLC library for video broadcast functions, let's first do the preparatory work before development.

Ready to work

First, we create a project WpfVLC, then enter Neget search Vlc.DotNet, get the following screen:

We chose Vlc.DotNet.Wpf, click Install (here I have installed, uninstall it as shown in the figure).

Then, we went to VLC official website, download VLC player.

VLC official website: http: //www.videolan.org/

Because my computer is 64, so I downloaded the 64-bit version of VLC, as shown below:

 Once downloaded, install it properly, download files shots are as follows:

After installation is complete, we find the specific location of the installation and open, as shown below:

Within the folder we find the file libvlc.dll, libvlccore.dll and folders plugins, and then copy them out.

We now return to the project WpfVLC we just created, enter the file directory, open the debug folder, then we create a folder libvlc in its directory, as follows:

Then, create a folder win-x64 in liblic, as follows:

And then, we'll just copy the three files vlc, into this folder, as follows:

This, our preparation work is done, now start coding.

Use Vlc.DotNet play video

Now, we enter code development projects.

First, we set the project 64 project, because we are using the VLC 64.

Then, we open the MainWindow page.

Join the local page namespace introduced Vlc.DotNet namespace.

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

Next, we add in the page layout VlcControl controls and open files, play and stop buttons, as follows:

<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文件的代码,如下:

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控制播放进度的方法很简单,如下:

 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会停止
 }

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

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的地址即可,如下:

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

播放摄像头

播放摄像头在这里也很简单,只是Play的入参稍微要注意一下即可,如下:

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

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

注:此文章为原创,任何形式的转载都请联系作者获得授权并注明出处!
若您觉得这篇文章还不错,请点击下方的推荐】,非常感谢!

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

 

Guess you like

Origin www.cnblogs.com/kiba/p/11303137.html