Click here to Skip to main content
15,885,546 members
Articles / Programming Languages / C#

Fun with C# and the Flash Player 8 External API

Rate me:
Please Sign up or sign in to vote.
4.72/5 (31 votes)
19 Oct 20054 min read 542.2K   13.8K   172  
Demonstrates how to communicate between Flash Player 8 and C# using the new External API
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
using System.Xml;
using AxShockwaveFlashObjects;

namespace Vml.FLVPlayer
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class FLVPlayer : System.Windows.Forms.Form
	{
        private StatusBar statusbar;
        private StatusBarPanel fileNameStatusBarPanel;
		private System.Windows.Forms.Panel videoPlaceholder;
		private System.Windows.Forms.Button openVideo;
		private System.Windows.Forms.OpenFileDialog openVideoDialog;
		private AxShockwaveFlashObjects.AxShockwaveFlash flashPlayer;

		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public FLVPlayer()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

            try
            {
                flashPlayer.LoadMovie(0, Application.StartupPath + "\\player.swf");
				flashPlayer.FlashCall += new _IShockwaveFlashEvents_FlashCallEventHandler(flashPlayer_FlashCall);
            }
            catch(Exception ex)
            {
                ExceptionUtilities.DisplayException("Unable to load SWF video player, please verify you have Flash Player 8 installed and try again.");
                this.Dispose();
            }
		}

        public FLVPlayer(string moviePath):this()
        {
            this.LoadVideo(moviePath);
        }

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(FLVPlayer));
			this.openVideo = new System.Windows.Forms.Button();
			this.openVideoDialog = new System.Windows.Forms.OpenFileDialog();
			this.statusbar = new System.Windows.Forms.StatusBar();
			this.fileNameStatusBarPanel = new System.Windows.Forms.StatusBarPanel();
			this.videoPlaceholder = new System.Windows.Forms.Panel();
			this.flashPlayer = new AxShockwaveFlashObjects.AxShockwaveFlash();
			((System.ComponentModel.ISupportInitialize)(this.fileNameStatusBarPanel)).BeginInit();
			this.videoPlaceholder.SuspendLayout();
			((System.ComponentModel.ISupportInitialize)(this.flashPlayer)).BeginInit();
			this.SuspendLayout();
			// 
			// openVideo
			// 
			this.openVideo.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.openVideo.Location = new System.Drawing.Point(8, 272);
			this.openVideo.Name = "openVideo";
			this.openVideo.Size = new System.Drawing.Size(88, 23);
			this.openVideo.TabIndex = 1;
			this.openVideo.Text = "Open...";
			this.openVideo.Click += new System.EventHandler(this.openVideo_Click);
			// 
			// statusbar
			// 
			this.statusbar.Location = new System.Drawing.Point(0, 298);
			this.statusbar.Name = "statusbar";
			this.statusbar.Panels.AddRange(new System.Windows.Forms.StatusBarPanel[] {
																						 this.fileNameStatusBarPanel});
			this.statusbar.ShowPanels = true;
			this.statusbar.Size = new System.Drawing.Size(338, 22);
			this.statusbar.SizingGrip = false;
			this.statusbar.TabIndex = 4;
			// 
			// fileNameStatusBarPanel
			// 
			this.fileNameStatusBarPanel.Text = "File not loaded";
			this.fileNameStatusBarPanel.ToolTipText = "The file currently loaded";
			this.fileNameStatusBarPanel.Width = 810;
			// 
			// videoPlaceholder
			// 
			this.videoPlaceholder.BackColor = System.Drawing.Color.Black;
			this.videoPlaceholder.Controls.Add(this.flashPlayer);
			this.videoPlaceholder.Location = new System.Drawing.Point(8, 8);
			this.videoPlaceholder.Name = "videoPlaceholder";
			this.videoPlaceholder.Size = new System.Drawing.Size(320, 240);
			this.videoPlaceholder.TabIndex = 6;
			// 
			// flashPlayer
			// 
			this.flashPlayer.ContainingControl = this;
			this.flashPlayer.Enabled = true;
			this.flashPlayer.Location = new System.Drawing.Point(0, 0);
			this.flashPlayer.Name = "flashPlayer";
			this.flashPlayer.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("flashPlayer.OcxState")));
			this.flashPlayer.Size = new System.Drawing.Size(40, 24);
			this.flashPlayer.TabIndex = 0;
			// 
			// FLVPlayer
			// 
			this.AccessibleName = "FLVPlayer";
			this.AllowDrop = true;
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(338, 320);
			this.Controls.Add(this.statusbar);
			this.Controls.Add(this.openVideo);
			this.Controls.Add(this.videoPlaceholder);
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
			this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
			this.MaximizeBox = false;
			this.Name = "FLVPlayer";
			this.Text = "FLVPlayer";
			this.DragDrop += new System.Windows.Forms.DragEventHandler(this.FLVPlayer_DragDrop);
			this.DragEnter += new System.Windows.Forms.DragEventHandler(this.FLVPlayer_DragEnter);
			((System.ComponentModel.ISupportInitialize)(this.fileNameStatusBarPanel)).EndInit();
			this.videoPlaceholder.ResumeLayout(false);
			((System.ComponentModel.ISupportInitialize)(this.flashPlayer)).EndInit();
			this.ResumeLayout(false);

		}
		#endregion

        private void openVideo_Click(object sender, System.EventArgs e)
        {
            openVideoDialog = new OpenFileDialog();
            openVideoDialog.Filter = "*.flv|*.flv";
            openVideoDialog.Title = "Select a Flash Video file...";
            openVideoDialog.Multiselect = false;
            openVideoDialog.RestoreDirectory = true;

            if(openVideoDialog.ShowDialog() == DialogResult.OK)
            {
                LoadVideo(openVideoDialog.FileName);
            }
        }

        private void LoadVideo(string videoPath)
        {
            fileNameStatusBarPanel.Text = videoPath;
			flashPlayer.CallFunction("<invoke name=\"loadAndPlayVideo\" returntype=\"xml\"><arguments><string>" + videoPath + "</string></arguments></invoke>");
        }

        private void FLVPlayer_DragDrop(object sender, System.Windows.Forms.DragEventArgs e)
        {
            string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);

            if(fileNameStatusBarPanel.Text != files[0])
            {
                LoadVideo(files[0]);
            }
        }

        private void FLVPlayer_DragEnter(object sender, System.Windows.Forms.DragEventArgs e)
        {
            if(e.Data.GetDataPresent(DataFormats.FileDrop, false))
            {
                e.Effect = DragDropEffects.All;
            }
        }

