Playing Video in C#

I have been working on a WPF (Windows Presentation Framework) application using Visual C# 2008 Express. Even though I am at the Initial Optimism phase of Software Development, and my imagination is running wild, I know that nothing will probably become of this work.

The basic goal is to play videos encoded with a number of different codecs (i.e. h.264, Xvid, and DivX). So, I thought, before I went into all of the other features that this application will do, I’ll keep things simple and just play a video with the filename hard-coded.

It’s the year 2008. I have the latest Microsoft tools available. Playing a video can’t be that difficult, can it?

1) WPF has a native MediaElement… that only supports wmv files.

2) Microsoft provides a Windows Media Player ActiveX Control (AxWMPLib), that does not seem to be able to use any codecs. The status displays “Locating codec”… “Error downloading codec”, and only plays the audio for the video file. Using the example from the Windows SDK has the same result.

3) DirectX 9.0 includes a Video class in Microsoft.DirectX.AudioVideoPlayback… that throws an exception “Error in the application” whenever you try to play a video. I even downloaded an example and built it in Visual Studio 2003, and that doesn’t work either.

4) VideoLAN has a .NET Interface… stored in a wiki. I guess the concept of a .zip file to download is too much for some developers. After copying-and-pasting the interface file-by-file, I built it, and got it to play video in the WPF application (by way of the WindowsFormsHost control).

    VLanControl.VlcUserControl lVideo = new VLanControl.VlcUserControl();
    windowsFormsHost1.Child = lVideo;
    lVideo.AddToPlayList( "W:/tv/Curb Your Enthusiasm/curb.3x01 - Chet's Shirt.avi", "Curb", null );

Hooray! The video played! I closed the application, ready to finish this off for good, when I experienced a blue screen of death.

As it turns out, you need to stop the video (and maybe clear the playlist) before you exit.

    protected override void OnClosing( CancelEventArgs e )
        if ( lVideo != null )
            System.Threading.Thread.Sleep( 1000 );
            System.Threading.Thread.Sleep( 1000 );
        base.OnClosing( e );

Be careful… in WPF, Microsoft decided to remove support for ‘OnClosing’-type events. You can add an event handler for Closed, but not Closing. At first, I had tried to add an event for Closed that would stop the video, but because this happened after the window had closed, I still experienced a blue screen.

Also note that the Sleep statements are absolutely necessary. I tried to get rid of them once, and tried to wait until the VlcUserControl state was no longer ‘Playing’, but the BSOD decided to appear anyway.

8 Responses to “Playing Video in C#”

  1. DaVince Says:

    What good is giving access to only half-working libraries for something? Microsoft’s weird. 😛

  2. ae Says:

    heyyyy im also trying to do this for very long time, i’ve installed c# express , now im going to try this out by the way i have zero experience of programing in any language, but we do have the same idea

    some french guy got the same idea to, its called streamplug check it out

    this bastard got it working for h264/xvid/3ivx/ in ogm

    but no source code !!!! damm french frog

  3. Akram Hameed Says:

    Ran across the same error myself recently. The actual problem you’re experiencing is likely to be a lack of a call to the native VLC object’s dispose function. If you tell the user-control to dispose when your form is closing, things should resolve themselves nicely and you won’t have to do dodgy non-deterministic things like invoking the Sleep function.

  4. randomguest Says:

    Any progress? 😀

  5. DaVince Says:

    Bastard? French frog? Shouldn’t you just be glad to see it’s possible, at least?

  6. stefano Says:

    I’ve never be able to compile the vlancontrol.dll file.
    Can you help me, sending me the project .csproj in the famous “zip file” 🙂
    thank you a lot

  7. sonam Says:

    Can u just create a small test application with the above code and required files,I have tried all the things whether in Windows form or WPF,using windowshostform but nothing happens.I would be very thankful to u if u can give a test project by mailing or uploading somewhere.
    Possibly u can reference your code to videolan forum becoz lots of people run in this problem.
    Plz reply

  8. sandrar Says:

    Hi! I was surfing and found your blog post… nice! I love your blog. 🙂 Cheers! Sandra. R.