C#下调用vlc库做一个简单播放器Word文件下载.docx
- 文档编号:20599020
- 上传时间:2023-01-24
- 格式:DOCX
- 页数:10
- 大小:21.75KB
C#下调用vlc库做一个简单播放器Word文件下载.docx
《C#下调用vlc库做一个简单播放器Word文件下载.docx》由会员分享,可在线阅读,更多相关《C#下调用vlc库做一个简单播放器Word文件下载.docx(10页珍藏版)》请在冰豆网上搜索。
publicstructExceptionStruct
privateintraised;
privateintcode;
privatestringmessage;
//造构Corehandle调
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);
//造构core调
classCore
//coreHandle字段和性属
privateCoreHandlecoreHandle;
publicCoreHandleCoreHandle
get{returncoreHandle;
//造方法构
publicCore(string[]argv,refMediaException.ExceptionStructex)
byte[][]argvbytes=newbyte[argv.Length][];
for(inti=0;
i<
argv.Length;
i++)
argvbytes[i]=Encoding.UTF8.GetBytes(argv[i]);
coreHandle=libvlc_new(argv.Length,ReturnIntPtr(argvbytes,argv.Length),refex);
//调调函数
privateIntPtrReturnIntPtr(byte[][]data,intlength)
IntPtr[]dataIntPtrArr=newIntPtr[length];
length;
dataIntPtrArr[i]=Marshal.AllocHGlobal(data[i].Length*sizeof(byte));
Marshal.Copy(data[i],0,dataIntPtrArr[i],data[i].Length);
IntPtrdataIntPtr=Marshal.AllocHGlobal(length*Marshal.SizeOf(typeof(IntPtr)));
Marshal.Copy(dataIntPtrArr,0,dataIntPtr,length);
returndataIntPtr;
//Dll调调调入
privatestaticexternCoreHandlelibvlc_new(intargc,IntPtrargv,ref
MediaException.ExceptionStructex);
//造构MediaHandle调
classMediaHandle:
publicMediaHandle()
:
libvlc_media_release(this);
privatestaticexternvoidlibvlc_media_release(MediaHandlemediaHandle);
//造构Media调
classMedia
//mediaHandle字段和性属
privateMediaHandlemediaHandle;
publicMediaHandleMediaHandle
get{returnmediaHandle;
publicMedia(CoreHandlecoreHandle,Stringfilename,ref
MediaException.ExceptionStructex)
//c#调UTF-16调调,libvlc.dll调UTF-8调调,需要调调.
UTF8Encodingutf8=newUTF8Encoding();
mediaHandle=libvlc_media_new(coreHandle,utf8.GetBytes(filename),
refex);
privatestaticexternMediaHandlelibvlc_media_new
(CoreHandlecoreHandle,[MarshalAs(UnmanagedType.LPArray)]
byte[]link,refMediaException.ExceptionStructex);
//造构MediaPlayerHandle调
classMediaPlayerHandle:
publicMediaPlayerHandle()
libvlc_media_player_release(this);
handle=IntPtr.Zero;
privatestaticexternvoidlibvlc_media_player_release(MediaPlayerHandle
mediaPlayerHandle);
//造构MediaPlayer调
classMediaPlayer
//mediaPlayerHandle字段和性属
privateMediaPlayerHandlemediaPlayerHandle;
publicMediaPlayerHandleMediaPlayerHandle
get{returnmediaPlayerHandle;
publicMediaPlayer(MediaHandlemediaHandle,refMediaException.ExceptionStructex)
mediaPlayerHandle=
libvlc_media_player_new_from_media(mediaHandle,refex);
//调置父口窗
publicvoidVedioSetParent(CoreHandlecoreHandle,IntPtrhDT,refMediaException.ExceptionStructex)
libvlc_video_set_parent(coreHandle,hDT,refex);
//播放
publicvoidPlay(refMediaException.ExceptionStructex)
libvlc_media_player_play(mediaPlayerHandle,refex);
//调停
publicvoidPause(refMediaException.ExceptionStructex)
libvlc_media_player_pause(mediaPlayerHandle,refex);
//停止
publicvoidstop(refMediaException.ExceptionStructex)
libvlc_media_player_stop(mediaPlayerHandle,refex);
privatestaticexternMediaPlayerHandle
libvlc_media_player_new_from_media(MediaHandlelibvlc_media_handle,refMediaException.ExceptionStructex);
privatestaticexternvoidlibvlc_video_set_parent(CoreHandlecoreHandle,
IntPtrhDT,refMediaException.ExceptionStructex);
privatestaticexternvoidlibvlc_media_player_play(MediaPlayerHandle
mediaPlayerHandle,refMediaException.ExceptionStructex);
privatestaticexternvoidlibvlc_media_player_pause(MediaPlayerHandle
privatestaticexternvoidlibvlc_media_player_stop(MediaPlayerHandlemediaPlayerHandle,refMediaException.ExceptionStructex);
//播放按调代调
privatevoidbutton1_Click(objectsender,EventArgse)
stringpath=textBox1.Text;
//调行播放的控件的句柄
IntPtrhdl=this.panel1.Handle;
//播放参数
string[]argv=newstring[]{"
-I"
"
--ignore-config"
};
//vlc调象的调建
MediaException.ExceptionStructex=newMediaException.ExceptionStruct();
Corecore=newCore(argv,refex);
Mediamedia=newMedia(core.CoreHandle,path,refex);
MediaPlayerplayer=newMediaPlayer(media.MediaHandle,refex);
//回收垃圾
GC.Collect();
player.VedioSetParent(core.CoreHandle,hdl,refex);
player.Play(refex);
//调调回收等相调操作垃圾
GC.WaitForPendingFinalizers();
//退出~调放
privatevoidbutton2_Click(objectsender,EventArgse)
if(player!
=null)
player.stop(refex);
player.MediaPlayerHandle.Dispose();
player=null;
media.MediaHandle.Dispose();
media=null;
core.CoreHandle.Dispose();
core=null;
第四步,调调~行。
调易播放器就做成了。
运
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- C# 调用 vlc 一个 简单 播放