vlc的应用之三:动态调用vlc-0.9.4的libvlc.dll

请移步https://higoge.github.io/,所有下载资料在那个博客都能找到。谢谢。 

--------------------------------------------------------------------

    vlc-0.9.4提供的libvlc.dll是可以动态调用的,Jeremiah这一篇博客就介绍下如何用C#和WinForm框架调用libvlc.dll作个简易播放器。

 

    1. vs2005新建工程,将vlc-0.9.4的libvlc.dll,libvlccore.dll,plugins目录全部拷贝到工程目录下面\bin\Debug中

 

    2. 创建异常结构体

InBlock.gifusing System;
InBlock.gifusing System.Collections.Generic;
InBlock.gifusing System.Text;
InBlock.gif
InBlock.gifnamespace MyOwnPlayer
InBlock.gif{
InBlock.gif        //异常结构体
InBlock.gif        public struct ExceptionStruct
InBlock.gif        {
InBlock.gif                private int raised;
InBlock.gif                private int code;
InBlock.gif                private string message;
InBlock.gif        }
InBlock.gif
InBlock.gif        class MediaException
InBlock.gif        {
InBlock.gif        }
InBlock.gif}

 

    3. CoreHandle和Core类

InBlock.gifusing System;
InBlock.gifusing System.Runtime.InteropServices;
InBlock.gif
InBlock.gifnamespace MyOwnPlayer
InBlock.gif{
InBlock.gif        class CoreHandle : SafeHandle
InBlock.gif        {
InBlock.gif                //构造方法
InBlock.gif                public CoreHandle()
InBlock.gif                        : base(IntPtr.Zero, true)
InBlock.gif                {
InBlock.gif                }
InBlock.gif
InBlock.gif                //重写的方法
InBlock.gif                public override bool IsInvalid
InBlock.gif                {
InBlock.gif                        get { return handle == IntPtr.Zero; }
InBlock.gif                }
InBlock.gif
InBlock.gif                protected override bool ReleaseHandle()
InBlock.gif                {
InBlock.gif                        if (!IsInvalid)
InBlock.gif                        {
InBlock.gif                                libvlc_release(this);
InBlock.gif                                handle = IntPtr.Zero;
InBlock.gif                        }
InBlock.gif                        return true;
InBlock.gif                }
InBlock.gif
InBlock.gif                protected override void Dispose(bool disposing)
InBlock.gif                {
InBlock.gif                        ReleaseHandle();
InBlock.gif                        base.Dispose(disposing);
InBlock.gif                }
InBlock.gif
InBlock.gif                //Dll动态导入
InBlock.gif                [DllImport("libvlc")]
InBlock.gif                private static extern void libvlc_release(CoreHandle coreHandle);
InBlock.gif        }
InBlock.gif}

 

InBlock.gifusing System;
InBlock.gifusing System.Runtime.InteropServices;
InBlock.gif
InBlock.gifnamespace MyOwnPlayer
InBlock.gif{
InBlock.gif        class Core
InBlock.gif        {
InBlock.gif                //coreHandle字段和属性
InBlock.gif                private CoreHandle coreHandle;
InBlock.gif                public CoreHandle CoreHandle
InBlock.gif                {
InBlock.gif                        get { return coreHandle; }
InBlock.gif                }
InBlock.gif                
InBlock.gif                //构造方法
InBlock.gif                public Core(string[] argv, ref ExceptionStruct ex)
InBlock.gif                {
InBlock.gif                        coreHandle = libvlc_new(argv.Length, argv, ref ex);
InBlock.gif                }
InBlock.gif
InBlock.gif                //Dll动态导入
InBlock.gif                [DllImport("libvlc")]
InBlock.gif                private static extern CoreHandle libvlc_new(int argc, string[] args, ref ExceptionStruct ex);
InBlock.gif        }
InBlock.gif}

 

    3. MediaHandle和Media类,注意里面的非英文路径处理方法。

