原文 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.

  1. class VlcInstance : IDisposable
  2. {
  3. internal IntPtr Handle;
  4.  
  5. public VlcInstance(string[] args)
  6. {
  7. VlcException ex = new VlcException();
  8. Handle = LibVlc.libvlc_new(args.Length, args, ref ex.Ex);
  9. if (ex.IsRaised) throw ex;
  10. }
  11.  
  12. public void Dispose()
  13. {
  14. LibVlc.libvlc_release(Handle);
  15. }
  16. }
  17.  
  18. class VlcMedia : IDisposable
  19. {
  20. internal IntPtr Handle;
  21.  
  22. public VlcMedia(VlcInstance instance, string url)
  23. {
  24. VlcException ex = new VlcException();
  25. Handle = LibVlc.libvlc_media_new(instance.Handle, url, ref ex.Ex);
  26. if (ex.IsRaised) throw ex;
  27. }
  28.  
  29. public void Dispose()
  30. {
  31. LibVlc.libvlc_media_release(Handle);
  32. }
  33. }
  34.  
  35. class VlcMediaPlayer : IDisposable
  36. {
  37. internal IntPtr Handle;
  38. private IntPtr drawable;
  39. private bool playing, paused;
  40.  
  41. public VlcMediaPlayer(VlcMedia media)
  42. {
  43. VlcException ex = new VlcException();
  44. Handle = LibVlc.libvlc_media_player_new_from_media(media.Handle, ref ex.Ex);
  45. if (ex.IsRaised) throw ex;
  46. }
  47.  
  48. public void Dispose()
  49. {
  50. LibVlc.libvlc_media_player_release(Handle);
  51. }
  52.  
  53. public IntPtr Drawable
  54. {
  55. get
  56. {
  57. return drawable;
  58. }
  59. set
  60. {
  61. VlcException ex = new VlcException();
  62. LibVlc.libvlc_media_player_set_drawable(Handle, value, ref ex.Ex);
  63. if (ex.IsRaised) throw ex;
  64. drawable = value;
  65. }
  66. }
  67.  
  68. public bool IsPlaying { get { return playing && !paused; } }
  69.  
  70. public bool IsPaused { get { return playing && paused; } }
  71.  
  72. public bool IsStopped { get { return !playing; } }
  73.  
  74. public void Play()
  75. {
  76. VlcException ex = new VlcException();
  77. LibVlc.libvlc_media_player_play(Handle, ref ex.Ex);
  78. if (ex.IsRaised) throw ex;
  79.  
  80. playing = true;
  81. paused = false;
  82. }
  83.  
  84. public void Pause()
  85. {
  86. VlcException ex = new VlcException();
  87. LibVlc.libvlc_media_player_pause(Handle, ref ex.Ex);
  88. if (ex.IsRaised) throw ex;
  89.  
  90. if (playing)
  91. paused ^= true;
  92. }
  93.  
  94. public void Stop()
  95. {
  96. VlcException ex = new VlcException();
  97. LibVlc.libvlc_media_player_stop(Handle, ref ex.Ex);
  98. if (ex.IsRaised) throw ex;
  99.  
  100. playing = false;
  101. paused = false;
  102. }
  103. }
  104.  
  105. class VlcException : Exception
  106. {
  107. internal libvlc_exception_t Ex;
  108.  
  109. public VlcException() : base()
  110. {
  111. Ex = new libvlc_exception_t();
  112. LibVlc.libvlc_exception_init(ref Ex);
  113. }
  114.  
  115. public bool IsRaised { get { return LibVlc.libvlc_exception_raised(ref Ex) != 0; } }
  116.  
  117. public override string Message { get { return LibVlc.libvlc_exception_get_message(ref Ex); } }
  118. }

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.

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8.  
  9. namespace MyLibVLC
  10. {
  11. public partial class Form1 : Form
  12. {
  13. VlcInstance instance;
  14. VlcMediaPlayer player;
  15.  
  16. public Form1()
  17. {
  18. InitializeComponent();
  19.  
  20. openFileDialog1.FileName = "";
  21. openFileDialog1.Filter = "MPEG|*.mpg|AVI|*.avi|All|*.*";
  22.  
  23. string[] args = new string[] {
  24. "-I", "dummy", "--ignore-config",
  25. @"--plugin-path=C:\Program Files (x86)\VideoLAN\VLC\plugins",
  26. "--vout-filter=deinterlace", "--deinterlace-mode=blend"
  27. };
  28.  
  29. instance = new VlcInstance(args);
  30. player = null;
  31. }
  32.  
  33. private void Form1_FormClosed(object sender, FormClosedEventArgs e)
  34. {
  35. if(player != null) player.Dispose();
  36. instance.Dispose();
  37. }
  38.  
  39. private void Open_Click(object sender, EventArgs e)
  40. {
  41. if (openFileDialog1.ShowDialog() != DialogResult.OK)
  42. return;
  43.  
  44. using (VlcMedia media = new VlcMedia(instance, openFileDialog1.FileName))
  45. {
  46. if (player != null) player.Dispose();
  47. player = new VlcMediaPlayer(media);
  48. }
  49.  
  50. player.Drawable = panel1.Handle;
  51. }
  52.  
  53. private void Play_Click(object sender, EventArgs e)
  54. {
  55. player.Play();
  56. }
  57.  
  58. private void Pause_Click(object sender, EventArgs e)
  59. {
  60. player.Pause();
  61. }
  62.  
  63. private void Stop_Click(object sender, EventArgs e)
  64. {
  65. player.Stop();
  66. }
  67. }
  68. }

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)

    原文 http://www.helyar.net/2009/libvlc-media-player-in-c/ There seems to be a massive misconception ab ...

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

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

  3. 用VLC Media Player搭建简单的流媒体服务器

    VLC可以作为播放器使用,也可以搭建服务器. 在经历了Helix Server和Darwin Streaming Server+Perl的失败之后,终于找到了一个搭建流媒体简单好用的方法. 这个网址中 ...

  4. android错误之MediaPlayer用法的Media Player called in state *,androidmediaplayer

    用到Media Player,遇到几个问题,记一下 用法就不说了,使用的时候最好参考一下mediaPlayer的这张图 第一个错误是Media Player called in state 8 这个是 ...

  5. win7自带windows media player 已停止工作

    解决方法如下: 在计算机开始,菜单找到控制面板 ,然后打开程序和功能,选择打开或关闭window功能,媒体功能.再取消windows Media Center Windows MediaPlayer选 ...

  6. 转:Media Player Classic - HC 源代码分析

    VC2010 编译 Media Player Classic - Home Cinema (mpc-hc) Media Player Classic - Home Cinema (mpc-hc)播放器 ...

  7. Media Player 把光盘中的内容拷贝出来的方法

    http://jingyan.baidu.com/article/cb5d610529f0c1005c2fe0b4.html  这个链接是通过Media  Player 把光盘中的内容拷贝出来的方法h ...

  8. 20 Free Open Source Web Media Player Apps

    free Media Players (Free MP3, Video, and Music Player ...) are cool because they let web developers ...

  9. Windows Media Player安装了却不能播放网页上的视频

    前段时间遇到Windows Media Player安装了却不能播放网页上的视频的问题,在网上查找资料时,发现大部分资料都没能解决我这个问题.偶尔试了网上一牛人的方法,后来竟然解决了.现在再找那个网页 ...

