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 WebBrowser control blows.
My first attempt was to add an embed tag into a WebBrowser control, which is put inside of a WindowsFormsHost
control, 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.
- Reference Interop.ShockwaveFlashObjects and AxInterop.ShockwaveFlashObjects
in 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 UserControl or Window you want to show this on, set up a WindowsFormsHost object as well as a AxShockwaveFlash object. (Make sure to include a using AxShockwaveFlashObjects; as well)
- In the Loaded event for your UserControl/Window, NOT the constructor, set up these objects and set the Child property of the WindowsFormsHost to the AxShockwaveFlash object 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 WindowsFormsHost to the UserControl/Window...this is one of those gotchas that can save you some time.
- Now you can set the Movie property 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: