1.首先下載下傳好VLC播放器在播放器的目錄下獲得LibVlc等核心dll庫,plugins檔案夾并将其放到c#的工程下。
2.将dll裡的方法導入到c#裡如下,還有的方法可以檢視vlc的api添加
1. class VlcPlayer
2. {
3. internal static class LibVlcAPI
4. {
5. internal struct PointerToArrayOfPointerHelper
6. {
7. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 11)]
8. public IntPtr[] pointers;
9. }
10.
11. public static IntPtr libvlc_new(string[] arguments)
12. {
13. new PointerToArrayOfPointerHelper();
14. new IntPtr[11];
15.
16. for (int i = 0; i < arguments.Length; i++)
17. {
18. argv.pointers[i] = Marshal.StringToHGlobalAnsi(arguments[i]);
19. }
20.
21. IntPtr argvPtr = IntPtr.Zero;
22. try
23. {
24. int size = Marshal.SizeOf(typeof(PointerToArrayOfPointerHelper));
25. argvPtr = Marshal.AllocHGlobal(size);
26. false);
27. return libvlc_new(arguments.Length, argvPtr);
28. }
29. finally
30. {
31. for (int i = 0; i < arguments.Length + 1; i++)
32. {
33. if (argv.pointers[i] != IntPtr.Zero)
34. {
35. Marshal.FreeHGlobal(argv.pointers[i]);
36. }
37. }
38. if (argvPtr != IntPtr.Zero)
39. {
40. Marshal.FreeHGlobal(argvPtr);
41. }
42. }
43. }
44. public static IntPtr libvlc_media_new_path(IntPtr libvlc_instance, string path)
45. {
46. IntPtr pMrl = IntPtr.Zero;
47. try
48. {
49. byte[] bytes = Encoding.UTF8.GetBytes(path);
50. pMrl = Marshal.AllocHGlobal(bytes.Length + 1);
51. Marshal.Copy(bytes, 0, pMrl, bytes.Length);
52. Marshal.WriteByte(pMrl, bytes.Length, 0);
53. return libvlc_media_new_path(libvlc_instance, pMrl);
54. }
55. finally
56. {
57. if (pMrl != IntPtr.Zero)
58. {
59. Marshal.FreeHGlobal(pMrl);
60. }
61. }
62. }
63.
64. public static IntPtr libvlc_media_new(IntPtr libvlc_instance, string path)
65. {
66. IntPtr pMrl = IntPtr.Zero;
67. try
68. {
69. byte[] bytes = Encoding.UTF8.GetBytes(path);
70. pMrl = Marshal.AllocHGlobal(bytes.Length + 1);
71. Marshal.Copy(bytes, 0, pMrl, bytes.Length);
72. Marshal.WriteByte(pMrl, bytes.Length, 0);
73. return libvlc_media_new_location(libvlc_instance, pMrl);
74. }
75. finally
76. {
77. if (pMrl != IntPtr.Zero)
78. {
79. Marshal.FreeHGlobal(pMrl);
80. }
81. }
82. }
83.
84. public static IntPtr libvlc_media_new_path_rtsp(IntPtr libvlc_instance, string path)
85. {
86. IntPtr pMrl = IntPtr.Zero;
87. try
88. {
89. byte[] bytes = Encoding.UTF8.GetBytes(path);
90. pMrl = Marshal.AllocHGlobal(bytes.Length + 1);
91. Marshal.Copy(bytes, 0, pMrl, bytes.Length);
92. Marshal.WriteByte(pMrl, bytes.Length, 0);
93. return libvlc_media_new_location(libvlc_instance, pMrl);
94. }
95. finally
96. {
97. if (pMrl != IntPtr.Zero)
98. {
99. Marshal.FreeHGlobal(pMrl);
100. }
101. }
102. }
103.
104.
105. public static IntPtr libvlc_media_new_location(IntPtr libvlc_instance, string path)
106. {
107. IntPtr pMrl = IntPtr.Zero;
108. try
109. {
110. byte[] bytes = Encoding.UTF8.GetBytes(path);
111. pMrl = Marshal.AllocHGlobal(bytes.Length + 1);
112. Marshal.Copy(bytes, 0, pMrl, bytes.Length);
113. Marshal.WriteByte(pMrl, bytes.Length, 0);
114. return libvlc_media_new_path(libvlc_instance, pMrl);
115. }
116. finally
117. {
118. if (pMrl != IntPtr.Zero)
119. {
120. Marshal.FreeHGlobal(pMrl);
121. }
122. }
123. }
124.
125. public static void libvlc_media_addoption(IntPtr libvlc_media, string[] arguments)
126. {
127. new PointerToArrayOfPointerHelper();
128. new IntPtr[11];
129.
130. for (int i = 0; i < arguments.Length; i++)
131. {
132. argv.pointers[i] = Marshal.StringToHGlobalAnsi(arguments[i]);
133. }
134.
135. IntPtr argvPtr = IntPtr.Zero;
136. try
137. {
138. int size = Marshal.SizeOf(typeof(PointerToArrayOfPointerHelper));
139. argvPtr = Marshal.AllocHGlobal(size);
140. false);
141. libvlc_media_add_option(libvlc_media, argvPtr);
142. }
143. finally
144. {
145. for (int i = 0; i < arguments.Length + 1; i++)
146. {
147. if (argv.pointers[i] != IntPtr.Zero)
148. {
149. Marshal.FreeHGlobal(argv.pointers[i]);
150. }
151. }
152. if (argvPtr != IntPtr.Zero)
153. {
154. Marshal.FreeHGlobal(argvPtr);
155. }
156. }
157. }
158.
159.
160. [DllImport("libvlc", CallingConvention = CallingConvention.StdCall, ExactSpelling = true)]
161. [SuppressUnmanagedCodeSecurity]
162. private static extern IntPtr libvlc_new(int argc, IntPtr argv);
163.
164. "libvlc", CallingConvention = CallingConvention.StdCall, ExactSpelling = true)]
165. [SuppressUnmanagedCodeSecurity]
166. public static extern void libvlc_media_add_option(IntPtr libvlc_media, IntPtr argv);
167.
168. "libvlc", CallingConvention = CallingConvention.StdCall, ExactSpelling = true)]
169. [SuppressUnmanagedCodeSecurity]
170. public static extern IntPtr libvlc_media_player_new_from_media(IntPtr libvlc_instance);
171.
172. // 釋放libvlc執行個體
173. "libvlc", CallingConvention = CallingConvention.StdCall, ExactSpelling = true)]
174. [SuppressUnmanagedCodeSecurity]
175. public static extern void libvlc_release(IntPtr libvlc_instance);
176.
177.
178. "libvlc",CallingConvention = CallingConvention.StdCall, ExactSpelling = true)]
179. [SuppressUnmanagedCodeSecurity]
180. private static extern IntPtr libvlc_media_new(IntPtr libvlc_media, IntPtr path);
181.
182. "libvlc", CallingConvention = CallingConvention.StdCall, ExactSpelling = true)]
183. [SuppressUnmanagedCodeSecurity]
184. public static extern String libvlc_get_version();
185.
186. // 從視訊來源(例如Url)建構一個libvlc_meida
187. "libvlc", CallingConvention = CallingConvention.StdCall, ExactSpelling = true)]
188. [SuppressUnmanagedCodeSecurity]
189. private static extern IntPtr libvlc_media_new_location(IntPtr libvlc_instance, IntPtr path);
190.
191. // 從本地檔案路徑建構一個libvlc_media
192. "libvlc", CallingConvention = CallingConvention.StdCall, ExactSpelling = true)]
193. [SuppressUnmanagedCodeSecurity]
194. private static extern IntPtr libvlc_media_new_path(IntPtr libvlc_instance, IntPtr path);
195.
196. "libvlc", CallingConvention = CallingConvention.StdCall, ExactSpelling = true)]
197. [SuppressUnmanagedCodeSecurity]
198. public static extern void libvlc_media_release(IntPtr libvlc_media_inst);
199.
200. // 建立libvlc_media_player(播放核心)
201. "libvlc", CallingConvention = CallingConvention.StdCall, ExactSpelling = true)]
202. [SuppressUnmanagedCodeSecurity]
203. public static extern IntPtr libvlc_media_player_new(IntPtr libvlc_instance);
204. // 将視訊(libvlc_media)綁定到播放器上
205. "libvlc", CallingConvention = CallingConvention.StdCall, ExactSpelling = true)]
206. [SuppressUnmanagedCodeSecurity]
207. public static extern void libvlc_media_player_set_media(IntPtr libvlc_media_player, IntPtr libvlc_media);
208.
209. // 設定圖像輸出的視窗
210. "libvlc", CallingConvention = CallingConvention.StdCall, ExactSpelling = true)]
211. [SuppressUnmanagedCodeSecurity]
212. public static extern void libvlc_media_player_set_hwnd(IntPtr libvlc_mediaplayer, Int32 drawable);
213.
214. "libvlc", CallingConvention = CallingConvention.StdCall, ExactSpelling = true)]
215. [SuppressUnmanagedCodeSecurity]
216. public static extern void libvlc_media_player_play(IntPtr libvlc_mediaplayer);
217.
218. "libvlc", CallingConvention = CallingConvention.StdCall, ExactSpelling = true)]
219. [SuppressUnmanagedCodeSecurity]
220. public static extern void libvlc_media_player_pause(IntPtr libvlc_mediaplayer);
221.
222. "libvlc", CallingConvention = CallingConvention.StdCall, ExactSpelling = true)]
223. [SuppressUnmanagedCodeSecurity]
224. public static extern void libvlc_media_player_stop(IntPtr libvlc_mediaplayer);
225.
226. // 解析視訊資源的媒體資訊(如時長等)
227. "libvlc", CallingConvention = CallingConvention.StdCall, ExactSpelling = true)]
228. [SuppressUnmanagedCodeSecurity]
229. public static extern void libvlc_media_parse(IntPtr libvlc_media);
230.
231. // 傳回視訊的時長(必須先調用libvlc_media_parse之後,該函數才會生效)
232. "libvlc", CallingConvention = CallingConvention.StdCall, ExactSpelling = true)]
233. [SuppressUnmanagedCodeSecurity]
234. public static extern Int64 libvlc_media_get_duration(IntPtr libvlc_media);
235.
236. // 目前播放的時間
237. "libvlc", CallingConvention = CallingConvention.StdCall, ExactSpelling = true)]
238. [SuppressUnmanagedCodeSecurity]
239. public static extern Int64 libvlc_media_player_get_time(IntPtr libvlc_mediaplayer);
240.
241. // 設定播放位置(拖動)
242. "libvlc", CallingConvention = CallingConvention.StdCall, ExactSpelling = true)]
243. [SuppressUnmanagedCodeSecurity]
244. public static extern void libvlc_media_player_set_time(IntPtr libvlc_mediaplayer, Int64 time);
245.
246. "libvlc", CallingConvention = CallingConvention.StdCall, ExactSpelling = true)]
247. [SuppressUnmanagedCodeSecurity]
248. public static extern void libvlc_media_player_release(IntPtr libvlc_mediaplayer);
249.
250. // 擷取和設定音量
251. "libvlc", CallingConvention = CallingConvention.StdCall, ExactSpelling = true)]
252. [SuppressUnmanagedCodeSecurity]
253. public static extern int libvlc_audio_get_volume(IntPtr libvlc_media_player);
254.
255. "libvlc", CallingConvention = CallingConvention.StdCall, ExactSpelling = true)]
256. [SuppressUnmanagedCodeSecurity]
257. public static extern void libvlc_audio_set_volume(IntPtr libvlc_media_player, int volume);
258.
259. // 設定全屏
260. "libvlc", CallingConvention = CallingConvention.StdCall, ExactSpelling = true)]
261. [SuppressUnmanagedCodeSecurity]
262. public static extern void libvlc_set_fullscreen(IntPtr libvlc_media_player, int isFullScreen);
263. }
1. 3.定義vlc執行個體
1. <pre name="code" class="csharp"><span style="white-space:pre"> </span>private IntPtr libvlc_instance_;
2.
3. private IntPtr libvlc_media_player_;
5.初始化執行個體
1. public VlcPlayer(string pluginPath,string[] arguments)
2. {
3. string plugin_arg = "--plugin-path=" + pluginPath;//插件路徑
4. // string[] arguments = {"--sout=#duplicate{dst=display,dst=std{access=udp,mux=ts,dst=" + "127.0.0.1:1234/1" + "}}"};
5. // string[] arguments = { "-I", "dummy", "--ignore-config", "--no-video-title", plugin_arg };
6. //:sout=#transcode{vcodec=h264,vb=800,acodec=mpga,ab=128,channels=2,samplerate=44100}:rtp{sdp=rtsp://:8554/1} :sout-keep
7. //string[] arguments = { "--sout=#transcode{vcodec=h264,acodec=mpga,ab=128,channels=2,samplerate=44100}:rtp{sdp=rtsp://:1234/1}", "--sout-all", "--sout-keep", "--sout-display" };
8. //string[] arguments = { "--sout=#transcode{vcodec=h264,acodec=mpga,ab=128,channels=2,samplerate=44100}:duplicate{dst=standard{access=dshow,mux=avi,dst=e:/test.avi}, dst=rtp{dst=127.0.0.1,name=stream,port=5005,mux=ts,sdp=rtp://127.0.0.1}, dst=display}" };
9. //string[] arguments = { "--sout=#transcode{vcodec=mp4v,vb=2048,scale=1,acodec=mp3,ab=128,channels=2,samplerate=44100}:duplicate{dst=rtp{sdp=rtsp://:8554/1},dst=display} --sout-all --sout-keep", "--dshow-size=1024" };
10. libvlc_instance_ = LibVlcAPI.libvlc_new(arguments);
11. //string[] arguments = { "--sout=#duplicate{dst=rtp{dst=127.0.0.1,port=1234,sdp=rtsp://127.0.0.1:1234/1}}" };
12. //string[] arguments = { "-I", "dummy", "--ignore-config", "--no-video-title", plugin_arg };
13.
14. libvlc_media_player_ = LibVlcAPI.libvlc_media_player_new(libvlc_instance_);
15. }
arguments就是該vlc的執行個體參數,可以檢視官方文檔,也可以在vlc播放器裡獲得

1. private VlcPlayer vlc_player_;
2.
3. vlc_player_ = new VlcPlayer(pluginPath, arguments);
4. IntPtr render_wnd = player.Handle;
5. int)render_wnd);
6. vlc_player_.PlayFile_OnRtsp((String)path);
在程式裡使用的參數與vlc播放器獲得的有些許不同,可以參考官方文檔稍作修改
6.建立執行個體并播放
1. private VlcPlayer vlc_player_;
2.
3. vlc_player_ = new VlcPlayer(pluginPath, arguments);
4. IntPtr render_wnd = player.Handle;
5. int)render_wnd);
6. vlc_player_.PlayFile_OnRtsp((String)path);
tips:wpf程式可以使用作為winform的控件作為播放界面
<wfi:WindowsFormsHost Margin="0,21,237,0">
<aforge:VideoSourcePlayer x:Name="player" Width="300" Height="360">
</aforge:VideoSourcePlayer>
</wfi:WindowsFormsHost>