[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解码前面会有一段预加载,
    源码

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

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值