随机推荐

  1. leetcode文章137称号-Single Number II

    #include<stdio.h> #include<stdlib.h> int singleNumber(int* nums, int numsSize) { int cou ...

  2. JAVA学习课第五十八届 — GUI

    GUI Graghical User Interface(图形用户接口) java为GUI提供的对象都存在java.awt和java.swing包中 Java的GUI做的的确干只是C++等.不打算浪费 ...

  3. MySQL SQL分析(SQL profile)

    分析SQL优化运营开销SQL的重要手段.在MySQL数据库.可配置profiling参数启用SQL分析.此参数可以在全局和session水平集.级别则作用于整个MySQL实例,而session级别紧影 ...

  4. Javascript中的深拷贝和浅拷贝

    var obj = { a:1, arr: [1,2] }; var obj1 = obj; //浅复制 var obj2 = deepCopy(obj); //深复制 javascript中创建对象 ...

  5. Cordova 使用经验

    1. 需要下载ant,ant需要的文件: build.xml <?xml version="1.0" ?> <project name ="antPro ...

  6. java 中间String分类

    String a = "aaa"; 用这样的方式的时候java首先在内存中寻找"aaa"字符串.假设有.就把aaa的地址给它 假设没有则创建 String a ...

  7. IOS的UITextField,UIButton,UIWebView它描述的一些属性和IOS提示图像资源

    有时UI要开发的资源与实际frame不符.这一次,我们要绘制图片 UIImage* image = [[UIImage imageNamed:@"text_field_bg.png" ...

  8. 兼容Firefox和IE的onpropertychange事件oninput

    原文 兼容Firefox和IE的onpropertychange事件oninput onpropertychange能够捕获每次输入值的变化.例如:对象的value值被改变时,onpropertych ...

  9. 一款非常棒的纯CSS3 3D菜单演示及制作教程

    原文:一款非常棒的纯CSS3 3D菜单演示及制作教程 这段时间比较忙,很久没在这里分享一些漂亮的HTML5和CSS3资源了,今天起的早,看到一款很不错的CSS3 3D菜单,觉得非常上眼,就将它分享给大 ...

  10. Android_显示器本身被卸载应用程序

    1.经jni实现功能 //LOG宏定义 #define LOG_INFO(tag, msg) __android_log_write(ANDROID_LOG_INFO, tag, msg) #defi ...