C# WPF集成VLC实现拉流显示

661 阅读1分钟

C# WPF集成VLC实现拉流显示Demo图片

image.png 1.首先Nuget安装Vlc.DotNet

image.png

2.下载VLC的依赖文件 VLC官网下载安装包,安装后,复制plugins文件夹、libvlc.dll、libvlccore.dll到工程目录中。需要注意的是安装的软件如果是64位则WPF工程运行x64版本。

image.png

3.WPF中初始化VLC

        VlcControl vlcVideo = null;

        private void InitVLC()
        {
            if (this.vlcVideo?.SourceProvider?.MediaPlayer != null)
            {
                this.vlcVideo.SourceProvider.MediaPlayer.PositionChanged -= MediaPlayer_PositionChanged;

                this.vlcVideo.SourceProvider.MediaPlayer.LengthChanged -= MediaPlayer_LengthChanged;
            }
            this.vlcVideo = new VlcControl();
            this.contentCtrl.Content = this.vlcVideo;
            var libDirectory = new DirectoryInfo(System.IO.Directory.GetCurrentDirectory() + "\\libvlc");
            this.vlcVideo.SourceProvider.CreatePlayer(libDirectory);//创建视频播放器
            this.vlcVideo.SourceProvider.MediaPlayer.PositionChanged += MediaPlayer_PositionChanged;//视频的定位移动事件
            this.vlcVideo.SourceProvider.MediaPlayer.LengthChanged += MediaPlayer_LengthChanged;//播放视频源的视频长度

        }

初始化函数完成依赖库的加载,创建VlcControl对象,绑定相关事件函数

4.配送视频流地址,实现拉流显示

string videourl = txbUrl.Text;
this.vlcVideo.SourceProvider.MediaPlayer.Play(new Uri(videourl));
this.vlcVideo.SourceProvider.MediaPlayer.Audio.Volume = 0;

5.暂停

vlcVideo.SourceProvider.MediaPlayer.Pause();

6.声音控制

this.vlcVideo.SourceProvider.MediaPlayer.Audio.Volume = (int)e.NewValue;

7.截图

string myVideos = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures);
FileInfo file = new FileInfo(myVideos + "\\" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".png");
this.vlcVideo.SourceProvider.MediaPlayer.TakeSnapshot(file);