Libvlc Media Player in C # (Part 2)

Source: Internet
Author: User

Original 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 specified tions, so that they are handled nicely in C #. for a program using the library, exceptions shoshould be completely transparent and shoshould 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 breaching) 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 intendedStopPeople from being reliant on the already existing, large, overcomplicated and quickly outdated libraries. They areNotIntended 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:VlcExceptionShocould useMarshal.PtrToStringAnsiNotMarshal.PtrToStringAuto

Libvlc Media Player in C # (Part 2)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.