C# vlc播放视频

一、方法方法一(winform实现)

安装VLC media player3.0,然后将安装的plugins文件复制到项目根目录,

1
1、新建一个类(VlcPlayerBase.cs):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
using System;
using System.Runtime.InteropServices;
using System.Security;
using System.Text;
using System.Threading;
 
namespace WinVlcProject
{
    using libvlc_media_t = System.IntPtr;
    using libvlc_instance_t = System.IntPtr;
    using libvlc_media_player_t = System.IntPtr;
    using static WinVlcProject.LibVlcAPI;
 
    public class VlcPlayerBase
    {
        private IntPtr libvlc_instance_;
        private IntPtr libvlc_media_player_;
 
        /// <summary>
        /// 视频时长
        /// </summary>
        private double duration_;
 
        /// <summary>
        /// VLC 播放器。
        /// </summary>
        /// <param name="pluginPath"></param>
        public VlcPlayerBase(string pluginPath)
        {
            //string pluginPath = Environment.CurrentDirectory + "\\vlc\\plugins\\";  //插件目录
            string plugin_arg = "--plugin-path=" + pluginPath;
            string[] arguments = { "-I", "dummy", "--ignore-config", "--no-video-title", plugin_arg };
            libvlc_instance_ = LibVlcAPI.libvlc_new(arguments);
 
            libvlc_media_player_ = LibVlcAPI.libvlc_media_player_new(libvlc_instance_);  //创建 libvlc_media_player 播放核心
        }
 
        /// <summary>
        /// 设置播放容器
        /// </summary>
        /// <param name="wndHandle">播放容器句柄</param>
        public void SetRenderWindow(int wndHandle)
        {
            if (libvlc_instance_ != IntPtr.Zero && wndHandle != 0)
            {
                LibVlcAPI.libvlc_media_player_set_hwnd(libvlc_media_player_, wndHandle);  //设置播放容器
            }
        }
 
        /// <summary>
        /// 播放指定媒体文件
        /// </summary>
        /// <param name="filePath"></param>
        public void LoadFile(string filePath)
        {
            IntPtr libvlc_media = LibVlcAPI.libvlc_media_new_path(libvlc_instance_, filePath);  //创建 libvlc_media_player 播放核心
            if (libvlc_media != IntPtr.Zero)
            {
                LibVlcAPI.libvlc_media_parse(libvlc_media);
                duration_ = LibVlcAPI.libvlc_media_get_duration(libvlc_media) / 1000.0;  //获取视频时长
 
                LibVlcAPI.libvlc_media_player_set_media(libvlc_media_player_, libvlc_media);  //将视频绑定到播放器去
                LibVlcAPI.libvlc_media_release(libvlc_media);
 
               // LibVlcAPI.libvlc_media_player_play(libvlc_media_player_);  //播放
            }
        }
        /// <summary>
        /// 播放指定媒体文件
        /// </summary>
        /// <param name="filePath"></param>
        public void LoadFileUrl(string filePath)
        {
            IntPtr libvlc_media = LibVlcAPI.libvlc_media_new_location(libvlc_instance_, filePath);  //创建 libvlc_media_player 播放核心
            if (libvlc_media != IntPtr.Zero)
            {
                LibVlcAPI.libvlc_media_parse(libvlc_media);
                duration_ = LibVlcAPI.libvlc_media_get_duration(libvlc_media) / 1000.0;  //获取视频时长
 
                LibVlcAPI.libvlc_media_player_set_media(libvlc_media_player_, libvlc_media);  //将视频绑定到播放器去
                LibVlcAPI.libvlc_media_release(libvlc_media);
 
                // LibVlcAPI.libvlc_media_player_play(libvlc_media_player_);  //播放
            }
        }
 
 
        /// <summary>
        /// 播放
        /// </summary>
        public void Play()
        {
            if (libvlc_media_player_ != IntPtr.Zero)
            {
                LibVlcAPI.libvlc_media_player_play(libvlc_media_player_);
            }
        }
 
        /// <summary>
        /// 暂停播放
        /// </summary>
        public void Pause()
        {
            if (libvlc_media_player_ != IntPtr.Zero)
            {
                LibVlcAPI.libvlc_media_player_pause(libvlc_media_player_);
            }
        }
 
        /// <summary>
        /// 停止播放
        /// </summary>
        public void Stop()
        {
            if (libvlc_media_player_ != IntPtr.Zero)
            {
                LibVlcAPI.libvlc_media_player_stop(libvlc_media_player_);
            }
        }
 
        public void Release()
        {
            if (libvlc_media_player_ != IntPtr.Zero)
            {
                LibVlcAPI.libvlc_media_release(libvlc_media_player_);
            }
        }
 
