为了将多个视频放在一个窗口,最开始想用的是windows media player ,6个视频,把整个电脑卡得不动了(显卡太弱,是多输出口的,没法换),于是又想把视频压缩成一个,网上的大部分软件要收费,还是研究播放,就弄了VLC。

代码网上基本都一样的,但我调试了很久才走通。

主要是在调用,以及库的问题上,总结成3点,

一、关于库的问题,不要纠结,直接在网上搜索VLC media player 下载,国内站的也可以,(官网有可能要翻墙)

安装后,可以看到三个需要的,plugins目录 ,libvlc.dll,libvlccore.dll ,放到你的debug目录

二、关于调用

string p = System.Environment.CurrentDirectory + "\\plugins\\";
VlcPlayer v = new VlcPlayer(p);
v.SetRenderWindow(pictureBox1.Handle.ToInt32());
v.PlayFile(@"1.mp4");

类是下面代码封装好的,大概就是这样

三、如果出现其它一些状况,请新建一个工程,不要在原工程上纠结。

  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. using System.Runtime.InteropServices;
  5. using System.Security;
  6. using System.Text;
  7.  
  8. namespace WindowsFormsApplication2
  9. {
  10. class VlcPlayer
  11. {
  12. private IntPtr libvlc_instance_;
  13. private IntPtr libvlc_media_player_;
  14. private double duration_;
  15. public VlcPlayer(string pluginPath)
  16. {
  17. string plugin_arg = "--plugin-path=" + pluginPath;
  18. string[] arguments = { "-I", "dummy", "--ignore-config", "--no-video-title", plugin_arg };
  19. libvlc_instance_ = LibVlcAPI.libvlc_new(arguments);
  20.  
  21. libvlc_media_player_ = LibVlcAPI.libvlc_media_player_new(libvlc_instance_);
  22. }
  23. public void SetRenderWindow(int wndHandle)
  24. {
  25. if (libvlc_instance_ != IntPtr.Zero && wndHandle != )
  26. {
  27. LibVlcAPI.libvlc_media_player_set_hwnd(libvlc_media_player_, wndHandle);
  28. }
  29. }
  30. public void PlayFile(string filePath)
  31. {
  32. IntPtr libvlc_media = LibVlcAPI.libvlc_media_new_path(libvlc_instance_, filePath);
  33. if (libvlc_media != IntPtr.Zero)
  34. {
  35. LibVlcAPI.libvlc_media_parse(libvlc_media);
  36. duration_ = LibVlcAPI.libvlc_media_get_duration(libvlc_media) / 1000.0;
  37. LibVlcAPI.libvlc_media_player_set_media(libvlc_media_player_, libvlc_media);
  38. LibVlcAPI.libvlc_media_release(libvlc_media);
  39. LibVlcAPI.libvlc_media_player_play(libvlc_media_player_);
  40. }
  41. }
  42. public void Pause()
  43. {
  44. if (libvlc_media_player_ != IntPtr.Zero)
  45. {
  46. LibVlcAPI.libvlc_media_player_pause(libvlc_media_player_);
  47. }
  48. }
  49. public void Play()
  50. {
  51. if (libvlc_media_player_ != IntPtr.Zero)
  52. {
  53. LibVlcAPI.libvlc_media_player_play(libvlc_media_player_);
  54. // LibVlcAPI.libvlc_media_player_pause(libvlc_media_player_);
  55. }
  56. }
  57. public void Stop()
  58. {
  59. if (libvlc_media_player_ != IntPtr.Zero)
  60. {
  61. LibVlcAPI.libvlc_media_player_stop(libvlc_media_player_);
  62. }
  63. }
  64. // public void FastForward()
  65. // {
  66. // if (libvlc_media_player_ != IntPtr.Zero)
  67. // {
  68. // LibVlcAPI.libvlc_media_player_fastforward(libvlc_media_player_);
  69. // }
  70. // }
  71. public double GetPlayTime()
  72. {
  73. return LibVlcAPI.libvlc_media_player_get_time(libvlc_media_player_) / 1000.0;
  74. }
  75. public void SetPlayTime(double seekTime)
  76. {
  77. LibVlcAPI.libvlc_media_player_set_time(libvlc_media_player_, (Int64)(seekTime * ));
  78. }
  79. public int GetVolume()
  80. {
  81. return LibVlcAPI.libvlc_audio_get_volume(libvlc_media_player_);
  82. }
  83. public void SetVolume(int volume)
  84. {
  85. LibVlcAPI.libvlc_audio_set_volume(libvlc_media_player_, volume);
  86. }
  87. public void SetFullScreen(bool istrue)
  88. {
  89. LibVlcAPI.libvlc_set_fullscreen(libvlc_media_player_, istrue ? : );
  90. }
  91. public double Duration()
  92. {
  93. return duration_;
  94. }
  95. public string Version()
  96. {
  97. return LibVlcAPI.libvlc_get_version();
  98. }
  99. }
  100. internal static class LibVlcAPI
  101. {
  102. internal struct PointerToArrayOfPointerHelper
  103. {
  104. [MarshalAs(UnmanagedType.ByValArray, SizeConst = )]
  105. public IntPtr[] pointers;
  106. }
  107. public static IntPtr libvlc_new(string[] arguments)
  108. {
  109. PointerToArrayOfPointerHelper argv = new PointerToArrayOfPointerHelper();
  110. argv.pointers = new IntPtr[];
  111. for (int i = ; i < arguments.Length; i++)
  112. {
  113. argv.pointers[i] = Marshal.StringToHGlobalAnsi(arguments[i]);
  114. }
  115. IntPtr argvPtr = IntPtr.Zero;
  116. try
  117. {
  118. int size = Marshal.SizeOf(typeof(PointerToArrayOfPointerHelper));
  119. argvPtr = Marshal.AllocHGlobal(size);
  120. Marshal.StructureToPtr(argv, argvPtr, false);
  121. return libvlc_new(arguments.Length, argvPtr);
  122. }
  123. finally
  124. {
  125. for (int i = ; i < arguments.Length + ; i++)
  126. {
  127. if (argv.pointers[i] != IntPtr.Zero)
  128. {
  129. Marshal.FreeHGlobal(argv.pointers[i]);
  130. }
  131. }
  132. if (argvPtr != IntPtr.Zero)
  133. {
  134. Marshal.FreeHGlobal(argvPtr);
  135. }
  136. }
  137. }
  138. public static IntPtr libvlc_media_new_path(IntPtr libvlc_instance, string path)
  139. {
  140. IntPtr pMrl = IntPtr.Zero;
  141. try
  142. {
  143. byte[] bytes = Encoding.UTF8.GetBytes(path);
  144. pMrl = Marshal.AllocHGlobal(bytes.Length + );
  145. Marshal.Copy(bytes, , pMrl, bytes.Length);
  146. Marshal.WriteByte(pMrl, bytes.Length, );
  147. return libvlc_media_new_path(libvlc_instance, pMrl);
  148. }
  149. finally
  150. {
  151. if (pMrl != IntPtr.Zero)
  152. {
  153. Marshal.FreeHGlobal(pMrl);
  154. }
  155. }
  156. }
  157. public static IntPtr libvlc_media_new_location(IntPtr libvlc_instance, string path)
  158. {
  159. IntPtr pMrl = IntPtr.Zero;
  160. try
  161. {
  162. byte[] bytes = Encoding.UTF8.GetBytes(path);
  163. pMrl = Marshal.AllocHGlobal(bytes.Length + );
  164. Marshal.Copy(bytes, , pMrl, bytes.Length);
  165. Marshal.WriteByte(pMrl, bytes.Length, );
  166. return libvlc_media_new_path(libvlc_instance, pMrl);
  167. }
  168. finally
  169. {
  170. if (pMrl != IntPtr.Zero)
  171. {
  172. Marshal.FreeHGlobal(pMrl);
  173. }
  174. }
  175. }
  176.  
  177. // ----------------------------------------------------------------------------------------
  178. // 以下是libvlc.dll导出函数
  179.  
  180. // 创建一个libvlc实例,它是引用计数的
  181. [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
  182. [SuppressUnmanagedCodeSecurity]
  183. private static extern IntPtr libvlc_new(int argc, IntPtr argv);
  184.  
  185. // 释放libvlc实例
  186. [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
  187. [SuppressUnmanagedCodeSecurity]
  188. public static extern void libvlc_release(IntPtr libvlc_instance);
  189.  
  190. [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
  191. [SuppressUnmanagedCodeSecurity]
  192. public static extern String libvlc_get_version();
  193.  
  194. // 从视频来源(例如Url)构建一个libvlc_meida
  195. [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
  196. [SuppressUnmanagedCodeSecurity]
  197. private static extern IntPtr libvlc_media_new_location(IntPtr libvlc_instance, IntPtr path);
  198.  
  199. // 从本地文件路径构建一个libvlc_media
  200. [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
  201. [SuppressUnmanagedCodeSecurity]
  202. private static extern IntPtr libvlc_media_new_path(IntPtr libvlc_instance, IntPtr path);
  203.  
  204. [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
  205. [SuppressUnmanagedCodeSecurity]
  206. public static extern void libvlc_media_release(IntPtr libvlc_media_inst);
  207.  
  208. // 创建libvlc_media_player(播放核心)
  209. [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
  210. [SuppressUnmanagedCodeSecurity]
  211. public static extern IntPtr libvlc_media_player_new(IntPtr libvlc_instance);
  212.  
  213. // 将视频(libvlc_media)绑定到播放器上
  214. [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
  215. [SuppressUnmanagedCodeSecurity]
  216. public static extern void libvlc_media_player_set_media(IntPtr libvlc_media_player, IntPtr libvlc_media);
  217.  
  218. // 设置图像输出的窗口
  219. [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
  220. [SuppressUnmanagedCodeSecurity]
  221. public static extern void libvlc_media_player_set_hwnd(IntPtr libvlc_mediaplayer, Int32 drawable);
  222.  
  223. [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
  224. [SuppressUnmanagedCodeSecurity]
  225. public static extern void libvlc_media_player_play(IntPtr libvlc_mediaplayer);
  226.  
  227. /// <summary>
  228. ///
  229. /// </summary>
  230. /// <param name="libvlc_mediaplayer"></param>
  231. //[DllImport("libvlc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
  232. //[SuppressUnmanagedCodeSecurity]
  233. // public static extern void libvlc_media_player_fastforward(IntPtr libvlc_mediaplayer);
  234.  
  235. [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
  236. [SuppressUnmanagedCodeSecurity]
  237. public static extern void libvlc_media_player_pause(IntPtr libvlc_mediaplayer);
  238.  
  239. [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
  240. [SuppressUnmanagedCodeSecurity]
  241. public static extern void libvlc_media_player_stop(IntPtr libvlc_mediaplayer);
  242.  
  243. // 解析视频资源的媒体信息(如时长等)
  244. [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
  245. [SuppressUnmanagedCodeSecurity]
  246. public static extern void libvlc_media_parse(IntPtr libvlc_media);
  247.  
  248. // 返回视频的时长(必须先调用libvlc_media_parse之后,该函数才会生效)
  249. [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
  250. [SuppressUnmanagedCodeSecurity]
  251. public static extern Int64 libvlc_media_get_duration(IntPtr libvlc_media);
  252.  
  253. // 当前播放的时间
  254. [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
  255. [SuppressUnmanagedCodeSecurity]
  256. public static extern Int64 libvlc_media_player_get_time(IntPtr libvlc_mediaplayer);
  257.  
  258. // 设置播放位置(拖动)
  259. [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
  260. [SuppressUnmanagedCodeSecurity]
  261. public static extern void libvlc_media_player_set_time(IntPtr libvlc_mediaplayer, Int64 time);
  262.  
  263. [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
  264. [SuppressUnmanagedCodeSecurity]
  265. public static extern void libvlc_media_player_release(IntPtr libvlc_mediaplayer);
  266.  
  267. // 获取和设置音量
  268. [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
  269. [SuppressUnmanagedCodeSecurity]
  270. public static extern int libvlc_audio_get_volume(IntPtr libvlc_media_player);
  271.  
  272. [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
  273. [SuppressUnmanagedCodeSecurity]
  274. public static extern void libvlc_audio_set_volume(IntPtr libvlc_media_player, int volume);
  275.  
  276. // 设置全屏
  277. [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
  278. [SuppressUnmanagedCodeSecurity]
  279. public static extern void libvlc_set_fullscreen(IntPtr libvlc_media_player, int isFullScreen);
  280.  
  281. }
  282. }

VLC播放器的更多相关文章

  1. 使用vlc播放器做rtsp流媒体服务器

    可参考: 使用vlc播放器播放rtsp视频 web网页中使用vlc插件播放相机rtsp流视频 使用vlc进行二次开发做自己的播放器 首先需要安装vlc播放器,下载及安装步骤略 使用vlc播放器做rts ...

  2. 使用vlc播放器播放rtsp流视频

    可参考: 使用vlc播放器做rtsp服务器 web网页中使用vlc插件播放相机rtsp流视频 使用vlc进行二次开发做自己的播放器 首先需要安装vlc播放器,下载及安装步骤略 使用vlc播放器播放rt ...

  3. Android VLC播放器二次开发1——程序结构分析

    最近因为一个新项目需要一个多媒体播放器,所以需要做个视频.音频.图片方面的播放器.也查阅了不少这方面的资料,如果要从头做一个播放器工作量太大了,而且难度也很大.所以最后选择了VLC作为基础,进行二次开 ...

  4. centos7安装VLC播放器

    centos7安装VLC播放器 1.安装eple 下载地址:https://dl.fedoraproject.org/pub/epel/7/x86_64/e/epel-release-7-5.noar ...

  5. Centos 上使用mmsh协议听猫扑网络电台 VLC播放器

    Centos 上使用mmsh协议听猫扑网络电台 VLC播放器 安装CentOS已经有一段时间了,但是由于在Linux下除了学习,其他是事情都干不了.今天想闲来无事开了CentOS就想听一下歌,突然想起 ...

  6. Ubuntu安装VLC播放器

    Ubuntu安装VLC官方介绍:http://www.videolan.org/vlc/download-ubuntu.html sudo apt-get update sudo apt-get in ...

  7. Android VLC播放器二次开发3——音乐播放(歌曲列表+歌词同步滚动)

    今天讲一下对VLC播放器音频播放功能进行二次开发,讲解如何改造音乐播放相关功能.最近一直在忙着优化视频解码部分代码,因为我的视频播放器需要在一台主频比较低的机器上跑(800M主频),所以视频解码能力受 ...

  8. iOS实现基于VLC播放器的封装效果

    前言: 在一些特定场景下,我们获取到的音视频,由于格式比较特殊,用avplayer等播放器是无法播放的,此时,我们可以借助强大的VLC播放器来处理. 原理这里不再赘述,下面我们讲一下如何添加VLC播放 ...

  9. Android VLC播放器二次开发2——CPU类型检查+界面初始化

    上一篇讲了VLC整个程序的模块划分和界面主要使用的技术,今天分析一下VLC程序初始化过程,主要是初始化界面.加载解码库的操作.今天主要分析一下org.videolan.vlc.gui.MainActi ...

  10. 网页IE轻松调用VLC播放器实现监控(组件+方法大全)【转】

    公司突发奇想,要把刚买回来的网络监控机用自己内部网站在线监控. 作为网站的开发员,我接下了这个任务. 网络上有很多资料参与,但是都不全都不尽人意,最后经过多次的不同关键字的查找和测试,总算让我成功了. ...

随机推荐

  1. Leet Code 3. Longest Substring Without Repeating Characters

    Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...

  2. websocket的属性readyState

    webSocket的readyState属性用来定义连接状态,该属性的值有下面几种: 0 :对应常量CONNECTING (numeric value 0), 正在建立连接连接,还没有完成.The c ...

  3. effective_java 第34条:用接口模拟可伸缩的枚举

    例如: /** * 加减乘除枚举 * Created by yulinfeng on 8/20/17. */ public enum Operation { PLUS { double apply(d ...

  4. spring的历史和设计科学

    Spring的起源 要谈Spring的历史,就要先谈J2EE.J2EE应用程序的广泛实现是在1999年和2000年开始的,它的出现带来了诸如事务管理之类的核心中间层概念的标准化,但是在实践中并没有获得 ...

  5. L347

    Even Educators Believe These 7 Myths About Learning1. Individuals learn better when they receive inf ...

  6. Spring Boot 与 swagger 结合

    . 配置pom.xml 2. 更改端口号, 在src/main/resources 下面添加一个application.yml文件. 3. 添加一个ModelCase entity. 4. 添加一个i ...

  7. Mysql慢查询-Mysql慢查询详细教程

    一.简介开启慢查询日志,可以让MySQL记录下查询超过指定时间的语句,通过定位分析性能的瓶颈,才能更好的优化数据库系统的性能.二.参数说明slow_query_log 慢查询开启状态slow_quer ...

  8. 泊爷带你学go -- 经典的继承与接口 简直吊炸天 !

    package main import ( "fmt" ) type TeamBase struct { m_TeamId uint64 m_Rid uint32 m_RoomRu ...

  9. Wisdom RESTClient 使用教程

    Wisdom RESTClient 一款自动化测试REST API的工具,它可以自动化测试RESTful API并生成精美的测试报告,同时基于测试过的历史API,可以生成精美的RESTful API文 ...

  10. 免费的DDos网络测试工具集合

    今天晚上看YT上的hulk VS monster Dogs 然后想看电影资源,给我推送了hulk这款工具了解下,发现了一些东西,收藏下 1.卢瓦(LOIC) (Low Orbit Ion Canon) ...