VLC在C#的使用方法-RTSP播放文件

1.首先下载好VLC播放器在播放器的目录下获得LibVlc等核心dll库,plugins文件夹并将其放到c#的工程下。

2.将dll里的方法导入到c#里如下,还有的方法可以查看vlc的api添加

[csharp]  view plain  copy
  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.             PointerToArrayOfPointerHelper argv = new PointerToArrayOfPointerHelper();  
  14.             argv.pointers = 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.                 Marshal.StructureToPtr(argv, argvPtr, 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.             PointerToArrayOfPointerHelper argv = new PointerToArrayOfPointerHelper();  
  128.             argv.pointers = 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.                 Marshal.StructureToPtr(argv, argvPtr, 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.         [DllImport("libvlc", CallingConvention = CallingConvention.StdCall, ExactSpelling = true)]  
  165.         [SuppressUnmanagedCodeSecurity]  
  166.         public static extern void libvlc_media_add_option(IntPtr libvlc_media, IntPtr argv);  
  167.   
  168.         [DllImport("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.         [DllImport("libvlc", CallingConvention = CallingConvention.StdCall, ExactSpelling = true)]  
  174.         [SuppressUnmanagedCodeSecurity]  
  175.         public static extern void libvlc_release(IntPtr libvlc_instance);  
  176.   
  177.   
  178.         [DllImport("libvlc",CallingConvention = CallingConvention.StdCall, ExactSpelling = true)]  
  179.         [SuppressUnmanagedCodeSecurity]  
  180.         private static extern IntPtr libvlc_media_new(IntPtr libvlc_media, IntPtr path);  
  181.   
  182.         [DllImport("libvlc", CallingConvention = CallingConvention.StdCall, ExactSpelling = true)]  
  183.         [SuppressUnmanagedCodeSecurity]  
  184.         public static extern String libvlc_get_version();  
  185.   
  186.         // 从视频来源(例如Url)构建一个libvlc_meida  
  187.         [DllImport("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.         [DllImport("libvlc", CallingConvention = CallingConvention.StdCall, ExactSpelling = true)]  
  193.         [SuppressUnmanagedCodeSecurity]  
  194.         private static extern IntPtr libvlc_media_new_path(IntPtr libvlc_instance, IntPtr path);  
  195.   
  196.         [DllImport("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.         [DllImport("libvlc", CallingConvention = CallingConvention.StdCall, ExactSpelling = true)]  
  202.         [SuppressUnmanagedCodeSecurity]  
  203.         public static extern IntPtr libvlc_media_player_new(IntPtr libvlc_instance);  
  204.         // 将视频(libvlc_media)绑定到播放器上  
  205.         [DllImport("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.         [DllImport("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.         [DllImport("libvlc", CallingConvention = CallingConvention.StdCall, ExactSpelling = true)]  
  215.         [SuppressUnmanagedCodeSecurity]  
  216.         public static extern void libvlc_media_player_play(IntPtr libvlc_mediaplayer);  
  217.   
  218.         [DllImport("libvlc", CallingConvention = CallingConvention.StdCall, ExactSpelling = true)]  
  219.         [SuppressUnmanagedCodeSecurity]  
  220.         public static extern void libvlc_media_player_pause(IntPtr libvlc_mediaplayer);  
  221.   
  222.         [DllImport("libvlc", CallingConvention = CallingConvention.StdCall, ExactSpelling = true)]  
  223.         [SuppressUnmanagedCodeSecurity]  
  224.         public static extern void libvlc_media_player_stop(IntPtr libvlc_mediaplayer);  
  225.   
  226.         // 解析视频资源的媒体信息(如时长等)  
  227.         [DllImport("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.         [DllImport("libvlc", CallingConvention = CallingConvention.StdCall, ExactSpelling = true)]  
  233.         [SuppressUnmanagedCodeSecurity]  
  234.         public static extern Int64 libvlc_media_get_duration(IntPtr libvlc_media);  
  235.   
  236.         // 当前播放的时间  
  237.         [DllImport("libvlc", CallingConvention = CallingConvention.StdCall, ExactSpelling = true)]  
  238.         [SuppressUnmanagedCodeSecurity]  
  239.         public static extern Int64 libvlc_media_player_get_time(IntPtr libvlc_mediaplayer);  
  240.   
  241.         // 设置播放位置(拖动)  
  242.         [DllImport("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.         [DllImport("libvlc", CallingConvention = CallingConvention.StdCall, ExactSpelling = true)]  
  247.         [SuppressUnmanagedCodeSecurity]  
  248.         public static extern void libvlc_media_player_release(IntPtr libvlc_mediaplayer);  
  249.   
  250.         // 获取和设置音量  
  251.         [DllImport("libvlc", CallingConvention = CallingConvention.StdCall, ExactSpelling = true)]  
  252.         [SuppressUnmanagedCodeSecurity]  
  253.         public static extern int libvlc_audio_get_volume(IntPtr libvlc_media_player);  
  254.   
  255.         [DllImport("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.         [DllImport("libvlc", CallingConvention = CallingConvention.StdCall, ExactSpelling = true)]  
  261.         [SuppressUnmanagedCodeSecurity]  
  262.         public static extern void libvlc_set_fullscreen(IntPtr libvlc_media_player, int isFullScreen);  
  263. }  


[csharp]  view plain  copy
  1. 3.定义vlc实例  
[csharp]  view plain  copy
  1. "code" class="csharp">"white-space:pre">  private IntPtr libvlc_instance_;  
  2.   
  3.         private IntPtr libvlc_media_player_;  


 
  5.初始化实例 
  

[csharp]  view plain  copy
  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播放器里获得
[csharp]  view plain  copy
  1. private VlcPlayer vlc_player_;  
  2.   
  3. vlc_player_ = new VlcPlayer(pluginPath, arguments);  
  4.                 IntPtr render_wnd = player.Handle;  
  5.                 vlc_player_.SetRenderWindow((int)render_wnd);  
  6.  vlc_player_.PlayFile_OnRtsp((String)path);  

在程序里使用的参数与vlc播放器获得的有些许不同,可以参考官方文档稍作修改
6.创建实例并播放

[csharp]  view plain  copy
  1. private VlcPlayer vlc_player_;  
  2.   
  3. vlc_player_ = new VlcPlayer(pluginPath, arguments);  
  4.                 IntPtr render_wnd = player.Handle;  
  5.                 vlc_player_.SetRenderWindow((int)render_wnd);  
  6.  vlc_player_.PlayFile_OnRtsp((String)path);  
tips:wpf程序可以使用作为winform的控件作为播放界面

 
           
           

   



转自:http://blog.csdn.net/io437/article/details/51188045

更多参考:

http://blog.csdn.net/lassewang/article/details/52240894


你可能感兴趣的:(流媒体,VLC)