一个媒体站点少不了播放Flash视频,此博文Insus.NET教你实现。
由于一个站点也许不止一个地方需要播放flash视频,为了简化代码,因此Insus.NET想把这个播放控件,写入用户控件内,在网页需要时,拉进去并给用户控件赋值即可。
建立一个用户控件SwfPlayer.ascx:
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="SwfPlayer.ascx.cs" Inherits="SwfPlayer" %>
SwfPlayer.ascx.cs:
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行。
View Code
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事件内给用户控件属性赋值:
View Code
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;
}
}
网页浏览效果: