vlc的应用之三:动态调用vlc-0.9.4的libvlc.dllvlc-0.9.4提供的libvlc.dll是可以动态调用的,Jeremiah这一篇博客就介绍下如何用C#和WinForm框架调用libvlc.dll作个简易播放器。1.vs2005新建工程,将vlc-0.9.4的libvlc.dll,libvlccore.dll,plugins目录全部拷贝到工程目录下面\bin\Debug中。2.创建异常结构体usingSystem;usingSystem.Collections.Generic;usingSystem.Text;namespaceMyOwnPlayer{//异常结构体publicstructExceptionStruct{privateintraised;privateintcode;privatestringmessage;}classMediaException{}}3.CoreHandle和Core类usingSystem;usingSystem.Runtime.InteropServices;namespaceMyOwnPlayer{classCoreHandle:SafeHandle{//构造方法publicCoreHandle():base(IntPtr.Zero,true){}//重写的方法publicoverrideboolIsInvalid{get{returnhandle==IntPtr.Zero;}}protectedoverrideboolReleaseHandle(){if(!IsInvalid){libvlc_release(this);handle=IntPtr.Zero;}returntrue;}protectedoverridevoidDispose(booldisposing){ReleaseHandle();base.Dispose(disposing);}//Dll动态导入[DllImport(libvlc)]privatestaticexternvoidlibvlc_release(CoreHandlecoreHandle);}}usingSystem;usingSystem.Runtime.InteropServices;namespaceMyOwnPlayer{classCore{//coreHandle字段和属性privateCoreHandlecoreHandle;publicCoreHandleCoreHandle{get{returncoreHandle;}}//构造方法publicCore(string[]argv,refExceptionStructex){coreHandle=libvlc_new(argv.Length,argv,refex);}//Dll动态导入[DllImport(libvlc)]privatestaticexternCoreHandlelibvlc_new(intargc,string[]args,refExceptionStructex);}}3.MediaHandle和Media类,注意里面的非英文路径处理方法。usingSystem;usingSystem.Runtime.InteropServices;namespaceMyOwnPlayer{classMediaHandle:SafeHandle{//构造方法publicMediaHandle():base(IntPtr.Zero,true){}//重写的方法publicoverrideboolIsInvalid{get{returnhandle==IntPtr.Zero;}}protectedoverrideboolReleaseHandle(){if(!IsInvalid){libvlc_media_release(this);handle=IntPtr.Zero;}returntrue;}protectedoverridevoidDispose(booldisposing){ReleaseHandle();base.Dispose(disposing);}//Dll动态导入[DllImport(libvlc)]privatestaticexternvoidlibvlc_media_release(MediaHandlemediaHandle);}}usingSystem;usingSystem.Text;usingSystem.Runtime.InteropServices;namespaceMyOwnPlayer{classMedia{//mediaHandle字段和属性privateMediaHandlemediaHandle;publicMediaHandleMediaHandle{get{returnmediaHandle;}}//构造方法publicMedia(CoreHandlecoreHandle,Stringfilename,refExceptionStructex){//c#为UTF-16编码,libvlc.dll为UTF-8编码,需要转换.UTF8Encodingutf8=newUTF8Encoding();mediaHandle=libvlc_media_new(coreHandle,utf8.GetBytes(filename),refex);}//Dll动态导入[DllImport(libvlc)]privatestaticexternMediaHandlelibvlc_media_new(CoreHandlecoreHandle,[MarshalAs(UnmanagedType.LPArray)]byte[]link,refExceptionStructex);}}5.MediaPlayerHandle和MediaPlayer类usingSystem;usingSystem.Runtime.InteropServices;namespaceMyOwnPlayer{classMediaPlayerHandle:SafeHandle{//构造方法publicMediaPlayerHandle():base(IntPtr.Zero,true){}//重写的方法publicoverrideboolIsInvalid{get{returnhandle==IntPtr.Zero;}}protectedoverrideboolReleaseHandle(){if(!IsInvalid){libvlc_media_player_release(this);handle=IntPtr.Zero;}returntrue;}protectedoverridevoidDispose(booldisposing){ReleaseHandle();base.Dispose(disposing);}//Dll动态导入[DllImport(libvlc)]privatestaticexternvoidlibvlc_media_player_release(MediaPlayerHandlemediaPlayerHandle);}}usingSystem;usingSystem.Runtime.InteropServices;namespaceMyOwnPlayer{classMediaPlayer{//mediaPlayerHandle字段和属性privateMediaPlayerHandlemediaPlayerHandle;publicMediaPlayerHandleMediaPlayerHandle{get{returnmediaPlayerHandle;}}//构造方法publicMediaPlayer(MediaHandlemediaHandle,refExceptionStructex){mediaPlayerHandle=libvlc_media_player_new_from_media(mediaHandle,refex);}//设置父窗口publicvoidVedioSetParent(CoreHandlecoreHandle,IntPtrhDT,refExceptionStructex){libvlc_video_set_parent(coreHandle,hDT,refex);}//播放publicvoidPlay(refExceptionStructex){libvlc_media_player_play(mediaPlayerHandle,refex);}//停止publicvoidStop(refExceptionStructex){libvlc_media_player_stop(mediaPlayerHandle,refex);}//Dll动态导入[DllImport(libvlc)]privatestaticexternMediaPlayerHandlelibvlc_media_player_new_from_media(MediaHandlelibvlc_media_handle,refExceptionStructex);[DllImport(libvlc)]privatestaticexternvoidlibvlc_video_set_parent(CoreHandlecoreHandle,IntPtrhDT,refExceptionStructex);[DllImport(libvlc)]privatestaticexternvoidlibvlc_media_player_play(MediaPlayerHandlemediaPlayerHandle,refExceptionStructex);[DllImport(libvlc)]privatestaticexternvoidlibvlc_media_player_stop(MediaPlayerHandlemediaPlayerHandle,refExceptionStructex);}}6.基本工作做好了。下一步建立一个Form,里面画一个Panel(播放容器),画一个Textbox(播放地址),画一个Button(播放按钮),Button的点击事件为:privatevoidbutton1_Click(objectsender,EventArgse){//要播放的文件的uristringuri=this.textBox1.Text;//进行播放的控件的句柄IntPtrhdl=this.panel1.Handle;//播放参数string[]argv=newstring[]{-I,--ignore-config};//vlc对象的创建ExceptionStructex=newExceptionStruct();Corecore=newCore(argv,refex);Mediamedia=newMedia(core.CoreHandle,uri,refex);MediaPlayerplayer=newMediaPlayer(media.MediaHandle,refex);//垃圾回收GC.Collect();//播放player.VedioSetParent(core.CoreHandle,hdl,refex);player.Play(refex);//继续回收垃圾等相关操作GC.Collect();GC.WaitForPendingFinalizers();}7.基本的播放功能就是这样实现的。其他接口请参考源码下面的\include\vlc\libvlc.h文件,里面有比较详细的对外接口的说明。8.以上代码已经发送到附件中(MyOwnPlayer.rar),参考网址的楼主写的代码也在附件中(Marx_libvlc_wrapper(2).zip)。调试附件请注意第1步。参考网址:1.[url]