        /// <summary>
        /// 获取播放时间进度
        /// </summary>
        /// <returns></returns>
        public double GetPlayTime()
        {
            return LibVlcAPI.libvlc_media_player_get_time(libvlc_media_player_) / 1000.0;
        }
 
        /// <summary>
        /// 设置播放时间
        /// </summary>
        /// <param name="seekTime"></param>
        public void SetPlayTime(double seekTime)
        {
            LibVlcAPI.libvlc_media_player_set_time(libvlc_media_player_, (Int64)(seekTime * 1000));
        }
 
        /// <summary>
        /// 获取音量
        /// </summary>
        /// <returns></returns>
        public int GetVolume()
        {
            return LibVlcAPI.libvlc_audio_get_volume(libvlc_media_player_);
        }
 
        /// <summary>
        /// 设置音量
        /// </summary>
        /// <param name="volume"></param>
        public void SetVolume(int volume)
        {
            LibVlcAPI.libvlc_audio_set_volume(libvlc_media_player_, volume);
        }
 
        /// <summary>
        /// 设置是否全屏
        /// </summary>
        /// <param name="istrue"></param>
        public void SetFullScreen(bool istrue)
        {
            LibVlcAPI.libvlc_set_fullscreen(libvlc_media_player_, istrue ? 1 : 0);
        }
        public void Aspect(string aspects)
        {
            if (lmpt != IntPtr.Zero)
            {
                SafeNativeMethods.libvlc_video_set_aspect_ratio(lmpt, aspects.ToCharArray());
            }
        }
 
        /// <summary>
        /// 视频时长
        /// </summary>
        /// <returns></returns>
        public double Duration { get { return duration_; } }
 
        /// <summary>
        /// 是否正在播放
        /// </summary>
        public   bool IsPlaying
        {
            get
            {
                if (Duration > 0 && (int)GetPlayTime() == (int)Duration) this.Stop();  //如果播放完,关闭视频
 
                return (int)GetPlayTime() < (int)Duration /* 播放时间进度小于视频时长 */
                    && Duration > 0 /* 播放时间进度大于0 */
                    && GetPlayTime() > 0; /* 视频时长大于0 */
            }
        }
 
        /// <summary>
        /// 获取版本(VS2015 调试模式程序会直接崩掉)
        /// </summary>
        /// <returns></returns>
        public string Version { get { return LibVlcAPI.libvlc_get_version(); } }
        //vlc库启动参数配置 
        private static string pluginPath = System.Environment.CurrentDirectory + "\\plugins\\";
        private static string plugin_arg = "--plugin-path=" + pluginPath;
        private static string[] arguments = { "-I", "dummy", "--ignore-config", "--video-title", plugin_arg };//, program_arg }; 
        private libvlc_instance_t lit;
        private libvlc_media_player_t lmpt;
        /// <summary> 
        /// 播放网络媒体 
        /// </summary> 
        /// <param name="libvlc_instance">VLC 全局变量</param> 
        /// <param name="libvlc_media_player">VLC MediaPlayer变量</param> 
        /// <param name="url">网络视频URL,支持http、rtp、udp等格式的URL播放</param> 
        /// <returns></returns> 
        private bool NetWork_Media_Play(libvlc_instance_t libvlc_instance, libvlc_media_player_t libvlc_media_player, string url)
        {
            IntPtr pMrl = IntPtr.Zero;
            libvlc_media_t libvlc_media = IntPtr.Zero;
 
            try
            {
                if (url == null ||
                    libvlc_instance == IntPtr.Zero ||
                    libvlc_instance == null ||
                    libvlc_media_player == IntPtr.Zero ||
                    libvlc_media_player == null)
                {
                    return false;
                }
 
                pMrl = StrToIntPtr(url);
                if (pMrl == null || pMrl == IntPtr.Zero)
                {
                    return false;
                }
 
                //播放网络文件 
                libvlc_media = SafeNativeMethods.libvlc_media_new_location(libvlc_instance, pMrl);
 
                if (libvlc_media == null || libvlc_media == IntPtr.Zero)
                {
                    return false;
                }
 
                //将Media绑定到播放器上 
                SafeNativeMethods.libvlc_media_player_set_media(libvlc_media_player, libvlc_media);
 
                //释放libvlc_media资源 
                SafeNativeMethods.libvlc_media_release(libvlc_media);
                libvlc_media = IntPtr.Zero;
 
                if (0 != SafeNativeMethods.libvlc_media_player_play(libvlc_media_player))
                {
                    return false;
                }
 
                //休眠指定时间 
                Thread.Sleep(500);
 
                return true;
            }
            catch (Exception)
            {
                //释放libvlc_media资源 
                if (libvlc_media != IntPtr.Zero)
                {
                    SafeNativeMethods.libvlc_media_release(libvlc_media);
                }
                libvlc_media = IntPtr.Zero;
 
                return false;
            }
        }
        //将string []转换为IntPtr 
        private static IntPtr StrToIntPtr(string[] args)
        {
            try
            {
                IntPtr ip_args = IntPtr.Zero;
 
                PointerToArrayOfPointerHelper argv = new PointerToArrayOfPointerHelper();
                argv.pointers = new IntPtr[11];
 
                for (int i = 0; i < args.Length; i++)
                {
                    argv.pointers[i] = Marshal.StringToHGlobalAnsi(args[i]);
                }
 
                int size = Marshal.SizeOf(typeof(PointerToArrayOfPointerHelper));
                ip_args = Marshal.AllocHGlobal(size);
                Marshal.StructureToPtr(argv, ip_args, false);
 
                return ip_args;
            }
            catch (Exception)
            {
                return IntPtr.Zero;
            }
        }
 
