原文 http://www.helyar.net/2009/libvlc-media-player-in-c-part-2/

I gave some simplified VLC media player code in part 1 to show how easy it was to do and how most wrapper libraries make a mountain out of a mole hill. In that entry, I briefly touched on using some classes to make it easier and safer to implement actual programs with this.

The first thing to do is write a wrapper for the exceptions, so that they are handled nicely in C#. For a program using the library, exceptions should be completely transparent and should be handled in the normal try/catch blocks without having to do anything like initialise them or check them.

Another thing to do is to move all of the initialisation functions into constructors and all of the release functions into destuctors or use the System.IDisposable interface.

Here is the code listing for the 4 classes used (VlcInstance, VlcMedia, VlcMediaPlayer and VlcException). Note that the first 3 of these are very similar and that the main difference is that the media player class has some extra functions for doing things like playing and pausing the content.

class VlcInstance : IDisposable
{internal IntPtr Handle;public VlcInstance(string[] args){VlcException ex = new VlcException();Handle = LibVlc.libvlc_new(args.Length, args, ref ex.Ex);if (ex.IsRaised) throw ex;}public void Dispose(){LibVlc.libvlc_release(Handle);}
}class VlcMedia : IDisposable
{internal IntPtr Handle;public VlcMedia(VlcInstance instance, string url){VlcException ex = new VlcException();Handle = LibVlc.libvlc_media_new(instance.Handle, url, ref ex.Ex);if (ex.IsRaised) throw ex;}public void Dispose(){LibVlc.libvlc_media_release(Handle);}
}class VlcMediaPlayer : IDisposable
{internal IntPtr Handle;private IntPtr drawable;private bool playing, paused;public VlcMediaPlayer(VlcMedia media){VlcException ex = new VlcException();Handle = LibVlc.libvlc_media_player_new_from_media(media.Handle, ref ex.Ex);if (ex.IsRaised) throw ex;}public void Dispose(){LibVlc.libvlc_media_player_release(Handle);}public IntPtr Drawable{get{return drawable;}set{VlcException ex = new VlcException();LibVlc.libvlc_media_player_set_drawable(Handle, value, ref ex.Ex);if (ex.IsRaised) throw ex;drawable = value;}}public bool IsPlaying { get { return playing && !paused; } }public bool IsPaused { get { return playing && paused; } }public bool IsStopped { get { return !playing; } }public void Play(){VlcException ex = new VlcException();LibVlc.libvlc_media_player_play(Handle, ref ex.Ex);if (ex.IsRaised) throw ex;playing = true;paused = false;}public void Pause(){VlcException ex = new VlcException();LibVlc.libvlc_media_player_pause(Handle, ref ex.Ex);if (ex.IsRaised) throw ex;if (playing)paused ^= true;}public void Stop(){VlcException ex = new VlcException();LibVlc.libvlc_media_player_stop(Handle, ref ex.Ex);if (ex.IsRaised) throw ex;playing = false;paused = false;}
}class VlcException : Exception
{internal libvlc_exception_t Ex;public VlcException() : base(){Ex = new libvlc_exception_t();LibVlc.libvlc_exception_init(ref Ex);}public bool IsRaised { get { return LibVlc.libvlc_exception_raised(ref Ex) != 0; } }public override string Message { get { return LibVlc.libvlc_exception_get_message(ref Ex); } }
}

Using these classes is even easier than before, can use proper exception handling (removed for brevity) and cleans up better at the end. In this example, I have added an OpenFileDialog, which is where the file is loaded.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace MyLibVLC
{public partial class Form1 : Form{VlcInstance instance;VlcMediaPlayer player;public Form1(){InitializeComponent();openFileDialog1.FileName = "";openFileDialog1.Filter = "MPEG|*.mpg|AVI|*.avi|All|*.*";string[] args = new string[] {"-I", "dummy", "--ignore-config",@"--plugin-path=C:\Program Files (x86)\VideoLAN\VLC\plugins","--vout-filter=deinterlace", "--deinterlace-mode=blend"};instance = new VlcInstance(args);player = null;}private void Form1_FormClosed(object sender, FormClosedEventArgs e){if(player != null) player.Dispose();instance.Dispose();}private void Open_Click(object sender, EventArgs e){if (openFileDialog1.ShowDialog() != DialogResult.OK)return;using (VlcMedia media = new VlcMedia(instance, openFileDialog1.FileName)){if (player != null) player.Dispose();player = new VlcMediaPlayer(media);}player.Drawable = panel1.Handle;}private void Play_Click(object sender, EventArgs e){player.Play();}private void Pause_Click(object sender, EventArgs e){player.Pause();}private void Stop_Click(object sender, EventArgs e){player.Stop();}}
}

Update:

I have just corrected a minor bug (the wrong release function being called on the player handle) and uploaded the full Visual Studio 2005 project. You can download the full project here (or see 1.1.2 version below). It comes with the libvlc.dll and libvlccore.dll for VLC 1.0.1 in the bin\x86\Debug directory so if you have a version other than this, just overwrite those files.

Update for VLC 1.1.2:

