VLCPlayer.cs 31 KB

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