Through the sun-lit window, I watched the white snowflakes drifting from the sky. Aerith, my dauther, was using her fingers to test the refreshingly cold window. Since it was her first birthday, I let her be. As long as she's not licking the window, she could do no harm.
As guests gathered, my wife started to play some flash movies for Aerith. Of course, we all know how to play them. Open a folder and click on the movie...its as easy as that. As the merry tone of "Ali Baba's Farm" rolled along the with flash movie, Aerith started to rock back'n forth with it.
Except.... 45 seconds later, the movie ended. Aerith started to cry and my wife jumped in to rescue. As "Beanbag Beanbag", "Bingo" were played, wife again and again had to click on the movies. And this isn't even the worst. The worst is that when she clicks on the movie, IE will start playing ... NOT! It asks you if you want to play this movie with a little pop-blocker!
So I created an Aerith player - a winform that would play Flash Movie through flash api. It allows a user to play flash movies in a winform, and once it starts playing, it will continue to play all the flash movies one after another. The user no longer has to keep clicking the next flash movie once the previous one finishes. Now both my Aerith and wife are happy.
The core of the program is to use Interop.ShockwaveFlashObjects and AxInterop.ShockwaveFlashObjects. After adding reference to these two dll, flash player control can be used in Winform.
Once the control is added, you need to do three steps: Init player, load movie, end movie.
Step 1. Initiate the flash player control and add it to the winform
this.Controls.Remove(axShockwaveFlash1);
axShockwaveFlash1.Dispose();
axShockwaveFlash1 = new AxShockwaveFlashObjects.AxShockwaveFlash();
axShockwaveFlash1.BeginInit();
axShockwaveFlash1.Location = new Point(50, 80);
axShockwaveFlash1.Name = "FlashPlayer";
axShockwaveFlash1.TabIndex = 0;
// Dynamically figure out the size of the flash player. Shrink it to 80% of all available screen space.
axShockwaveFlash1.Size = new Size(Convert.ToInt32(Screen.PrimaryScreen.Bounds.Width*0.8), Convert.ToInt32(Screen.PrimaryScreen.Bounds.Height*0.8));
axShockwaveFlash1.EndInit();
this.Controls.Add(axShockwaveFlash1);
Step 2: Load the movie. Skip files that are not swf.
if (fi_list[_nextMovie].Extension.Equals(".swf"))
{
axShockwaveFlash1.LoadMovie(0, di.FullName + @"\" + fi_list[_nextMovie].Name);
FileNameBox.Text = fi_list[_nextMovie].Name;
axShockwaveFlash1.OnReadyStateChange += new AxShockwaveFlashObjects._IShockwaveFlashEvents_OnReadyStateChangeEventHandler (axShockwaveFlash1_OnReadyStateChange);
_currentFrame = axShockwaveFlash1.FrameNum;
Step 3: The trick to figure out when the flash movie ended is to do the following:
1. Setting up a timer
2. Call TotalFrames in axShockwaveFlash to see the total frame count
3. Use the timer to update the application and see the current frame being played.
4. If the currentframe being played is (total frame - 1), then the application has reached the end of the flash file.
5. Set the timer interval to be 33 milliseconds. Because 12 frames is roughly played for each second, every frame will take roughly 83 milliseconds to play.
_timer.Interval = 83;
_timer.Tick += new EventHandler(_timer_Tick);
_timer.Start();
6. However, some flash movies are constructed incorrectly, so the total frame count is far greater than the frame that has actual content.
For example, a flash movie has a total of 3000 frames; however, it ends at 2000 frames. the timer trick then will never reach the end of the frame.
So something else has to be done here.
7. Another problem is that some flash movies will get stuck at certain frames due to a corrupted file or bad programming.
There are ways to handle this but the simplest method might be to just skip over the bad movie.
axShockwaveFlash1.Play();
_movieStartTime = DateTime.Now;
For more detail, you can download the Aerith Player project with this link: Aerith Flash Player