You can now download the VLC 1.1.2 compatible version. There were some changes to the way libvlc handles exceptions that needed to be corrected. Other than that, there were a couple of minor function name changes.

Please use these posts as a starting point to use your own code though. These posts are intended to stoppeople from being reliant on the already existing, large, overcomplicated and quickly outdated libraries. They are not intended to be just another library for people to blindly use without understanding how it works. You can use this to learn how to write your own native interop code on a well designed library then adapt it for your own changes and keep it up to date with whichever version of VLC you want. This also means you never have to use the terrible code on pinvoke.net for other libraries, as you can write your own from the original documentation and it will almost always be better.

Bugfix: VlcException should use Marshal.PtrToStringAnsi not Marshal.PtrToStringAuto

libvlc media player in C# (part 2)相关推荐

  1. libvlc media player in C# (part 1)

    libvlc media player in C# (part 1) 原文 http://www.helyar.net/2009/libvlc-media-player-in-c/ There see ...

  2. 【流媒体开发】VLC Media Player - Android 平台源码编译 与 二次开发详解 (提供详细800M下载好的编译源码及eclipse可调试播放器源码下载)

    作者 : 韩曙亮  博客地址 : http://blog.csdn.net/shulianghan/article/details/42707293 转载请注明出处 : http://blog.csd ...

  3. Unity3d C# 使用Universal Media Player(ump)插件播放视频的众坑之无法播放视频和VLC播放器依赖的问题

    前言 Ump播放视频的坑,相信很多人都踩过了很多了,这个问题是必须VLC播放器的问题,我默认导入UMP Pro Win Mac Linux WebGL 2.0.3后,设置界面是这样的: 并且无法去除U ...

  4. VLC media player ActiveX控件制作

    昨天折腾折腾这个折腾了一天.大致干了这几件事: 一,重新制作VLC安装包,去掉一些不必要的东东,设置一些必要的东东 二,制作ActiveX cab包 三,给CAB包加数字签名 四,调用代码 一,重新制 ...

  5. 用Windows Media Player截图的方法

    视频截图方法: 关闭"视频加速功能即可". 以Windows Media Player 9.0为例,选择菜单"工具→选项",找到"性能"选项 ...

  6. 【Qt】Qt再学习(八):Media Player(Qt实现多媒体播放器)

    1.简介 Media Player演示了一个简单的多媒体播放器,该播放器可以使用各种编解码器播放音频和/或视频文件. 涉及到的类有 QMediaPlayer.QMediaPlaylist.QVideo ...

  7. Media Player网页播放音频,视频,图片总汇

    播放MP3 url为MP3文件的路径,width,height为播放器大小 1 privatestring mp3(string url,intwidth,intheight)2 {3 return@ ...

  8. Windows Media Player 损坏提示“出现了内部应用程序错误解决方法

    在线看电影的时候图像花屏,更换浏览器都无法解决.每次开启在线电影的时候提示Windows Media Player 损坏提示"出现了内部应用程序错误".笔者尝试在Windows组件 ...

  9. [Winform]Media Player组件全屏播放的设置

    摘要 在设置程序开始运行时,让视频全屏播放时,直接设置 windowsMediaPlay.fullScreen = true; 会报错,代码如下 windowsMediaPlay.URL =_vide ...

最新文章

  1. 医院电脑瘫痪 病人排长队苦等5小时
  2. 周末一起用文本数据库玩玩Code First
  3. linux热插拔原理,.NET Core 的热插拔机制的深入探索
  4. pythonprint end_python print end =''
  5. 基于 Netty 如何实现高性能的 HTTP Client 的连接池
  6. java进出栈_JVM函数调用:Java出入栈
  7. QT实现RSS新闻阅读器
  8. IOS-状态栏的简单操作
  9. linux服务器监控zabbix,Linux监控之--使用ZABBIX监控web服务器
  10. MQ 消息队列问题整理
  11. java发卡系统_java毕业设计_springboot框架的自动发卡平台
  12. [C++] [OpenGL] 基于GLFW+GLAD的OpenGL简单程序
  13. php开发工具PhpStorm新版本V2022.1 新增功能亮点介绍
  14. 用人篇-曾国藩家书整理
  15. ANDROID 65536错误
  16. MySQL 去重方法之一
  17. 读此一席话,胜读十年书:最牛情场职场语录大全
  18. windows修改默认端口3389
  19. 解决:npm WARN config global `--global`, `--local` are deprecated. Use `--location=global` instead.
  20. Window10 应用商店闪退问题

热门文章

  1. js 数组遍历符合条件跳出循环体_Js数组遍历方法对比总结
  2. js中遍历数组加到新数组_JS数组遍历的十二种方式
  3. 走出雾霾,中国工业只能向上,你还在拖后腿吗?
  4. 软件著作权-源码清理
  5. 云计算与移动架构:AI认知系统与洁净的云计算 PPT分享
  6. 扶桑号战列舰(单调栈)
  7. 高校学生使用计算机软件,高校计算机运用软件教学
  8. 软件测试题库怎么样 这个刷题小程序很适合临时抱佛脚
  9. Matlab中的set函数用法
  10. JLINK识别不到芯片