InBlock.gifusing System;
InBlock.gifusing System.Runtime.InteropServices;
InBlock.gif
InBlock.gifnamespace MyOwnPlayer
InBlock.gif{
InBlock.gif        class MediaHandle : SafeHandle
InBlock.gif        {
InBlock.gif                //构造方法
InBlock.gif                public MediaHandle()
InBlock.gif                        : base(IntPtr.Zero, true)
InBlock.gif                {
InBlock.gif                }
InBlock.gif
InBlock.gif                //重写的方法
InBlock.gif                public override bool IsInvalid
InBlock.gif                {
InBlock.gif                        get { return handle == IntPtr.Zero; }
InBlock.gif                }
InBlock.gif
InBlock.gif                protected override bool ReleaseHandle()
InBlock.gif                {
InBlock.gif                        if (!IsInvalid)
InBlock.gif                        {
InBlock.gif                                libvlc_media_release(this);
InBlock.gif                                handle = IntPtr.Zero;
InBlock.gif                        }
InBlock.gif                        return true;
InBlock.gif                }
InBlock.gif
InBlock.gif                protected override void Dispose(bool disposing)
InBlock.gif                {
InBlock.gif                        ReleaseHandle();
InBlock.gif                        base.Dispose(disposing);
InBlock.gif                }
InBlock.gif
InBlock.gif                //Dll动态导入
InBlock.gif                [DllImport("libvlc")]
InBlock.gif                private static extern void libvlc_media_release(MediaHandle mediaHandle);                
InBlock.gif        }
InBlock.gif}

 

InBlock.gifusing System;
InBlock.gifusing System.Text;
InBlock.gifusing System.Runtime.InteropServices;
InBlock.gif
InBlock.gifnamespace MyOwnPlayer
InBlock.gif{
InBlock.gif        class Media
InBlock.gif        {
InBlock.gif                //mediaHandle字段和属性
InBlock.gif                private MediaHandle mediaHandle;
InBlock.gif                public MediaHandle MediaHandle
InBlock.gif                {
InBlock.gif                        get { return mediaHandle; }
InBlock.gif                }
InBlock.gif
InBlock.gif                //构造方法                
InBlock.gif                public Media(CoreHandle coreHandle, String filename, ref ExceptionStruct ex)
InBlock.gif                {
InBlock.gif                        //c#为UTF-16编码, libvlc.dll为UTF-8编码, 需要转换.
InBlock.gif                        UTF8Encoding utf8 = new UTF8Encoding();
InBlock.gif                        mediaHandle = libvlc_media_new(coreHandle, utf8.GetBytes(filename), ref ex);
InBlock.gif                }
InBlock.gif                
InBlock.gif                //Dll动态导入
InBlock.gif                [DllImport("libvlc")]
InBlock.gif                private static extern MediaHandle libvlc_media_new
InBlock.gif                        (CoreHandle coreHandle, [MarshalAs(UnmanagedType.LPArray)] byte[] link, ref ExceptionStruct ex);
InBlock.gif                }
InBlock.gif}

 

    5. MediaPlayerHandle和MediaPlayer类

InBlock.gifusing System;
InBlock.gifusing System.Runtime.InteropServices;
InBlock.gif
InBlock.gifnamespace MyOwnPlayer
InBlock.gif{
InBlock.gif        class MediaPlayerHandle : SafeHandle
InBlock.gif        {
InBlock.gif                //构造方法
InBlock.gif                public MediaPlayerHandle()
InBlock.gif                        : base(IntPtr.Zero, true)
InBlock.gif                {
InBlock.gif                }
InBlock.gif
InBlock.gif                //重写的方法
InBlock.gif                public override bool IsInvalid
InBlock.gif                {
InBlock.gif                        get { return handle == IntPtr.Zero; }
InBlock.gif                }
InBlock.gif
InBlock.gif                protected override bool ReleaseHandle()
InBlock.gif                {
InBlock.gif                        if (!IsInvalid)
InBlock.gif                        {
InBlock.gif                                libvlc_media_player_release(this);InBlock.gif                                handle = IntPtr.Zero;
InBlock.gif                        }
InBlock.gif                        return true;
InBlock.gif                }
InBlock.gif
InBlock.gif                protected override void Dispose(bool disposing)
InBlock.gif                {
InBlock.gif                        ReleaseHandle();
InBlock.gif                        base.Dispose(disposing);
InBlock.gif                }
InBlock.gif
InBlock.gif                //Dll动态导入
InBlock.gif                [DllImport("libvlc")]
InBlock.gif                private static extern void libvlc_media_player_release(MediaPlayerHandle mediaPlayerHandle);
InBlock.gif        }
InBlock.gif}

 

