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

There seems to be a massive misconception about using VLC inside an application and many, many large wrapper libraries have been written. These are often harder to use than libvlc itself, buggy or just downright don’t work (at least not in what will be “the latest” version of VLC at the time you want to write anything).

Using the libvlc documentation directly and the libvlc example I wrote a simple wrapper class that performs the basics needed to play, pause and stop media. Because it is libvlc, things like resizing the video, toggling full screen by double clicking the video output or streaming media from a source device or network are handled automatically.

This code was all written and tested with VLC 0.98a but because it is taken from the documentation and example, it should work for all versions 0.9x and later with only minor changes. Because it is so simple, these changes should be easy to make. Most of the time, these changes will just be slight function name changes and no new re-structuring is needed.

The first thing to note is that there is no version of libvlc for Windows x64. All developers should set their CPU type to x86, even if they have a 32bit machine. If you set it to “Any CPU” then 64bit users will not be able to load libvlc.dll and will crash out. If you are compiling from the command line, this should look something like csc /platform:x86 foobar.cs

The second thing to note, which trips up a lot of users, is that you must specify VLC’s plugin directory. This may make distribution a nightmare, as the plugin directory is a large directory full of DLLs. It may be possible to narrow down these DLLs to just the ones your application actually needs but I don’t know if videolan have any advice about or licensing for redistribution of these.

libvlc is made up of several modules. For the sake of simplicity in this example, I will use 1 static class to contain every exported C function and split them up visually by module with #region.

The nicest thing about VLC, as far as interop with C# goes, is that all memory management is handled internally by libvlc and functions are provided for doing anything that you would need to do to their members. This means that using an IntPtr is suitable for almost everything. You just need to make sure that you pass the correct IntPtr into each function but another layer of C# encapsulating this would easily be able to make sure of that, as discussed in part 2. The only structure that you need to define is an exception, which is very simple. You then simply always pass in references to these structs with ref ex.

The code listing for the wrapper class is as follows:

using System;
using System.Runtime.InteropServices;
 
namespace MyLibVLC
{
// http://www.videolan.org/developers/vlc/doc/doxygen/html/group__libvlc.html
 
[StructLayout(LayoutKind.Sequential, Pack = 1)]
struct libvlc_exception_t
{
public int b_raised;
public int i_code;
[MarshalAs(UnmanagedType.LPStr)]
public string psz_message;
}
 
static class LibVlc
{
#region core
[DllImport("libvlc")]
public static extern IntPtr libvlc_new(int argc, [MarshalAs(UnmanagedType.LPArray,
ArraySubType = UnmanagedType.LPStr)] string[] argv, ref libvlc_exception_t ex);
 
[DllImport("libvlc")]
public static extern void libvlc_release(IntPtr instance);
#endregion
 
#region media
[DllImport("libvlc")]
public static extern IntPtr libvlc_media_new(IntPtr p_instance,
[MarshalAs(UnmanagedType.LPStr)] string psz_mrl, ref libvlc_exception_t p_e);
 
[DllImport("libvlc")]
public static extern void libvlc_media_release(IntPtr p_meta_desc);
#endregion
 
#region media player
[DllImport("libvlc")]
public static extern IntPtr libvlc_media_player_new_from_media(IntPtr media,
ref libvlc_exception_t ex);
 
[DllImport("libvlc")]
public static extern void libvlc_media_player_release(IntPtr player);
 
[DllImport("libvlc")]
public static extern void libvlc_media_player_set_drawable(IntPtr player, IntPtr drawable,
ref libvlc_exception_t p_e);
 
[DllImport("libvlc")]
public static extern void libvlc_media_player_play(IntPtr player, ref libvlc_exception_t ex);
 
[DllImport("libvlc")]
public static extern void libvlc_media_player_pause(IntPtr player, ref libvlc_exception_t ex);
 
[DllImport("libvlc")]
public static extern void libvlc_media_player_stop(IntPtr player, ref libvlc_exception_t ex);
#endregion
 
#region exception
[DllImport("libvlc")]
public static extern void libvlc_exception_init(ref libvlc_exception_t p_exception);
 
[DllImport("libvlc")]
public static extern int libvlc_exception_raised(ref libvlc_exception_t p_exception);
 
[DllImport("libvlc")]
public static extern string libvlc_exception_get_message(ref libvlc_exception_t p_exception);
#endregion
}
}

For a sample application to use this simple wrapper, I just created a new Windows form and added a play button, stop button and a panel for viewing the video. In this example, the stop button also cleans everything up so you should make sure to press it before closing the form.

At one point during this code, libvlc can optionally be given a HWND to draw to. If you don’t give it one, it pops up a new player. However, people seem to be confused over how simple this is to do in C# and have been making large amounts of interop calls to the Win32 API to get handles. This is not necessary, as System.Windows.Forms.Control.Handle allows you go get the window handle (HWND) to any component that inherits from the Control class. This includes the Form class and the Panel class (and even the Button class) so all you actually need to pass it is this.Handle (for the handle to the form itself) or panel.Handle (for a Panel called panel). If you want it to start fullscreen, add the command line argument “-f” rather than using the Win32 function GetDesktopWindow().

