WPF使用VLC播放本地和流媒体视频 循环播放

1、创建wpf项目

 

 

 2、使用Nuget安装vlc,选择vlc.docnet.wpf

 

 3、xaml中添加vlc

 

 4、编写cs代码

 

 trsp协议的视频源 公开的不好找,网上有的基本没法使用,可以自己找个视频用VLC软件输出一个rtsp协议的视频流进行测试。

5、注意事项

首先 需要安装VLC,然后将VLC整体拷贝到项目的输出目录,比如debug目录下;

 

 

 

 

另外,下载安装时,一定要注意VLC的版本,如果你的项目是默认anycpu,那么就需要安装32位的VLC,

如果指定输出64位,那么久安装64位的VLC

 

 最后源码

MainWindow.xaml

 1 <Window x:Class="wpfVlcDemo.MainWindow"
 2         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 3         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 4         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
 5         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
 6         xmlns:local="clr-namespace:wpfVlcDemo"
 7         xmlns:vlc="clr-namespace:Vlc.DotNet.Wpf;assembly=Vlc.DotNet.Wpf"
 8         mc:Ignorable="d"
 9         Title="MainWindow" Height="450" Width="800">
10     <Grid>
11         <vlc:VlcControl x:Name="vlcControl"></vlc:VlcControl>
12     </Grid>
13 </Window>
View Code

MainWindow.xaml.cs

 1 using System;
 2 using System.Collections.Generic;
 3 using System.IO;
 4 using System.Linq;
 5 using System.Text;
 6 using System.Threading.Tasks;
 7 using System.Windows;
 8 using System.Windows.Controls;
 9 using System.Windows.Data;
10 using System.Windows.Documents;
11 using System.Windows.Input;
12 using System.Windows.Media;
13 using System.Windows.Media.Imaging;
14 using System.Windows.Navigation;
15 using System.Windows.Shapes;
16 
17 namespace wpfVlcDemo
18 {
19     /// <summary>
20     /// MainWindow.xaml 的交互逻辑
21     /// </summary>
22     public partial class MainWindow : Window
23     {
24         //获取输出目录
25         private static string appPath = AppDomain.CurrentDomain.BaseDirectory;
26         //vlc文件的地址
27         private DirectoryInfo vlcLibDirectory = new DirectoryInfo(appPath + @"VLC");
28         //配置项
29         string[] options = new string[]
30         {
31             // VLC options can be given here. Please refer to the VLC command line documentation.
32         };
33         public MainWindow()
34         {
35             InitializeComponent();
36             //创建播放器
37             this.vlcControl.SourceProvider.CreatePlayer(vlcLibDirectory, options);
38             //http协议视频流
39             //this.vlcControl.SourceProvider.MediaPlayer.Play(new Uri("http://ivi.bupt.edu.cn/hls/cctv1hd.m3u8"));
40             //rtmp协议
41             //this.vlcControl.SourceProvider.MediaPlayer.Play(new Uri("rtmp://58.200.131.2:1935/livetv/hunantv"));
42             //本地视频
43             this.vlcControl.SourceProvider.MediaPlayer.Play(new Uri("d:/1.avi"));
44             //添加播放结束事件
45             //其他事件 请自行查看
46             this.vlcControl.SourceProvider.MediaPlayer.EndReached += MediaPlayerEndEvent;
47 
48         }
49         private void MediaPlayerEndEvent(object sender,Vlc.DotNet.Core.VlcMediaPlayerEndReachedEventArgs e)
50         {
51             //播放结束,循环播放本地视频
52             Task.Factory.StartNew(() => {
53                 this.vlcControl.Dispose();
54                 Application.Current.Dispatcher.BeginInvoke(new Action(() => {
55                     this.vlcControl.SourceProvider.CreatePlayer(vlcLibDirectory);
56                     this.vlcControl.SourceProvider.MediaPlayer.Play(new Uri("d:/1.avi"));
57                     this.vlcControl.SourceProvider.MediaPlayer.EndReached += MediaPlayerEndEvent;
58                 }));
59             });
60         }
61     }
62 }
View Code
posted @ 2019-11-29 08:48  阿发博客  阅读(3600)  评论(2编辑  收藏  举报