asp.net 中插入flash
最近写了一个关于在asp.net 下,播放flash 的东西。其实,这东西大家都很熟悉,网上关于此类的代码也很多,但是请注意下边代码的红色注释,flash文件为本工程下的目录。
如:
如:
<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0"
width="575" height="375" align="middle">
<param name="movie" value='/flashs/flash.swf''> 注意:此处为该工程下的相对目录
<param name="quality" value="high">
<param name="wmode" value="Transparent" />
<param name="menu" value="false" />
<param name="FlashVars" value="init=yes&check=true" />
<embed src='/flashs/flash.swf''' ——注意:此处为该工程下的相对目录
quality="high" wmode="transparent" width="575" height="375"
name="fileUpload" align="middle" allowscriptaccess="sameDomain" type="application/x-shockwave-flash"
pluginspage="http://www.macromedia.com/go/getflashplayer" /></object>
可是如果要是工程外部的目录或者是其他的磁盘映射,这么是无法工作的。那怎么办呢!功夫不负我这样的有心人,终于在网上找到一关于把flash 转换成stream 的一个方法,经过一点小的改动完成。
如下:
FLVStreamHandler 代码:
<%@ WebHandler Language="C#" Class="FLVStreamHandler" %>
using System;
using System.Web;
using System.IO;
using Tribal.Tsite.API;
using Tribal.Tsite.Implementation;
public class FLVStreamHandler : IHttpHandler {
private static readonly byte[] _flvheader = HexToByte("464C5601010000000900000009"); //"FLV\x1\x1\0\0\0\x9\0\0\0\x9"
public void ProcessRequest (HttpContext context)
{
try
{
int pos;
int length;
// Check start parameter if present
//string filename = Path.GetFileName(context.Request.FilePath);
int flashId = int.Parse(context.Request.QueryString["Id"]);
MainController mainController = MainController.GetInstance();
IFlashFile flashFile = mainController.FlashFileController.GetFlashFileById(flashId);
string flashPath = "";
if (File.Exists(flashFile.FilePath + flashFile.FileName))
flashPath=flashFile.FilePath + flashFile.FileName;
else
flashPath = context.Server.MapPath("~/App_Themes/Normal/Images/default.swf");
using (FileStream fs = new FileStream(flashPath, FileMode.Open, FileAccess.Read, FileShare.Read))
{
string qs = context.Request.Params["start"];
if (string.IsNullOrEmpty(qs))
{
pos = 0;
length = Convert.ToInt32(fs.Length);
}
else
{
pos = Convert.ToInt32(qs);
length = Convert.ToInt32(fs.Length - pos) + _flvheader.Length;
}
// Add HTTP header stuff:clear cache, content type and length
context.Response.Cache.SetCacheability(HttpCacheability.NoCache);//注意:如果每次打开都不变换flash文件,请使用缓存
//context.Response.Cache.SetCacheability(HttpCacheability.Public);
context.Response.Cache.SetExpires(DateTime.Now.AddDays(-1));
context.Response.Cache.SetLastModified(DateTime.Now);
context.Response.AppendHeader("Content-Type", "video/x-flv");
context.Response.AppendHeader("Content-Length", length.ToString());
// Append FLV header when sending partial file
if (pos > 0)
{
context.Response.OutputStream.Write(_flvheader, 0, _flvheader.Length);
fs.Position = pos;
}
// Read buffer and write stream to the response stream
const int buffersize = 16384;
byte[] buffer = new byte[buffersize];
int count = fs.Read(buffer, 0, buffersize);
while (count > 0)
{
if (context.Response.IsClientConnected)
{
context.Response.OutputStream.Write(buffer, 0, count);
count = fs.Read(buffer, 0, buffersize);
}
else
{
count = -1;
}
}
}
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.ToString());
}
}
public bool IsReusable
{
get
{
return false;
}
}
private static byte[] HexToByte(string hexString)
{
byte[] returnBytes = new byte[hexString.Length / 2];
for (int i = 0; i < returnBytes.Length; i++)
returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);
return returnBytes;
}
}
前台代码:
width="575" height="375" align="middle">
<param name="movie" value='/flashs/flash.swf''> 注意:此处为该工程下的相对目录
<param name="quality" value="high">
<param name="wmode" value="Transparent" />
<param name="menu" value="false" />
<param name="FlashVars" value="init=yes&check=true" />
<embed src='/flashs/flash.swf''' ——注意:此处为该工程下的相对目录
quality="high" wmode="transparent" width="575" height="375"
name="fileUpload" align="middle" allowscriptaccess="sameDomain" type="application/x-shockwave-flash"
pluginspage="http://www.macromedia.com/go/getflashplayer" /></object>
可是如果要是工程外部的目录或者是其他的磁盘映射,这么是无法工作的。那怎么办呢!功夫不负我这样的有心人,终于在网上找到一关于把flash 转换成stream 的一个方法,经过一点小的改动完成。
如下:
FLVStreamHandler 代码:
<%@ WebHandler Language="C#" Class="FLVStreamHandler" %>
using System;
using System.Web;
using System.IO;
using Tribal.Tsite.API;
using Tribal.Tsite.Implementation;
public class FLVStreamHandler : IHttpHandler {
private static readonly byte[] _flvheader = HexToByte("464C5601010000000900000009"); //"FLV\x1\x1\0\0\0\x9\0\0\0\x9"
public void ProcessRequest (HttpContext context)
{
try
{
int pos;
int length;
// Check start parameter if present
//string filename = Path.GetFileName(context.Request.FilePath);
int flashId = int.Parse(context.Request.QueryString["Id"]);
MainController mainController = MainController.GetInstance();
IFlashFile flashFile = mainController.FlashFileController.GetFlashFileById(flashId);
string flashPath = "";
if (File.Exists(flashFile.FilePath + flashFile.FileName))
flashPath=flashFile.FilePath + flashFile.FileName;
else
flashPath = context.Server.MapPath("~/App_Themes/Normal/Images/default.swf");
using (FileStream fs = new FileStream(flashPath, FileMode.Open, FileAccess.Read, FileShare.Read))
{
string qs = context.Request.Params["start"];
if (string.IsNullOrEmpty(qs))
{
pos = 0;
length = Convert.ToInt32(fs.Length);
}
else
{
pos = Convert.ToInt32(qs);
length = Convert.ToInt32(fs.Length - pos) + _flvheader.Length;
}
// Add HTTP header stuff:clear cache, content type and length
context.Response.Cache.SetCacheability(HttpCacheability.NoCache);//注意:如果每次打开都不变换flash文件,请使用缓存
//context.Response.Cache.SetCacheability(HttpCacheability.Public);
context.Response.Cache.SetExpires(DateTime.Now.AddDays(-1));
context.Response.Cache.SetLastModified(DateTime.Now);
context.Response.AppendHeader("Content-Type", "video/x-flv");
context.Response.AppendHeader("Content-Length", length.ToString());
// Append FLV header when sending partial file
if (pos > 0)
{
context.Response.OutputStream.Write(_flvheader, 0, _flvheader.Length);
fs.Position = pos;
}
// Read buffer and write stream to the response stream
const int buffersize = 16384;
byte[] buffer = new byte[buffersize];
int count = fs.Read(buffer, 0, buffersize);
while (count > 0)
{
if (context.Response.IsClientConnected)
{
context.Response.OutputStream.Write(buffer, 0, count);
count = fs.Read(buffer, 0, buffersize);
}
else
{
count = -1;
}
}
}
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.ToString());
}
}
public bool IsReusable
{
get
{
return false;
}
}
private static byte[] HexToByte(string hexString)
{
byte[] returnBytes = new byte[hexString.Length / 2];
for (int i = 0; i < returnBytes.Length; i++)
returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);
return returnBytes;
}
}
前台代码:
<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0"
width="575" height="375" align="middle">
<param name="movie" value='http://www.cnblogs.com/Handler/FLVStreamHandler.ashx?Id=<%=FlashFileId %>'>注意:通过FLVStreamHandler.ashx把fllash流化
<param name="quality" value="high">
<param name="wmode" value="Transparent" />
<param name="menu" value="false" />
<param name="FlashVars" value="init=yes&check=true" />
<embed src='http://www.cnblogs.com/Handler/FLVStreamHandler.ashx?Id=<%=FlashFileId %>' quality="high" wmode="transparent" width="575" height="375"
name="fileUpload" align="middle" allowscriptaccess="sameDomain" type="application/x-shockwave-flash"
pluginspage="http://www.macromedia.com/go/getflashplayer" /></object>
以上是我在开发时,解决asp.net 中插入flash的一种方法。
width="575" height="375" align="middle">
<param name="movie" value='http://www.cnblogs.com/Handler/FLVStreamHandler.ashx?Id=<%=FlashFileId %>'>注意:通过FLVStreamHandler.ashx把fllash流化
<param name="quality" value="high">
<param name="wmode" value="Transparent" />
<param name="menu" value="false" />
<param name="FlashVars" value="init=yes&check=true" />
<embed src='http://www.cnblogs.com/Handler/FLVStreamHandler.ashx?Id=<%=FlashFileId %>' quality="high" wmode="transparent" width="575" height="375"
name="fileUpload" align="middle" allowscriptaccess="sameDomain" type="application/x-shockwave-flash"
pluginspage="http://www.macromedia.com/go/getflashplayer" /></object>
以上是我在开发时,解决asp.net 中插入flash的一种方法。