//        private void btnUrl_Click(object sender, System.EventArgs e)
//        {
//            UrlDialog urlForm = new UrlDialog();
//
//            if(urlForm.ShowDialog(this) == DialogResult.OK)
//            {
//                if(urlForm.Url != null && urlForm.Url.Length > 0)
//                {
//                    LoadVideo(urlForm.Url);
//                }
//            }
//        }

		public void ResizePlayer(int width, int height)
		{
			flashPlayer.Width = width;
			flashPlayer.Height = height;
			videoPlaceholder.Width = width;
			videoPlaceholder.Height = height;	
		}

		private void flashPlayer_FlashCall(object sender, _IShockwaveFlashEvents_FlashCallEvent e)
		{
			XmlDocument document = new XmlDocument();
			document.LoadXml(e.request);
			
			// Since I have only one call back I just grab the arguments and call
			// the function.  This needs to be made much more flexible when there are
			// multiple call backs going back and forth
			XmlNodeList list = document.GetElementsByTagName("arguments");
			ResizePlayer(Convert.ToInt32(list[0].FirstChild.InnerText), Convert.ToInt32(list[0].ChildNodes[1].InnerText));
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States
.NET Software Developer

Education
- Working towards M.S. Computer Science
- B.S. Computer Science
- B.S. Computer Information Systems
- Minor Mathematics

Professional
- Microsoft Certified Application Developer
- Microsoft Certified Professional
- C# ASP.NET Developer (4 years)

Other Interests
- Flash Remoting
- Video Encoding (Windows Media and Flash Video)
- Microsoft Content Management Server

More Programming Thoughts and Tips
- http://gabewishnie.blogspot.com

Comments and Discussions