一個媒體站點少不了播放Flash視頻,此博文Insus.NET教你實現。
由於一個站點也許不止一個地方需要播放flash視頻,為了簡化代碼,因此Insus.NET想把這個播放控件,寫入用戶控件內,在網頁需要時,拉進去並給用戶控件賦值即可。
建立一個用戶控件SwfPlayer.ascx:
<%
@ Control Language
=
"
C#
"
AutoEventWireup
=
"
true
"
CodeFile
=
"
SwfPlayer.ascx.cs
"
Inherits
=
"
SwfPlayer
"
%>
SwfPlayer.ascx.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class SwfPlayer : System.Web.UI.UserControl
{
private string _File;
private int _Width;
private int _Height;
// 設置Flash寬度屬性
public int Width
{
set { _Width = value; }
}
// 設置Flash高度屬性
public int Height
{
set { _Height = value; }
}
// Flash文件
public string File
{
set { _File = value; }
}
protected void Page_Load( object sender, EventArgs e)
{
}
// 重新Render方法
protected override void Render(HtmlTextWriter writer)
{
// Flash對象
StringBuilder sb = new StringBuilder();
sb.AppendFormat( " <object classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0'width='{0}' height='{1}'> ", _Width, _Height);
sb.AppendFormat( " <param name='movie' value='{0}'> ", _File);
sb.AppendFormat( " <param name='quality' value='high'> ");
sb.AppendFormat( " <embed src='{0}' quality='high' pluginspage='http://www.macromedia.com/go/getflashplayer' type='application/x-shockwave-flash' width='{1}' height='{2}'></embed> ",_File,_Width,_Height);
sb.Append( " </object> ");
writer.Write(sb.ToString());
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class SwfPlayer : System.Web.UI.UserControl
{
private string _File;
private int _Width;
private int _Height;
// 設置Flash寬度屬性
public int Width
{
set { _Width = value; }
}
// 設置Flash高度屬性
public int Height
{
set { _Height = value; }
}
// Flash文件
public string File
{
set { _File = value; }
}
protected void Page_Load( object sender, EventArgs e)
{
}
// 重新Render方法
protected override void Render(HtmlTextWriter writer)
{
// Flash對象
StringBuilder sb = new StringBuilder();
sb.AppendFormat( " <object classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0'width='{0}' height='{1}'> ", _Width, _Height);
sb.AppendFormat( " <param name='movie' value='{0}'> ", _File);
sb.AppendFormat( " <param name='quality' value='high'> ");
sb.AppendFormat( " <embed src='{0}' quality='high' pluginspage='http://www.macromedia.com/go/getflashplayer' type='application/x-shockwave-flash' width='{1}' height='{2}'></embed> ",_File,_Width,_Height);
sb.Append( " </object> ");
writer.Write(sb.ToString());
}
}
應用這個用戶控件,把它拉進網頁需要的地方即可。下列html代碼的第3行與第13行。

1
<%
@ Page Language
=
"
C#
"
AutoEventWireup
=
"
true
"
CodeFile
=
"
Default.aspx.cs
"
Inherits
=
"
_Default
"
%>
2
3 <% @ Register Src = " SwfPlayer.ascx " TagName = " SwfPlayer " TagPrefix = " uc1 " %>
4
5 <! DOCTYPE html >
6
7 < html xmlns ="http://www.w3.org/1999/xhtml" >
8 < head runat ="server" >
9 < title ></ title >
10 </ head >
11 < body >
12 < form id ="form1" runat ="server" >
13 < uc1:SwfPlayer ID ="SwfPlayer1" runat ="server" />
14 </ form >
15 </ body >
16 </ html >
2
3 <% @ Register Src = " SwfPlayer.ascx " TagName = " SwfPlayer " TagPrefix = " uc1 " %>
4
5 <! DOCTYPE html >
6
7 < html xmlns ="http://www.w3.org/1999/xhtml" >
8 < head runat ="server" >
9 < title ></ title >
10 </ head >
11 < body >
12 < form id ="form1" runat ="server" >
13 < uc1:SwfPlayer ID ="SwfPlayer1" runat ="server" />
14 </ form >
15 </ body >
16 </ html >
在.cs的Page_load事件內給用戶控件屬性賦值:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Insus.NET;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load( object sender, EventArgs e)
{
string file = ResolveUrl( " ~/FlashFiles/Wildlife.swf ");
SwfPlayer1.Width = 400;
SwfPlayer1.Height = 300;
SwfPlayer1.File = file;
}
}
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Insus.NET;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load( object sender, EventArgs e)
{
string file = ResolveUrl( " ~/FlashFiles/Wildlife.swf ");
SwfPlayer1.Width = 400;
SwfPlayer1.Height = 300;
SwfPlayer1.File = file;
}
}
網頁瀏覽效果: