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 );
    lVideo.Play();

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 )
        {
            lVideo.Stop();
            System.Threading.Thread.Sleep( 1000 );
            lVideo.ClearPlayList();
            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.

19 Responses to “Playing Video in C#”

  1. DaVince Says:

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

  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? :D

  5. DaVince Says:

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

  6. stefano Says:

    heeelp
    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
    Stefano

  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.

  9. Lawrence Says:

    Hi

    Can you u send me a project file, so that i can understand better and about your comment its good and very useful bec the blue screen error comes to us also. Also if you know how to implement vlc player directly into the wpf application, pl send me a project

    Thank you
    R.Lawrence

  10. Drew Says:

    Can you send me a copy of the .dll? Preeze

  11. Deann Kremple Says:

    Its incredible now there is a exiting way to fix your RROD I just found out today. Check my link posted. If it does not work try rrod-fix-pro dot com.

  12. driver download windows 7 Says:

    As a Newbie, I am always searching online for articles that can aid me. Thank you

  13. cialis Says:

    Nice post. I be taught something tougher on completely different blogs everyday. It would all the time be stimulating to learn content material from other writers and observe slightly one thing from their store. I’d desire to make use of some with the content material on my blog whether you don’t mind. Natually I’ll provide you with a link in your net blog. Thanks for sharing.

  14. Leda Hilgefort Says:

    Hi. I’ve been lurking for a while now on your blog and decisively wanted to get the bravado up to say thanks to you. You’ve impressed me so significantly I have started my very own weblog and it’s been going ok so far.

  15. Zandra Lannom Says:

    I’m inside the crossroads throughout my career. I’m in the heart of using for law school, however really should write. You’ll manage to area that may mix the 2, or is one able to select one within the other? I’m not searching to train law, much I know about myself. If possibly to create a move that’s challenging, fun, creative and significant. I’m afraid that individuals must quit conntacting have the ability to fullfill an operating existence..

  16. Marhta Antunez Says:

    There are some fascinating cut-off dates in this article however I don’t know if I see all of them center to heart. There’s some validity but I will take hold opinion until I look into it further. Good article , thanks and we want extra! Added to FeedBurner as effectively

  17. Dick Alvin Says:

    How much of an appealing guide, keep on making much better half

  18. Voetensex Says:

    You can abide by Anahi on Twitter right here:

  19. Escort London Service Says:

    Best site, very good article.

Leave a Reply