Because I will be using this to display PAL video, which is interlaced at 576i, I have added some deinterlacing options to the command line. These are --vout-filter=deinterlace and --deinterlace-mode=blend.

Without further ado, here is the code listing for the partial windows form class:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
 
using System.Runtime.InteropServices;
 
namespace MyLibVLC
{
public partial class Form1 : Form
{
IntPtr instance, player;
 
public Form1()
{
InitializeComponent();
}
 
private void Play_Click(object sender, EventArgs e)
{
libvlc_exception_t ex = new libvlc_exception_t();
LibVlc.libvlc_exception_init(ref ex);
 
string[] args = new string[] {
"-I", "dummy", "--ignore-config",
@"--plugin-path=C:\Program Files (x86)\VideoLAN\VLC\plugins",
"--vout-filter=deinterlace", "--deinterlace-mode=blend"
};
 
instance = LibVlc.libvlc_new(args.Length, args, ref ex);
Raise(ref ex);
 
IntPtr media = LibVlc.libvlc_media_new(instance, @"C:\foobar.mpg", ref ex);
Raise(ref ex);
 
player = LibVlc.libvlc_media_player_new_from_media(media, ref ex);
Raise(ref ex);
 
LibVlc.libvlc_media_release(media);
 
// panel1 may be any component including a System.Windows.Forms.Form but
// this example uses a System.Windows.Forms.Panel
LibVlc.libvlc_media_player_set_drawable(player, panel1.Handle, ref ex);
Raise(ref ex);
 
LibVlc.libvlc_media_player_play(player, ref ex);
Raise(ref ex);
}
 
private void Stop_Click(object sender, EventArgs e)
{
libvlc_exception_t ex = new libvlc_exception_t();
LibVlc.libvlc_exception_init(ref ex);
 
LibVlc.libvlc_media_player_stop(player, ref ex);
Raise(ref ex);
 
LibVlc.libvlc_media_player_release(player);
LibVlc.libvlc_release(instance);
}
 
static void Raise(ref libvlc_exception_t ex)
{
if (LibVlc.libvlc_exception_raised(ref ex) != 0)
MessageBox.Show(LibVlc.libvlc_exception_get_message(ref ex));
}
}
}

Note that this section of code is deprecated and the code from part 2 should be used instead.

Adding a pause button is similar to the stop button but without the cleanup.

Here is an example slightly further on down the line but using the same code:

libvlc media player in C# (part 1)的更多相关文章

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

    原文 http://www.helyar.net/2009/libvlc-media-player-in-c-part-2/ I gave some simplified VLC media play ...

  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. apache本地多域配置(wampserver本地多域配置)

    当我们在当地发展.通常在浏览器中输入 http://localhost/项目目录名 测试Web文件,你有没有想过在本地浏览器中,输入自己设定的名字进入项目目录,名相关的问题. 比方我想配置一个主域名w ...

  2. C# 判断文件的真实格式

    为了防止图片木马,通过后缀判断文件的格式是不准确的.可以通过这种方式进行判断. static void Main(string[] args) { string path = @"C:\Us ...

  3. phpcmsv9更改fckeditor编者ueditor编辑

    于phpcmsv9文件夹中找到phpcms/libs/classes/form.class.php档 找到几行代码 <span style="font-size:18px;" ...

  4. NSIS:安装、卸载时检查程序是否正在运行

    原文 NSIS:安装.卸载时检查程序是否正在运行 如果我们要安装或升级的程序正在运行,文件肯定会替换不成功,以下代码可以提示用户结束正在运行的程序. 需要使用插件FindProcDLL.dll,下载路 ...

  5. Codeforces Round #274 (Div. 2) B. Towers

    As you know, all the kids in Berland love playing with cubes. Little Petya has n towers consisting o ...

  6. Windows下一个MySQL有些错误的解决方法

    1.无论是什么提示.我们有一个直接看错误日志.由于它描述了最具体描述错误日志. 于MySQL安装文件夹中找到 my.ini简介 看日志保存路径 2. 我的错误是[ERROR] Fatal error: ...

  7. 王立平-NGUI

    NGUI: UI插入 UI : 人机交互界面 UI大部分是2D 眼下就我们总结这些.也许会增加 版权声明:本文博主原创文章,博客,未经同意不得转载.

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

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

  9. Effective C++学习笔记(Part One:Item 1-4)

    最近的最终effectvie C++仔细阅读侧,我很惊讶C++动力和魅力.最近的" LL最近记得阅读体验和读书笔记其.必要查找使用,是什么假设总结不合适.欢迎批评: 如今仅仅列出框架,近期会 ...

  10. 使用pfile 启动oracle 实例时,启动失败---db_recovery_file_dest參数值在os上不存在。

    [oracle@vm22 ~]$ export ORACLE_SID=orcl [oracle@vm22 ~]$ sqlplus / as sysdba SQL*Plus: Release 10.2. ...