Hosting Flash Movie in WPF (Part 2): Some ‘Strictly Microsoft Technology Please’ options
Few weeks back I posted a blog entry about hosting a Flash movie in a WPF project. There are situations when a project team may want to leverage the investments done in Flash movie development, but may want to strictly stay within the Microsoft technology stack for executing the entire project.
Here are a few options that the team can look at.
1. If the Flash movie is small and does not involve, say, more than 150 frames, the team can look at converting important frames of the Flash movie into XAML by using a Flash Movie to XAML convertor like SWF2XAML. The tool has some limitations but for basic stuff, this approach should work.
2. For longer and more complicated movies the best option is to convert the Flash movie (.swf) into the Microsoft promoted Windows Movie (.wmv) format and then host this converted movie using the MediaElement control.
Several free conversion softwares are available on the net, but for professional results that keep integrity of the original Flash movie, it may be necessary to acquire a good conversion software. Amongst a few vendors I have looked at in past, the Flash-to-Video-Encoder-Pro product by GeoVid gave the best results.
Once you have this well converted movie, there is a lot you can do with it when you host it through the MediaElement control. Here are some tricks that worked for me:
1. In XAML code, make sure that you set LoadedBehavior="Manual"so that you can manipulate the start, stop and pause of the movie programmatically.
2. Ensure that scrubbingEnabled="True" to allow the MediaElement control update frames for seek operation while paused.
3. You can specify the ‘source’ of the movie in XAML but I used codebehind to get more flexiblity about where the movie file is stored. Using the System.Environment.CurrentDirectory approach gives you the freedom not have the source destination hardcoded.
4. There are two ways to control the actual playback of the movie, through Storyboards that control the Mediaelement or through codebehind file – again the more felxible approach.
5. Since the specifc project needed more flexibility around interactive control of the media element, I used the codebehind approach.
6. Sometimes, the movie you have has some blank or black screens at the beginning and you do not want the default screen of the applicaiotn to look blank. To roll forward the movie by a few screens to reach a point where the screen makes more sense as a default screen, use code like
mediaElement1.Play();
System.Threading.Thread.Sleep(50);
mediaElement1.Pause();
within the MediaOpened event handler.
7. To start from a specific point of time in the playback length of the movie file, use the powerful ‘position’ property of the MediaElement control. For example, to set your start point for move playback at 3 seconds into the beginning f the movie, use a code bit like
mediaElement1.Position = new TimeSpan(0, 0, 0, 0, 3000);
8. To start and play the movie for a span of, say 5 seconds, use a code bit like
mediaElement1.Play();
System.Threading.Thread.Sleep(5000);
mediaElement1.Pause();
I found this codebehind based approach to control the start, pause, resume and stop the movie at any point a lot more relaible compared to the storyboard based approach.
