[RTSP]WPF用VLC显示RTSP视频


title: ‘[RTSP]WPF用VLC显示RTSP视频’
date: 2017-11-30 22:48:38
tags:

WPF用VLC显示RTSP视频

场景

  • vlc是一个开源的解码库,有很多的版本当然也有WPF的版本,但是WPF其实是WinForm的基础上实现的。所有控件不自己处理的话,一直处于顶层。但它可以自带解码和显示的功能

操作

  • 下载对VLC播放器,找到安装目录把libvlc.dll,libvlccore.dll,和plugins文件夹拿出来,插件看自己的需要,用多少加多少。一般方便管理我们都会在工程目录下新建一个文件夹如libvlc_x64,32位的可以x86。
  • 之后我们引用这些dll
    这里写图片描述
  1. 我们在xaml中添加VlcControl,代码如下
<Window x:Class="TestVideo.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:TestVideo"
        xmlns:wpf="clr-namespace:Vlc.DotNet.Wpf;assembly=Vlc.DotNet.Wpf"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>

        <wpf:VlcControl Grid.Row="0" x:Name="myControl"/>

        <Button Grid.Row="1" Click="OnPlayButtonClick">Play</Button>
        <Button Grid.Row="2" Click="OnForwardButtonClick" x:Name="Forward">Forward</Button>
        <Button Grid.Row="3" Click="GetLength_Click" x:Name="GetLength">Get Length</Button>
        <Button Grid.Row="4" Click="GetCurrentTime_Click" x:Name="GetCurrentTime">Get Current Time</Button>
        <Button Grid.Row="5" Click="SetCurrentTime_Click" x:Name="SetCurrentTime">Set Current Time to 5s</Button>
    </Grid>
</Window>
  1. 在播放前要指定引用插件和库所在的路径,我们在load里面,自己的工程不要写在这,会影响性成的。
  // Use 64 bits library
            this.myControl.MediaPlayer.VlcLibDirectory = new DirectoryInfo(System.IO.Path.Combine(Environment.CurrentDirectory, "libvlc_x64"));
            var options = new string[]
{
 // VLC options can be given here. Please refer to the VLC command line documentation.
};
            this.myControl.MediaPlayer.VlcMediaplayerOptions = options;

            // Load libvlc libraries and initializes stuff. It is important that the options (if you want to pass any) and lib directory are given before calling this method.
            this.myControl.MediaPlayer.EndInit();
  1. 我们可以做的操作就很多了。如下

        private void OnPlayButtonClick(object sender, RoutedEventArgs e)
        {
              myControl.MediaPlayer.Play(new Uri("rtsp://184.72.239.149/vod/mp4://BigBuckBunny_175k.mov"));
         //本地视频写写本地路径
        }

        private void OnForwardButtonClick(object sender, RoutedEventArgs e)
        {
            myControl.MediaPlayer.Rate = 2;
        }

        private void GetLength_Click(object sender, RoutedEventArgs e)
        {
            GetLength.Content = myControl.MediaPlayer.Length + " ms";
        }

        private void GetCurrentTime_Click(object sender, RoutedEventArgs e)
        {
            GetCurrentTime.Content = myControl.MediaPlayer.Time + " ms";
        }

        private void SetCurrentTime_Click(object sender, RoutedEventArgs e)
        {
            myControl.MediaPlayer.Time = 5000;
            SetCurrentTime.Content = myControl.MediaPlayer.Time + " ms";
        }
  1. 效果如下
    这里写图片描述

提示

  • 就是视频的黑框,所以大小自己要处理好
  • wpf控件其实是winform来实现的
  • vlc解码前面会有一段预加载,
    源码

所有的东西都有自己的优点和缺点,不要拿短来比长。做界面做样式只是一个模块,要有整体把握,不然软件或者网站都是问题,代码维护也是问题。

©️2020 CSDN 皮肤主题: 像素格子 设计师:CSDN官方博客 返回首页