InBlock.gifusing System;
InBlock.gifusing System.Runtime.InteropServices;
InBlock.gif
InBlock.gifnamespace MyOwnPlayer
InBlock.gif{
InBlock.gif        class MediaPlayer
InBlock.gif        {
InBlock.gif                //mediaPlayerHandle字段和属性
InBlock.gif                private MediaPlayerHandle mediaPlayerHandle;
InBlock.gif                public MediaPlayerHandle MediaPlayerHandle
InBlock.gif                {
InBlock.gif                        get { return mediaPlayerHandle; }
InBlock.gif                }
InBlock.gif
InBlock.gif                //构造方法
InBlock.gif                public MediaPlayer(MediaHandle mediaHandle, ref ExceptionStruct ex)
InBlock.gif                {
InBlock.gif                        mediaPlayerHandle = libvlc_media_player_new_from_media(mediaHandle, ref ex);
InBlock.gif                }
InBlock.gif
InBlock.gif                //设置父窗口
InBlock.gif                public void VedioSetParent(CoreHandle coreHandle, IntPtr hDT, ref ExceptionStruct ex)
InBlock.gif                {
InBlock.gif                        libvlc_video_set_parent(coreHandle, hDT, ref ex);
InBlock.gif                }
InBlock.gif
InBlock.gif                //播放
InBlock.gif                public void Play(ref ExceptionStruct ex)
InBlock.gif                {
InBlock.gif                        libvlc_media_player_play(mediaPlayerHandle, ref ex);
InBlock.gif                }
InBlock.gif
InBlock.gif                //停止
InBlock.gif                public void Stop(ref ExceptionStruct ex)
InBlock.gif                {
InBlock.gif                        libvlc_media_player_stop(mediaPlayerHandle, ref ex);
InBlock.gif                }
InBlock.gif
InBlock.gif                //Dll动态导入
InBlock.gif                [DllImport("libvlc")]
InBlock.gif                private static extern MediaPlayerHandle libvlc_media_player_new_from_media(MediaHandle libvlc_media_handle, ref ExceptionStruct ex);
InBlock.gif
InBlock.gif                [DllImport("libvlc")]
InBlock.gif                private static extern void libvlc_video_set_parent(CoreHandle coreHandle, IntPtr hDT, ref ExceptionStruct ex);
InBlock.gif
InBlock.gif                [DllImport("libvlc")]
InBlock.gif                private static extern void libvlc_media_player_play(MediaPlayerHandle mediaPlayerHandle, ref ExceptionStruct ex);
InBlock.gif
InBlock.gif                [DllImport("libvlc")]
InBlock.gif                private static extern void libvlc_media_player_stop(MediaPlayerHandle mediaPlayerHandle, ref ExceptionStruct ex);
InBlock.gif        }
InBlock.gif}

 

    6. 基本工作做好了。下一步建立一个Form,里面画一个Panel(播放容器),画一个Textbox(播放地址),画一个Button(播放按钮),Button的点击事件为:

InBlock.gifprivate void button1_Click(object sender, EventArgs e)
InBlock.gif{
InBlock.gif        
//要播放的文件的uri
InBlock.gif        
string uri = this.textBox1.Text;
InBlock.gif
InBlock.gif        
//进行播放的控件的句柄
InBlock.gif        IntPtr hdl =
this.panel1.Handle;
InBlock.gif
InBlock.gif        
//播放参数
InBlock.gif        
string[] argv = new string[] { "-I", "--ignore-config" };
InBlock.gif
InBlock.gif        
//vlc对象的创建
InBlock.gif        ExceptionStruct ex =
new ExceptionStruct();
InBlock.gif        Core core =
new Core(argv, ref ex);
InBlock.gif        Media media =
new Media(core.CoreHandle, uri, ref ex);
InBlock.gif        MediaPlayer player =
new MediaPlayer(media.MediaHandle, ref ex);
InBlock.gif
InBlock.gif        
//垃圾回收
InBlock.gif        GC.Collect();
InBlock.gif
InBlock.gif        
//播放
InBlock.gif        player.VedioSetParent(core.CoreHandle, hdl,
ref ex);
InBlock.gif        player.Play(ref ex);
InBlock.gif
InBlock.gif        
//继续回收垃圾等相关操作
InBlock.gif        GC.Collect();
InBlock.gif        GC.WaitForPendingFinalizers();
InBlock.gif}

 

    7. 基本的播放功能就是这样实现的。其他接口请参考源码下面的\include\vlc\libvlc.h文件,里面有比较详细的对外接口的说明。

 

    8. 以上代码已经发送到附件中(MyOwnPlayer.rar),参考网址的楼主写的代码也在附件中(Marx_libvlc_wrapper(2).zip)。调试附件请注意第1步。

   

参考网址:

1. [url]http://forum.videolan.org/viewtopic.php?f=14&t=47385&st=0&sk=t&sd=a&hilit=C%23+wrapper[/url]

你可能感兴趣的:(C#,WinForm,VLC,休闲,libvlc.dll)