原文網址:http://www.cnblogs.com/haibindev/archive/2011/12/21/2296173.html
原文作者:haibindev
原文標題:c#万能视频播放器
本文的重點在於修正 class VlcPlayer,使其能順利在 VC# Express 2010 .Net Framework 4 下順利編譯。
修正重點在於 CallingConvention = CallingConvention.StdCall 改成 CallingConvention = CallingConvention.Cdecl
原文作者:haibindev
原文標題:c#万能视频播放器
本文的重點在於修正 class VlcPlayer,使其能順利在 VC# Express 2010 .Net Framework 4 下順利編譯。
修正重點在於 CallingConvention = CallingConvention.
using System; using System.Runtime.InteropServices; using System.Security; using System.Text; namespace VlcDotNet { class VlcPlayer { private IntPtr libvlc_instance_; private IntPtr libvlc_media_player_; private double duration_; public VlcPlayer(string pluginPath) { 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_); } public void SetRenderWindow(int wndHandle) { if (libvlc_instance_ != IntPtr.Zero && wndHandle != 0) { LibVlcAPI.libvlc_media_player_set_hwnd(libvlc_media_player_, wndHandle); } } public void PlayFile(string filePath) { IntPtr libvlc_media = LibVlcAPI.libvlc_media_new_path(libvlc_instance_, filePath); 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_); } } public void Pause() { if (libvlc_media_player_ != IntPtr.Zero) { LibVlcAPI.libvlc_media_player_pause(libvlc_media_player_); } } public void Stop() { if (libvlc_media_player_ != IntPtr.Zero) { LibVlcAPI.libvlc_media_player_stop(libvlc_media_player_); } } public double GetPlayTime() { return LibVlcAPI.libvlc_media_player_get_time(libvlc_media_player_) / 1000.0; } public void SetPlayTime(double seekTime) { LibVlcAPI.libvlc_media_player_set_time(libvlc_media_player_, (Int64)(seekTime * 1000)); } public int GetVolume() { return LibVlcAPI.libvlc_audio_get_volume(libvlc_media_player_); } public void SetVolume(int volume) { LibVlcAPI.libvlc_audio_set_volume(libvlc_media_player_, volume); } public void SetFullScreen(bool istrue) { LibVlcAPI.libvlc_set_fullscreen(libvlc_media_player_, istrue ? 1 : 0); } public double Duration() { return duration_; } public string Version() { return LibVlcAPI.libvlc_get_version(); } } internal static class LibVlcAPI { internal struct PointerToArrayOfPointerHelper { [MarshalAs(UnmanagedType.ByValArray, SizeConst = 11)] public IntPtr[] pointers; } 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]); } 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); } finally { for (int i = 0; i < arguments.Length + 1; i++) { if (argv.pointers[i] != IntPtr.Zero) { Marshal.FreeHGlobal(argv.pointers[i]); } } if (argvPtr != IntPtr.Zero) { Marshal.FreeHGlobal(argvPtr); } } } 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); } finally { if (pMrl != IntPtr.Zero) { Marshal.FreeHGlobal(pMrl); } } } 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); } finally { if (pMrl != IntPtr.Zero) { Marshal.FreeHGlobal(pMrl); } } } // ---------------------------------------------------------------------------------------- // libvlc.dll // 建立 libvlc 實體,注意引用參數(類似於 main()) [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] [SuppressUnmanagedCodeSecurity] private static extern IntPtr libvlc_new(int argc, IntPtr argv); // 釋放 libvlc 實體 [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] [SuppressUnmanagedCodeSecurity] public static extern void libvlc_release(IntPtr libvlc_instance); [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] [SuppressUnmanagedCodeSecurity] public static extern String libvlc_get_version(); // 從媒體來源(如Url)來建立 libvlc_media [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] [SuppressUnmanagedCodeSecurity] private static extern IntPtr libvlc_media_new_location(IntPtr libvlc_instance, IntPtr path); // 從本機文件路徑建立 libvlc_media [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] [SuppressUnmanagedCodeSecurity] private static extern IntPtr libvlc_media_new_path(IntPtr libvlc_instance, IntPtr path); [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] [SuppressUnmanagedCodeSecurity] public static extern void libvlc_media_release(IntPtr libvlc_media_inst); // 創建 libvlc_media_player (播放核心) [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] [SuppressUnmanagedCodeSecurity] public static extern IntPtr libvlc_media_player_new(IntPtr libvlc_instance); // 將媒體綁定到播放器上 [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] [SuppressUnmanagedCodeSecurity] public static extern void libvlc_media_player_set_media(IntPtr libvlc_media_player, IntPtr libvlc_media); // 設定輸出的地方 [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] [SuppressUnmanagedCodeSecurity] public static extern void libvlc_media_player_set_hwnd(IntPtr libvlc_mediaplayer, Int32 drawable); [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] [SuppressUnmanagedCodeSecurity] public static extern void libvlc_media_player_play(IntPtr libvlc_mediaplayer); [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] [SuppressUnmanagedCodeSecurity] public static extern void libvlc_media_player_pause(IntPtr libvlc_mediaplayer); [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] [SuppressUnmanagedCodeSecurity] public static extern void libvlc_media_player_stop(IntPtr libvlc_mediaplayer); // 剖析媒體資源的信息(如時間長短等) [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] [SuppressUnmanagedCodeSecurity] public static extern void libvlc_media_parse(IntPtr libvlc_media); // 回傳媒體的時間(必須先呼叫libvlc_media_parse後,此函數才會生效) [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] [SuppressUnmanagedCodeSecurity] public static extern Int64 libvlc_media_get_duration(IntPtr libvlc_media); // 目前播放的時間 [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] [SuppressUnmanagedCodeSecurity] public static extern Int64 libvlc_media_player_get_time(IntPtr libvlc_mediaplayer); // 設定播放位置(拖動) [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] [SuppressUnmanagedCodeSecurity] public static extern void libvlc_media_player_set_time(IntPtr libvlc_mediaplayer, Int64 time); [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] [SuppressUnmanagedCodeSecurity] public static extern void libvlc_media_player_release(IntPtr libvlc_mediaplayer); // 獲取和設定音量 [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] [SuppressUnmanagedCodeSecurity] public static extern int libvlc_audio_get_volume(IntPtr libvlc_media_player); [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] [SuppressUnmanagedCodeSecurity] public static extern void libvlc_audio_set_volume(IntPtr libvlc_media_player, int volume); // 設定全螢幕 [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] [SuppressUnmanagedCodeSecurity] public static extern void libvlc_set_fullscreen(IntPtr libvlc_media_player, int isFullScreen); } }
please post the a sample project of vlc in c#. Thanks
回覆刪除請發表最新的C#示例項目的libvlc。我需要它。請大家幫忙。謝謝
回覆刪除非常感谢您的程序,它给了我很大的帮助。
回覆刪除您的程序有一个地方需要修改:
public static IntPtr libvlc_media_new_location 函数里 return libvlc_media_new_path(libvlc_instance, pMrl);语句
应改为 return libvlc_media_new_location(libvlc_instance, pMrl); 这样才能播放网络视屏 Url 路径