I could say this about WPF as a whole, but when things actually work with WPF it’s so damn smooth I can’t help but feel somewhat accomplished. Nevertheless, I had a real pain trying to get an embedded flash player into my WPF app. Why Flash? Why not…dare I say, Silverlight? Well, I’m building a small app that plays YouTube videos, so obviously Flash makes sense. First, here’s some lessons I learned while on this wonderfully frustrating journey:
- For this instance, the
WebBrowsercontrol blows.: My first attempt was to add an embed tag into a
WebBrowsercontrol, which is put inside of a
WindowsFormsHostcontrol, which is how .NET encapsulates pre-3.0 controls. This did not work in the long run because first of all, IE throws script errors when you try to play YouTube’s .SWF files, and also when resizing the background flashes and looks like utter garbage. Don’t get me wrong, it has its uses. Just not here.
- Pre-packaged solutions also suck.: There’s a few flash solutions out there that I tried, either got working and had to pay ridiculous amounts for (oh look, $1000 off this month!), or plainly didn’t work at all. Just don’t use them.
- Google is your only hope.: Basically any problem I’d come across I’d have to google immediately, and answers were sparse. The solution I finally ended up with was embedded ActiveX objects, which I also had a lot of trouble with, hence this blog post.
So, here’s an overview of what I did, and how you can get it working. A small note, I’m targeting .NET 3.0.
AxInterop.ShockwaveFlashObjectsin your project. The latter assembly may only be the necessary one, but back in the 2.0 world the forms designer added both. These DLLs will be included in the download project, but you can usually find them in the COM tab of the Add Reference dialog.
- For whatever
Windowyou want to show this on, set up a
WindowsFormsHostobject as well as a
AxShockwaveFlashobject. (Make sure to include a
using AxShockwaveFlashObjects;as well)
- In the
Loadedevent for your
Window, NOT the constructor, set up these objects and set the
Childproperty of the
AxShockwaveFlashobject you made. If you don’t do it in this order, or do this in the constructor, ActiveX gets quite angry at you.
- Make sure to add the
Window…this is one of those gotchas that can save you some time.
- Now you can set the
Movieproperty of the flash player, and call
Play()and it should work fine. If not, leave a comment and I’ll help you out.
Here’s an example project for you to get started with: