{"id":23,"date":"2008-02-16T15:41:30","date_gmt":"2008-02-16T20:41:30","guid":{"rendered":"http:\/\/www.redrecondite.com\/blog\/2008\/02\/16\/playing-video-in-c\/"},"modified":"2013-04-30T17:01:14","modified_gmt":"2013-04-30T22:01:14","slug":"playing-video-in-c","status":"publish","type":"post","link":"https:\/\/www.redrecondite.com\/blog\/2008\/02\/16\/playing-video-in-c\/","title":{"rendered":"Playing Video in C#"},"content":{"rendered":"<p>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.<\/p>\n<p>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&#8217;ll keep things simple and just play a video with the filename hard-coded.<\/p>\n<p>It&#8217;s the year 2008.  I have the latest Microsoft tools available.  Playing a video can&#8217;t be that difficult, can it?<!--more--><\/p>\n<p>1) WPF has a native MediaElement&#8230; that only supports wmv files.<\/p>\n<p>2) Microsoft provides a Windows Media Player ActiveX Control (AxWMPLib), that does not seem to be able to use any codecs.  The status displays &#8220;Locating codec&#8221;&#8230; &#8220;Error downloading codec&#8221;, and only plays the audio for the video file.  Using the example from the Windows SDK has the same result.<\/p>\n<p>3) DirectX 9.0 includes a Video class in Microsoft.DirectX.AudioVideoPlayback&#8230; that throws an exception &#8220;Error in the application&#8221; whenever you try to play a video.  I even downloaded an example and built it in Visual Studio 2003, and that doesn&#8217;t work either.<\/p>\n<p>4)  <a href=\"\/\/www.videolan.org\/\">VideoLAN<\/a> has a <a href=\"\/\/wiki.videolan.org\/.Net_Interface_to_VLC\">.NET Interface<\/a>&#8230; 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).<\/p>\n<pre>    VLanControl.VlcUserControl lVideo = new VLanControl.VlcUserControl();<\/pre>\n<pre>    windowsFormsHost1.Child = lVideo;<\/pre>\n<pre>    lVideo.AddToPlayList( \"W:\/tv\/Curb Your Enthusiasm\/curb.3x01 - Chet's Shirt.avi\", \"Curb\", null );<\/pre>\n<pre>    lVideo.Play();<\/pre>\n<p>Hooray!  The video played!  I closed the application, ready to finish this off for good, when I experienced a blue screen of death.<\/p>\n<p>As it turns out, you need to stop the video (and maybe clear the playlist) before you exit.<\/p>\n<pre>    protected override void OnClosing( CancelEventArgs e )\r\n    {\r\n        if ( lVideo != null )\r\n        {\r\n            lVideo.Stop();\r\n            System.Threading.Thread.Sleep( 1000 );\r\n            lVideo.ClearPlayList();\r\n            System.Threading.Thread.Sleep( 1000 );\r\n        }\r\n        base.OnClosing( e );\r\n    }<\/pre>\n<p>Be careful&#8230; in WPF, Microsoft decided to remove support for &#8216;OnClosing&#8217;-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.<\/p>\n<p>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 &#8216;Playing&#8217;, but the BSOD decided to appear anyway.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13,12,14],"tags":[],"class_list":["post-23","post","type-post","status-publish","format-standard","hentry","category-c-sharp","category-development","category-wpf"],"_links":{"self":[{"href":"https:\/\/www.redrecondite.com\/blog\/wp-json\/wp\/v2\/posts\/23"}],"collection":[{"href":"https:\/\/www.redrecondite.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.redrecondite.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.redrecondite.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.redrecondite.com\/blog\/wp-json\/wp\/v2\/comments?post=23"}],"version-history":[{"count":1,"href":"https:\/\/www.redrecondite.com\/blog\/wp-json\/wp\/v2\/posts\/23\/revisions"}],"predecessor-version":[{"id":284,"href":"https:\/\/www.redrecondite.com\/blog\/wp-json\/wp\/v2\/posts\/23\/revisions\/284"}],"wp:attachment":[{"href":"https:\/\/www.redrecondite.com\/blog\/wp-json\/wp\/v2\/media?parent=23"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.redrecondite.com\/blog\/wp-json\/wp\/v2\/categories?post=23"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.redrecondite.com\/blog\/wp-json\/wp\/v2\/tags?post=23"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}