        //将string转换为IntPtr 
        private static IntPtr StrToIntPtr(string url)
        {
            try
            {
                if (string.IsNullOrEmpty(url))
                {
                    return IntPtr.Zero;
                }
 
                IntPtr pMrl = IntPtr.Zero;
                byte[] bytes = Encoding.UTF8.GetBytes(url);
 
                pMrl = Marshal.AllocHGlobal(bytes.Length + 1);
                Marshal.Copy(bytes, 0, pMrl, bytes.Length);
                Marshal.WriteByte(pMrl, bytes.Length, 0);
 
                return pMrl;
            }
            catch (Exception)
            {
                return IntPtr.Zero;
            }
        }
 
        /// <summary>
        /// 播放网络视频流
        /// </summary>
        /// <param name="url">url地址</param>
        /// <param name="handle">显示控件句柄</param>
        /// <returns>true:播放成功;false:播放失败</returns>
        public bool playUrl(string url, IntPtr handle)
        {
            lit = Create_Media_Instance();
            lmpt = Create_MediaPlayer(lit, handle);
            //播放网络视频
            return NetWork_Media_Play(lit, lmpt, url);
            //播放本地视频
            // return Local_Media_Play(lit, lmpt, url);
        }/// <summary> 
         /// 创建VLC播放器 
         /// </summary> 
         /// <param name="libvlc_instance">VLC 全局变量</param> 
         /// <param name="handle">VLC MediaPlayer需要绑定显示的窗体句柄</param> 
         /// <returns></returns> 
        private libvlc_media_player_t Create_MediaPlayer(libvlc_instance_t libvlc_instance, IntPtr handle)
        {
            libvlc_media_player_t libvlc_media_player = IntPtr.Zero;
 
            try
            {
                if (libvlc_instance == IntPtr.Zero ||
                    libvlc_instance == null ||
                    handle == IntPtr.Zero ||
                    handle == null)
                {
                    return IntPtr.Zero;
                }
 
                //创建播放器 
                libvlc_media_player = SafeNativeMethods.libvlc_media_player_new(libvlc_instance);
                if (libvlc_media_player == null || libvlc_media_player == IntPtr.Zero)
                {
                    return IntPtr.Zero;
                }
 
                //设置播放窗口             
                SafeNativeMethods.libvlc_media_player_set_hwnd(libvlc_media_player, (int)handle);
 
                return libvlc_media_player;
            }
            catch
            {
                SafeNativeMethods.libvlc_media_player_release(libvlc_media_player);
 
                return IntPtr.Zero;
            }
        }
        #region 导入库函数
        [SuppressUnmanagedCodeSecurity]
        internal static class SafeNativeMethods
        {
            // 创建一个libvlc实例,它是引用计数的 
            [DllImport(@"libvlc.DLL", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
            internal static extern libvlc_instance_t libvlc_new(int argc, IntPtr argv);
 
            // 释放libvlc实例 
            [DllImport(@"libvlc.DLL", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
            internal static extern void libvlc_release(libvlc_instance_t libvlc_instance);
 
            //获取libvlc的版本 
            [DllImport(@"libvlc.DLL", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
            internal static extern String libvlc_get_version();
 
            //从视频来源(例如http、rtsp)构建一个libvlc_meida 
            [DllImport(@"libvlc.DLL", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
            internal static extern libvlc_media_t libvlc_media_new_location(libvlc_instance_t libvlc_instance, IntPtr path);
 
            //从本地文件路径构建一个libvlc_media 
            [DllImport(@"libvlc.DLL", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
            internal static extern libvlc_media_t libvlc_media_new_path(libvlc_instance_t libvlc_instance, IntPtr path);
 
            //释放libvlc_media 
            [DllImport(@"libvlc.DLL", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
            internal static extern void libvlc_media_release(libvlc_media_t libvlc_media_inst);
 
            // 创建一个空的播放器 
            [DllImport(@"libvlc.DLL", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
            internal static extern libvlc_media_player_t libvlc_media_player_new(libvlc_instance_t libvlc_instance);
 
            //从libvlc_media构建播放器 
            [DllImport(@"libvlc.DLL", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
            internal static extern libvlc_media_player_t libvlc_media_player_new_from_media(libvlc_media_t libvlc_media);
 
            //释放播放器资源 
            [DllImport(@"libvlc.DLL", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
            internal static extern void libvlc_media_player_release(libvlc_media_player_t libvlc_mediaplayer);
 
            // 将视频(libvlc_media)绑定到播放器上 
            [DllImport(@"libvlc.DLL", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
            internal static extern void libvlc_media_player_set_media(libvlc_media_player_t libvlc_media_player, libvlc_media_t libvlc_media);
 
            // 设置图像输出的窗口 
            [DllImport(@"libvlc.DLL", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
            internal static extern void libvlc_media_player_set_hwnd(libvlc_media_player_t libvlc_mediaplayer, Int32 drawable);
 
            //播放器播放 
            [DllImport(@"libvlc.DLL", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
            internal static extern int libvlc_media_player_play(libvlc_media_player_t libvlc_mediaplayer);
 
            //播放器暂停 
            [DllImport(@"libvlc.DLL", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
            internal static extern void libvlc_media_player_pause(libvlc_media_player_t libvlc_mediaplayer);
 
            //播放器停止 
            [DllImport(@"libvlc.DLL", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
            internal static extern void libvlc_media_player_stop(libvlc_media_player_t libvlc_mediaplayer);
 
            // 解析视频资源的媒体信息(如时长等) 
            [DllImport(@"libvlc.DLL", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
            internal static extern void libvlc_media_parse(libvlc_media_t libvlc_media);
 
            // 返回视频的时长(必须先调用libvlc_media_parse之后,该函数才会生效) 
            [DllImport(@"libvlc.DLL", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
            internal static extern Int64 libvlc_media_get_duration(libvlc_media_t libvlc_media);
 
            // 当前播放时间 
            [DllImport(@"libvlc.DLL", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
            internal static extern Int64 libvlc_media_player_get_time(libvlc_media_player_t libvlc_mediaplayer);
 
            // 设置播放时间 
            [DllImport(@"libvlc.DLL", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
            internal static extern void libvlc_media_player_set_time(libvlc_media_player_t libvlc_mediaplayer, Int64 time);
 
            // 获取音量 
            [DllImport(@"libvlc.DLL", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
            internal static extern int libvlc_audio_get_volume(libvlc_media_player_t libvlc_media_player);
 
            //设置音量 
            [DllImport(@"libvlc.DLL", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
            internal static extern void libvlc_audio_set_volume(libvlc_media_player_t libvlc_media_player, int volume);
 
            // 设置全屏 
            [DllImport(@"libvlc.DLL", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
            internal static extern void libvlc_set_fullscreen(libvlc_media_player_t libvlc_media_player, int isFullScreen);
 
            [DllImport(@"libvlc.DLL", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
            internal static extern int libvlc_get_fullscreen(libvlc_media_player_t libvlc_media_player);
 
            [DllImport(@"libvlc.DLL", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
            internal static extern void libvlc_toggle_fullscreen(libvlc_media_player_t libvlc_media_player);
 
            //判断播放时是否在播放 
            [DllImport(@"libvlc.DLL", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
            internal static extern bool libvlc_media_player_is_playing(libvlc_media_player_t libvlc_media_player);
 
            //判断播放时是否能够Seek 
            [DllImport(@"libvlc.DLL", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
            internal static extern bool libvlc_media_player_is_seekable(libvlc_media_player_t libvlc_media_player);
 
            //判断播放时是否能够Pause 
            [DllImport(@"libvlc.DLL", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
            internal static extern bool libvlc_media_player_can_pause(libvlc_media_player_t libvlc_media_player);
 
            //判断播放器是否可以播放 
            [DllImport(@"libvlc.DLL", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
            internal static extern int libvlc_media_player_will_play(libvlc_media_player_t libvlc_media_player);
 
            //进行快照 
            [DllImport(@"libvlc.DLL", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
            internal static extern int libvlc_video_take_snapshot(libvlc_media_player_t libvlc_media_player, int num, char[] filepath, int i_width, int i_height);
 
            //获取Media信息 
            [DllImport(@"libvlc.DLL", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
            internal static extern libvlc_media_t libvlc_media_player_get_media(libvlc_media_player_t libvlc_media_player);
 
            //获取媒体信息 
            [DllImport(@"libvlc.DLL", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
            internal static extern int libvlc_media_get_stats(libvlc_media_t libvlc_media, ref libvlc_media_stats_t lib_vlc_media_stats);
            [DllImport(@"libvlc.DLL", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
            internal static extern void libvlc_video_set_aspect_ratio(libvlc_media_player_t libvlc_media_player, char[] aspect);
        }
        #endregion
        #region 结构体
        public struct libvlc_media_stats_t
        {
            /* Input */
            public int i_read_bytes;
            public float f_input_bitrate;
 
            /* Demux */
            public int i_demux_read_bytes;
            public float f_demux_bitrate;
            public int i_demux_corrupted;
            public int i_demux_discontinuity;
 
            /* Decoders */
            public int i_decoded_video;
            public int i_decoded_audio;
 
            /* Video Output */
            public int i_displayed_pictures;
            public int i_lost_pictures;
 
            /* Audio output */
            public int i_played_abuffers;
            public int i_lost_abuffers;
 
            /* Stream output */
            public int i_sent_packets;
            public int i_sent_bytes;
            public float f_send_bitrate;
        }
        #endregion
       
        /// <summary> 
        /// 创建VLC播放资源索引 
        /// </summary> 
        /// <param name="arguments"></param> 
        /// <returns></returns> 
        private libvlc_instance_t Create_Media_Instance()
        {
            libvlc_instance_t libvlc_instance = IntPtr.Zero;
            IntPtr argvPtr = IntPtr.Zero;
 
            try
            {
                if (arguments.Length == 0 ||
                    arguments == null)
                {
                    return IntPtr.Zero;
                }
 
                //将string数组转换为指针 
                argvPtr = StrToIntPtr(arguments);
                if (argvPtr == null || argvPtr == IntPtr.Zero)
                {
                    return IntPtr.Zero;
                }
 
                //设置启动参数 
                libvlc_instance = SafeNativeMethods.libvlc_new(arguments.Length, argvPtr);
                if (libvlc_instance == null || libvlc_instance == IntPtr.Zero)
                {
                    return IntPtr.Zero;
                }
 
                return libvlc_instance;
            }
            catch
            {
                return IntPtr.Zero;
            }
        }
 
 
    }
 
    #region vlclib.dll
 
    internal static class LibVlcAPI
    {
        internal struct PointerToArrayOfPointerHelper
        {
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 11)]
            public IntPtr[] pointers;
        }
 
        /// <summary>
        /// 传入播放参数
        /// </summary>
        /// <param name="arguments"></param>
        /// <returns></returns>
        public static IntPtr libvlc_new(string[] arguments)
        {
            PointerToArrayOfPointerHelper argv = new PointerToArrayOfPointerHelper();
            argv.pointers = new IntPtr[11];
 
            for (int i = 0; i < arguments.Length; i++)
            {
                argv.pointers[i] = Marshal.StringToHGlobalAnsi(arguments[i]);  //将托管 System.String 中的内容复制到非托管内存,并在复制时转换为 ANSI 格式。
            }
 
            IntPtr argvPtr = IntPtr.Zero;
            try
            {
                int size = Marshal.SizeOf(typeof(PointerToArrayOfPointerHelper));  //返回非托管类型的大小(以字节为单位)。
                argvPtr = Marshal.AllocHGlobal(size);  //从进程的非托管内存中分配内存。
                Marshal.StructureToPtr(argv, argvPtr, false);  //将数据从托管对象封送到非托管内存块。
 
                return libvlc_new(arguments.Length, argvPtr);  //创建一个libvlc实例,它是引用计数的
            }
            finally
            {
                for (int i = 0; i < arguments.Length + 1; i++)
                {
                    if (argv.pointers[i] != IntPtr.Zero)
                    {
                        Marshal.FreeHGlobal(argv.pointers[i]);  //释放以前使用 System.Runtime.InteropServices.Marshal.AllocHGlobal(System.IntPtr) 从进程的非托管内存中分配的内存。
                    }
                }
 
                if (argvPtr != IntPtr.Zero) { Marshal.FreeHGlobal(argvPtr);/* 释放以前使用 System.Runtime.InteropServices.Marshal.AllocHGlobal(System.IntPtr) 从进程的非托管内存中分配的内存。 */ }
            }
        }
 
        /// <summary>
        /// 从本地文件系统路径新建,其他参照上一条
        /// </summary>
        /// <param name="libvlc_instance"></param>
        /// <param name="path"></param>
        /// <returns></returns>
        public static IntPtr libvlc_media_new_path(IntPtr libvlc_instance, string path)
        {
            IntPtr pMrl = IntPtr.Zero;
            try
            {
                byte[] bytes = Encoding.UTF8.GetBytes(path);
                pMrl = Marshal.AllocHGlobal(bytes.Length + 1);
                Marshal.Copy(bytes, 0, pMrl, bytes.Length);
                Marshal.WriteByte(pMrl, bytes.Length, 0);
                return libvlc_media_new_path(libvlc_instance, pMrl);  // 从本地文件路径构建一个libvlc_media
            }
            finally
            {
                if (pMrl != IntPtr.Zero) { Marshal.FreeHGlobal(pMrl);/* 释放以前使用 System.Runtime.InteropServices.Marshal.AllocHGlobal(System.IntPtr) 从进程的非托管内存中分配的内存。 */                }
            }
        }
 
        /// <summary>
        /// 使用一个给定的媒体资源路径来建立一个libvlc_media对象.参数psz_mrl为要读取的MRL(Media Resource Location).此函数返回新建的对象或NULL.
        /// </summary>
        /// <param name="libvlc_instance"></param>
        /// <param name="path"></param>
        /// <returns></returns>
        public static IntPtr libvlc_media_new_location(IntPtr libvlc_instance, string path)
        {
            IntPtr pMrl = IntPtr.Zero;
            try
            {
                byte[] bytes = Encoding.UTF8.GetBytes(path);
                pMrl = Marshal.AllocHGlobal(bytes.Length + 1);
                Marshal.Copy(bytes, 0, pMrl, bytes.Length);
                Marshal.WriteByte(pMrl, bytes.Length, 0);
                return libvlc_media_new_path(libvlc_instance, pMrl);  // 从本地文件路径构建一个libvlc_media
            }
            finally
            {
                if (pMrl != IntPtr.Zero) { Marshal.FreeHGlobal(pMrl);/* 释放以前使用 System.Runtime.InteropServices.Marshal.AllocHGlobal(System.IntPtr) 从进程的非托管内存中分配的内存。 */                }
            }
        }
 
        // ----------------------------------------------------------------------------------------
        // 以下是libvlc.dll导出函数
 
        /// <summary>
        /// 创建一个libvlc实例,它是引用计数的
        /// </summary>
        /// <param name="argc"></param>
        /// <param name="argv"></param>
        /// <returns></returns>
        [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        [SuppressUnmanagedCodeSecurity]
        private static extern IntPtr libvlc_new(int argc, IntPtr argv);
 
        /// <summary>
        /// 释放libvlc实例
        /// </summary>
        /// <param name="libvlc_instance"></param>
        [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        [SuppressUnmanagedCodeSecurity]
        public static extern void libvlc_release(IntPtr libvlc_instance);
 
        /// <summary>
        /// 获取版本
        /// </summary>
        /// <returns></returns>
        [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        [SuppressUnmanagedCodeSecurity]
        public static extern String libvlc_get_version();
 
        /// <summary>
        /// 从视频来源(例如Url)构建一个libvlc_meida
        /// </summary>
        /// <param name="libvlc_instance"></param>
        /// <param name="path"></param>
        /// <returns></returns>
        [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        [SuppressUnmanagedCodeSecurity]
        private static extern IntPtr libvlc_media_new_location(IntPtr libvlc_instance, IntPtr path);
 
        /// <summary>
        /// 从本地文件路径构建一个libvlc_media
        /// </summary>
        /// <param name="libvlc_instance"></param>
        /// <param name="path"></param>
        /// <returns></returns>
        [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        [SuppressUnmanagedCodeSecurity]
        private static extern IntPtr libvlc_media_new_path(IntPtr libvlc_instance, IntPtr path);
 
        /// <summary>
        ///
        /// </summary>
        /// <param name="libvlc_media_inst"></param>
        [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        [SuppressUnmanagedCodeSecurity]
        public static extern void libvlc_media_release(IntPtr libvlc_media_inst);
 
        /// <summary>
        /// 创建libvlc_media_player(播放核心)
        /// </summary>
        /// <param name="libvlc_instance"></param>
        /// <returns></returns>
        [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        [SuppressUnmanagedCodeSecurity]
        public static extern IntPtr libvlc_media_player_new(IntPtr libvlc_instance);
 
        /// <summary>
        /// 将视频(libvlc_media)绑定到播放器上
        /// </summary>
        /// <param name="libvlc_media_player"></param>
        /// <param name="libvlc_media"></param>
        [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        [SuppressUnmanagedCodeSecurity]
        public static extern void libvlc_media_player_set_media(IntPtr libvlc_media_player, IntPtr libvlc_media);
 
        /// <summary>
        /// 设置图像输出的窗口
        /// </summary>
        /// <param name="libvlc_mediaplayer"></param>
        /// <param name="drawable"></param>
        [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        [SuppressUnmanagedCodeSecurity]
        public static extern void libvlc_media_player_set_hwnd(IntPtr libvlc_mediaplayer, Int32 drawable);
 
        #region 播放控制
        /// <summary>
        /// 播放
        /// </summary>
        /// <param name="libvlc_mediaplayer"></param>
        [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        [SuppressUnmanagedCodeSecurity]
        public static extern void libvlc_media_player_play(IntPtr libvlc_mediaplayer);
 
        /// <summary>
        /// 暂停
        /// </summary>
        /// <param name="libvlc_mediaplayer"></param>
        [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        [SuppressUnmanagedCodeSecurity]
        public static extern void libvlc_media_player_pause(IntPtr libvlc_mediaplayer);
 
        /// <summary>
        /// 停止
        /// </summary>
        /// <param name="libvlc_mediaplayer"></param>
        [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        [SuppressUnmanagedCodeSecurity]
        public static extern void libvlc_media_player_stop(IntPtr libvlc_mediaplayer);
        #endregion
 
        /// <summary>
        /// 释放播放文件
        /// </summary>
        /// <param name="libvlc_mediaplayer"></param>
        [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        [SuppressUnmanagedCodeSecurity]
        public static extern void libvlc_media_player_release(IntPtr libvlc_mediaplayer);
 
        /// <summary>
        /// 解析视频资源的媒体信息(如时长等)
        /// </summary>
        /// <param name="libvlc_media"></param>
        [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        [SuppressUnmanagedCodeSecurity]
        public static extern void libvlc_media_parse(IntPtr libvlc_media);
 
        /// <summary>
        /// 返回视频的时长(必须先调用libvlc_media_parse之后,该函数才会生效)
        /// </summary>
        /// <param name="libvlc_media"></param>
        /// <returns></returns>
        [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        [SuppressUnmanagedCodeSecurity]
        public static extern Int64 libvlc_media_get_duration(IntPtr libvlc_media);
 
 
        #region 播放时间进度
 
        /// <summary>
        /// 当前播放的时间进度
        /// </summary>
        /// <param name="libvlc_mediaplayer"></param>
        /// <returns></returns>
        [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        [SuppressUnmanagedCodeSecurity]
        public static extern Int64 libvlc_media_player_get_time(IntPtr libvlc_mediaplayer);
 
        /// <summary>
        /// 设置播放位置(拖动)
        /// </summary>
        /// <param name="libvlc_mediaplayer"></param>
        /// <param name="time"></param>
        [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        [SuppressUnmanagedCodeSecurity]
        public static extern void libvlc_media_player_set_time(IntPtr libvlc_mediaplayer, Int64 time);
 
        #endregion
 
        #region 音量
 
        /// <summary>
        /// 获取音量
        /// </summary>
        /// <param name="libvlc_media_player"></param>
        /// <returns></returns>
        [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        [SuppressUnmanagedCodeSecurity]
        public static extern int libvlc_audio_get_volume(IntPtr libvlc_media_player);
 
        /// <summary>
        /// 设置音量
        /// </summary>
        /// <param name="libvlc_media_player"></param>
        /// <param name="volume"></param>
        [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        [SuppressUnmanagedCodeSecurity]
        public static extern void libvlc_audio_set_volume(IntPtr libvlc_media_player, int volume);
 
        #endregion
 
        /// <summary>
        /// 设置全屏
        /// </summary>
        /// <param name="libvlc_media_player"></param>
        /// <param name="isFullScreen"></param>
        [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        [SuppressUnmanagedCodeSecurity]
        public static extern void libvlc_set_fullscreen(IntPtr libvlc_media_player, int isFullScreen);
 
        /// <summary>
        /// 获取播放状态。(Win10 不支持)
        /// </summary>
        /// <param name="libvlc_mediaplayer"></param>
        /// <returns></returns>
        //[DllImport("libvlc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        //[SuppressUnmanagedCodeSecurity]
        //public static extern Int64 libvlc_media_player_get_state(IntPtr libvlc_mediaplayer);
 
    }
 
    #endregion
 
}

  2、新建一个form窗体,在窗体上拖一个panel1控件,然后输入下面代码即可

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
 
namespace WinVlcProject
{
    public partial class Form2 : Form
    {
        VlcPlayerBase player = null;
        public Form2()
        {
            InitializeComponent();
        }
 
        private void Form2_Load(object sender, EventArgs e)
        {
            try
            {
                string p = System.Environment.CurrentDirectory + "\\plugins\\";
                player = new VlcPlayerBase(p);
                player.SetRenderWindow((int)panel1.Handle);
                VlcPlayerBase v = new VlcPlayerBase(p);
                v.playUrl("http://222.128.103.58:21909/system-demo/testvideo.mpg", this.panel1.Handle);// 网络路径播放 这里的这个panel是winform窗体的一个panel的控件
                // v.Aspect("1:1");//这个是设置屏幕比的`
                this.player.SetFullScreen(true);
                this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;//隐藏边框
                this.WindowState = System.Windows.Forms.FormWindowState.Maximized; //实现窗体全屏显示
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    
    }
}

 方法方法二(WPF实现)

1、先安装VLC media player3.0,将安装的VLC文件夹整体复制到项目根目录下:

2、然后界面添加VlcControl控件,如下图,代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
<Window x:Class="WpfApp1.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:WpfApp1"
          xmlns:vlc="clr-namespace:Vlc.DotNet.Wpf;assembly=Vlc.DotNet.Wpf"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <vlc:VlcControl x:Name="vlcControl"/>
    </Grid>
</Window>

 3.然后进入窗体.cs文件,代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using WpfApp1.Utils;
 
namespace WpfApp1
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        private readonly IniUtil iniUtil = new IniUtil(AppDomain.CurrentDomain.BaseDirectory + "\\config.ini");
        //获取输出目录
        private static string appPath = AppDomain.CurrentDomain.BaseDirectory;
        //vlc文件的目录
        private DirectoryInfo vlcLibDirectory = new DirectoryInfo(appPath + @"VLC");
        //配置项
        string[] options = new string[]
            {
 
            };
 
        public MainWindow()
        {
           // Width = iniUtil.getInt("FormSetting", "Width", 0);
            InitializeComponent();
 
            #region 设置界面全屏显示
            this.WindowState = System.Windows.WindowState.Normal;
            this.WindowStyle = System.Windows.WindowStyle.None;
            this.ResizeMode = System.Windows.ResizeMode.NoResize;
            this.Topmost = true;
            this.Left = 0.0;
            this.Top = 0.0;
            this.Width = System.Windows.SystemParameters.PrimaryScreenWidth;
            this.Height = System.Windows.SystemParameters.PrimaryScreenHeight;
            #endregion
 
            //创建播放器
            this.vlcControl.SourceProvider.CreatePlayer(vlcLibDirectory, options);
            //http协议视频流
            this.vlcControl.SourceProvider.MediaPlayer.Play(new Uri("http://222.128.103.58:21909/system-demo/testvideo.mpg"));
            LogHelper.WriteLog(GetType(), "MainWindow播放视频");
            //rtmp协议
            //this.vlcControl.SourceProvider.MediaPlayer.Play(new Uri("rtmp://58.200.131.2:1935/livetv/hunantv"));
            //本地视频
            //  this.vlcControl.SourceProvider.MediaPlayer.Play(new Uri("C:\\Users\\Administrator\\Desktop\\新建文件夹 (4)\\QQ录屏20230103141518.mp4"));
            //添加播放结束事件
            this.vlcControl.SourceProvider.MediaPlayer.EndReached += MediaPlayerEndEvent;
        }
        //添加播放结束事件
        private void MediaPlayerEndEvent(object sender, Vlc.DotNet.Core.VlcMediaPlayerEndReachedEventArgs e)
        {
            LogHelper.WriteLog(GetType(), "进入MediaPlayerEndEvent方法");
            Task.Factory.StartNew(() =>
            {
                 
                this.vlcControl.Dispose();
                Application.Current.Dispatcher.BeginInvoke(new Action(() =>
                {
                    this.vlcControl.SourceProvider.CreatePlayer(vlcLibDirectory);
                    this.vlcControl.SourceProvider.MediaPlayer.Play(new Uri("http://222.128.103.58:21909/system-demo/testvideo.mpg"));//http协议视频流
                    LogHelper.WriteLog(GetType(), "MediaPlayerEndEvent重新加载视频");
                    //this.vlcControl.SourceProvider.MediaPlayer.Play(new Uri("C:\\Users\\Administrator\\Desktop\\新建文件夹 (4)\\QQ录屏20230103141518.mp4"));//本地视频
                    this.vlcControl.SourceProvider.MediaPlayer.EndReached += MediaPlayerEndEvent;
                }));
            });
        }
    }
}

  

WPF参考地址:https://www.cnblogs.com/rsj1767/p/11955600.html

Winform参考地址:https://blog.csdn.net/qq_38826949/article/details/98749193?utm_medium=distribute.pc_relevant.none-task-blog-2~default~baidujs_baidulandingword~default-0-98749193-blog-127024637.pc_relevant_3mothn_strategy_and_data_recovery&spm=1001.2101.3001.4242.1&utm_relevant_index=3

 

posted @   fulllove  阅读(496)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
点击右上角即可分享
微信分享提示