[ASP.net WebForm] 網頁播放影音語法 (含.flv檔)
要在網頁播放影音,最簡單做法就是嵌入Windows Media Player,語法如下
#region 網頁嵌入Windows Media Player影音
/// <summary>
/// 網頁嵌入Windows Media Player影音
/// </summary>
/// <param name="width">影音寬度,建議374</param>
/// <param name="height">影音高度,建議355</param>
/// <param name="httpFilePath">Http完整檔案路徑</param>
/// <returns>回傳 影音的HTML程式碼</returns>
public string MovieHtml(int width, int height, string httpFilePath)
{
string content_att = string.Empty;
content_att += "<div align='center'>";
content_att += "<OBJECT ID='ActiveMovie1' WIDTH='" + width + "' HEIGHT='"+height+"' CLASSID='CLSID:05589FA1-C356-11CE-BF01-00AA0055595A'>";
content_att += "<PARAM NAME='Version' VALUE='1'>";
content_att += "<PARAM NAME='EnableContextMenu' VALUE='-1'>";
content_att += "<PARAM NAME='ShowDisplay' VALUE='0'>";
content_att += "<PARAM NAME='ShowControls' VALUE='-1'>";
content_att += "<PARAM NAME='ShowPositionControls' VALUE='0'>";
content_att += "<PARAM NAME='ShowSelectionControls' VALUE='0'>";
content_att += "<PARAM NAME='EnablePositionControls' VALUE='-1'>";
content_att += "<PARAM NAME='EnableSelectionControls' VALUE='-1'>";
content_att += "<PARAM NAME='ShowTracker' VALUE='-1'>";
content_att += "<PARAM NAME='EnableTracker' VALUE='-1'>";
content_att += "<PARAM NAME='AllowHideDisplay' VALUE='-1'>";
content_att += "<PARAM NAME='AllowHideControls' VALUE='-1'>";
content_att += "<PARAM NAME='MovieWindowSize' VALUE='0'>";
content_att += "<PARAM NAME='FullScreenMode' VALUE='0'>";
content_att += "<PARAM NAME='AutoStart' VALUE='0'>";
content_att += "<PARAM NAME='AutoRewind' VALUE='1'>";
content_att += "<PARAM NAME='PlayCount' VALUE='99'>";
content_att += "<PARAM NAME='SelectionStart' VALUE='0'>";
content_att += "<PARAM NAME='SelectionEnd' VALUE='200.5151388'>";
content_att += "<PARAM NAME='Appearance' VALUE='1'>";
content_att += "<PARAM NAME='BorderStyle' VALUE='1'>";
content_att += "<PARAM NAME='FileName' VALUE='" + httpFilePath + "'>";
content_att += "<PARAM NAME='DisplayMode' VALUE='0'>";
content_att += "<PARAM NAME='AllowChangeDisplayMode' VALUE='-1'>";
content_att += "<PARAM NAME='DisplayForeColor' VALUE='16777215'>";
content_att += "<PARAM NAME='DisplayBackColor' VALUE='0'>";
content_att += "影音內容";
content_att += "</OBJECT>";
content_att += "</div>";
return content_att;
}
#endregion
只要是Windows Media Player支援的格式應該都可以播放(.rmvb 應該不行)
另外也有一種很特殊很特殊的,客戶想要在網頁上直接播放.flv檔(此需要透過第三方套件實現)
#region 網頁嵌入flv影音(網頁無法直接播放flv檔,開發時期,網站需要對外IP才能透過第三方播放flv)
//語法來源:http://bshadow.pixnet.net/blog/post/17173067
//網站架在IIS6的話,IIS6不認識flv副檔名,請見保哥設定:http://blog.miniasp.com/post/2008/12/03/Adding-Flash-video-FLV-MIME-Type-in-IIS.aspx
/// <summary>
/// 網頁嵌入flv影音(網頁無法直接播放flv檔,開發時期,網站需要對外IP才能透過第三方播放flv)
/// </summary>
/// <param name="width">影音寬度,建議374</param>
/// <param name="height">影音高度,建議355</param>
/// <param name="httpFilePath">Http完整檔案路徑</param>
/// <returns>回傳 flv的HTML程式碼</returns>
public string FlvHtml(int width, int height, string httpFilePath)
{
string content_att = string.Empty;
content_att =@"<object type='application/x-shockwave-flash' data='http://flv-player.net/medias/player_flv.swf' width='"+width+"' height='"+height+"'>"+
"<param name='movie' value='http://flv-player.net/medias/player_flv.swf' />"+
"<param name='allowFullScreen' value='true' />"+
"<param name='FlashVars' value='flv="+httpFilePath+"&width="+width+"&height="+height+"' />" +
//"<embed src='"+httpFilePath+"' quality='high' pluginspage='http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash' type='application/x-shockwave-flash' wmode='transparent' width='"+width+"' height='"+height+"'></embed>" +
"<span style='display:none;'>flv影音檔</span>"+
"</object>";
return content_att;
}
#endregion
使用方法:
.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Movie.aspx.cs" Inherits="Internet_Movie" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<h3>Windows Media Player</h3>
<asp:Literal ID="li_wmp" runat="server" />
<h3>.flv</h3>
<asp:Literal ID="li_flv" runat="server" />
</form>
</body>
</html>
.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Internet_Movie : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//影音檔Url
string fileUrl = @"http://www.jhepple.com/SampleMovies/niceday.wmv";
li_wmp.Text = this.MovieHtml(374, 355, fileUrl);
//flv檔Url
string FlvUrl = @"http://www.hackerdude.com/channels-test/20051210-w50s.flv";
li_flv.Text = this.FlvHtml(374, 355, FlvUrl);
}
}
#region 網頁嵌入Windows Media Player影音
/// <summary>
/// 網頁嵌入Windows Media Player影音
/// </summary>
/// <param name="width">影音寬度,建議374</param>
/// <param name="height">影音高度,建議355</param>
/// <param name="httpFilePath">Http完整檔案路徑</param>
/// <returns>回傳 影音的HTML程式碼</returns>
public string MovieHtml(int width, int height, string httpFilePath)
{
string content_att = string.Empty;
content_att += "<div align='center'>";
content_att += "<OBJECT ID='ActiveMovie1' WIDTH='" + width + "' HEIGHT='" + height + "' CLASSID='CLSID:05589FA1-C356-11CE-BF01-00AA0055595A'>";
content_att += "<PARAM NAME='Version' VALUE='1'>";
content_att += "<PARAM NAME='EnableContextMenu' VALUE='-1'>";
content_att += "<PARAM NAME='ShowDisplay' VALUE='0'>";
content_att += "<PARAM NAME='ShowControls' VALUE='-1'>";
content_att += "<PARAM NAME='ShowPositionControls' VALUE='0'>";
content_att += "<PARAM NAME='ShowSelectionControls' VALUE='0'>";
content_att += "<PARAM NAME='EnablePositionControls' VALUE='-1'>";
content_att += "<PARAM NAME='EnableSelectionControls' VALUE='-1'>";
content_att += "<PARAM NAME='ShowTracker' VALUE='-1'>";
content_att += "<PARAM NAME='EnableTracker' VALUE='-1'>";
content_att += "<PARAM NAME='AllowHideDisplay' VALUE='-1'>";
content_att += "<PARAM NAME='AllowHideControls' VALUE='-1'>";
content_att += "<PARAM NAME='MovieWindowSize' VALUE='0'>";
content_att += "<PARAM NAME='FullScreenMode' VALUE='0'>";
content_att += "<PARAM NAME='AutoStart' VALUE='0'>";
content_att += "<PARAM NAME='AutoRewind' VALUE='1'>";
content_att += "<PARAM NAME='PlayCount' VALUE='99'>";
content_att += "<PARAM NAME='SelectionStart' VALUE='0'>";
content_att += "<PARAM NAME='SelectionEnd' VALUE='200.5151388'>";
content_att += "<PARAM NAME='Appearance' VALUE='1'>";
content_att += "<PARAM NAME='BorderStyle' VALUE='1'>";
content_att += "<PARAM NAME='FileName' VALUE='" + httpFilePath + "'>";
content_att += "<PARAM NAME='DisplayMode' VALUE='0'>";
content_att += "<PARAM NAME='AllowChangeDisplayMode' VALUE='-1'>";
content_att += "<PARAM NAME='DisplayForeColor' VALUE='16777215'>";
content_att += "<PARAM NAME='DisplayBackColor' VALUE='0'>";
content_att += "影音內容";
content_att += "</OBJECT>";
content_att += "</div>";
return content_att;
}
#endregion
#region 網頁嵌入flv影音(網頁無法直接播放flv檔,開發時期,網站需要對外IP才能透過第三方播放flv)
//語法來源:http://bshadow.pixnet.net/blog/post/17173067
//網站架在IIS6的話,IIS6不認識flv副檔名,請見保哥設定:http://blog.miniasp.com/post/2008/12/03/Adding-Flash-video-FLV-MIME-Type-in-IIS.aspx
/// <summary>
/// 網頁嵌入flv影音(網頁無法直接播放flv檔,開發時期,網站需要對外IP才能透過第三方播放flv)
/// </summary>
/// <param name="width">影音寬度,建議374</param>
/// <param name="height">影音高度,建議355</param>
/// <param name="httpFilePath">Http完整檔案路徑</param>
/// <returns>回傳 flv的HTML程式碼</returns>
public string FlvHtml(int width, int height, string httpFilePath)
{
string content_att = string.Empty;
content_att = @"<object type='application/x-shockwave-flash' data='http://flv-player.net/medias/player_flv.swf' width='" + width + "' height='" + height + "'>" +
"<param name='movie' value='http://flv-player.net/medias/player_flv.swf' />" +
"<param name='allowFullScreen' value='true' />" +
"<param name='FlashVars' value='flv=" + httpFilePath + "&width=" + width + "&height=" + height + "' />" +
//"<embed src='"+httpFilePath+"' quality='high' pluginspage='http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash' type='application/x-shockwave-flash' wmode='transparent' width='"+width+"' height='"+height+"'></embed>" +
"<span style='display:none;'>flv影音檔</span>" +
"</object>";
return content_att;
}
#endregion
}
執行結果:
如果WMP要播的影片超過1MB的話,按下播放鍵後可能要等一段時間
衍伸閱讀: