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.
April 22nd, 2008 at 3:42 am
What good is giving access to only half-working libraries for something? Microsoft’s weird. 😛
May 24th, 2008 at 5:31 pm
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
June 26th, 2008 at 12:55 am
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.
July 4th, 2008 at 8:25 pm
Any progress? 😀
July 6th, 2008 at 5:40 am
Bastard? French frog? Shouldn’t you just be glad to see it’s possible, at least?
July 24th, 2008 at 12:44 am
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
January 12th, 2009 at 1:29 am
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
September 10th, 2009 at 5:56 pm
Hi! I was surfing and found your blog post… nice! I love your blog. 🙂 Cheers! Sandra. R.