1. 程式人生 > >請問如何動態實現播放語音檔案[WEB學習示例]

請問如何動態實現播放語音檔案[WEB學習示例]

重新發布完整程式碼:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class 多媒體播放 : System.Web.UI.Page
{
    DateTime Date = System.DateTime.Now;
    TiShi_XinXi Ts_XinXi = new TiShi_XinXi();
    ShuJuKu_ChaoZuo SJK_CaoZuo = new ShuJuKu_ChaoZuo();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            Label2.Text = PlayMedia("Gong_Ju/《南無觀世音菩薩》.WMV");
        }

    }
    public string PlayMedia(string MediaFile)
    {
        string strScript = "";
        strScript += "<TABLE id=\"Table2\" align=\"center\" border=\"0\" runat=\"server\" width = '620' height ='450'>";  
        strScript += "<TR>";
        strScript += "<TD align=\"center\">";
        strScript += "<OBJECT id=\"mdpTrailer\" classid=\"clsid:22D6F312-B0F6-11D0-94AB-0080C74C7E95\" name=\"mdpTrailer\" width='620' height = '450'>";
        strScript += "<PARAM NAME=\"AudioStream\" VALUE=\"-1\">";
        strScript += "<PARAM NAME=\"AutoSize\" VALUE=\"-1\">";
        strScript += "<PARAM NAME=\"AutoStart\" VALUE=\"-1\">";
        strScript += "<PARAM NAME=\"AnimationAtStart\" VALUE=\"-1\">";
        strScript += "<PARAM NAME=\"AllowScan\" VALUE=\"-1\">";
        strScript += "<PARAM NAME=\"AllowChangeDisplaySize\" VALUE=\"-1\">";
        strScript += "<PARAM NAME=\"AutoRewind\" VALUE=\"0\">";
        strScript += "<PARAM NAME=\"Balance\" VALUE=\"0\">";
        strScript += "<PARAM NAME=\"BaseURL\" VALUE=\"\">";
        strScript += "<PARAM NAME=\"BufferingTime\" VALUE=\"5\">";
        strScript += "<PARAM NAME=\"CaptioningID\" VALUE=\"\">";
        strScript += "<PARAM NAME=\"ClickToPlay\" VALUE=\"-1\">";
        strScript += "<PARAM NAME=\"CursorType\" VALUE=\"0\">";
        strScript += "<PARAM NAME=\"CurrentPosition\" VALUE=\"-1\">";
        strScript += "<PARAM NAME=\"CurrentMarker\" VALUE=\"0\">";
        strScript += "<PARAM NAME=\"DefaultFrame\" VALUE=\"\">";
        strScript += "<PARAM NAME=\"DisplayBackColor\" VALUE=\"0\">";
        strScript += "<PARAM NAME=\"DisplayForeColor\" VALUE=\"16777215\">";
        strScript += "<PARAM NAME=\"DisplayMode\" VALUE=\"0\">";
        strScript += "<PARAM NAME=\"DisplaySize\" VALUE=\"4\">";
        strScript += "<PARAM NAME=\"Enabled\" VALUE=\"-1\">";
        strScript += "<PARAM NAME=\"EnableContextMenu\" VALUE=\"-1\">";
        strScript += "<PARAM NAME=\"EnablePositionControls\" VALUE=\"-1\">";
        strScript += "<PARAM NAME=\"EnableFullScreenControls\" VALUE=\"0\">";
        strScript += "<PARAM NAME=\"EnableTracker\" VALUE=\"-1\">";
        strScript += "<PARAM NAME=\"Filename\" VALUE=\""+ MediaFile + "\">";
        strScript += "<PARAM NAME=\"InvokeURLs\" VALUE=\"-1\">";
        strScript += "<PARAM NAME=\"Language\" VALUE=\"-1\">";
        strScript += "<PARAM NAME=\"Mute\" VALUE=\"0\">";
        strScript += "<PARAM NAME=\"PlayCount\" VALUE=\"1\">";
        strScript += "<PARAM NAME=\"PreviewMode\" VALUE=\"0\">";
        strScript += "<PARAM NAME=\"Rate\" VALUE=\"1\">";
        strScript += "<PARAM NAME=\"SAMILang\" VALUE=\"\">";
        strScript += "<PARAM NAME=\"SAMIStyle\" VALUE=\"\">";
        strScript += "<PARAM NAME=\"SAMIFileName\" VALUE=\"\">";
        strScript += "<PARAM NAME=\"SelectionStart\" VALUE=\"-1\">";
        strScript += "<PARAM NAME=\"SelectionEnd\" VALUE=\"-1\">";
        strScript += "<PARAM NAME=\"SendOpenStateChangeEvents\" VALUE=\"-1\">";
        strScript += "<PARAM NAME=\"SendWarningEvents\" VALUE=\"-1\">";
        strScript += "<PARAM NAME=\"SendErrorEvents\" VALUE=\"-1\">";
        strScript += "<PARAM NAME=\"SendKeyboardEvents\" VALUE=\"0\">";
        strScript += "<PARAM NAME=\"SendMouseClickEvents\" VALUE=\"0\">";
        strScript += "<PARAM NAME=\"SendMouseMoveEvents\" VALUE=\"0\">";
        strScript += "<PARAM NAME=\"SendPlayStateChangeEvents\" VALUE=\"-1\">";
        strScript += "<PARAM NAME=\"ShowCaptioning\" VALUE=\"0\">";
        strScript += "<PARAM NAME=\"ShowControls\" VALUE=\"-1\">";
        strScript += "<PARAM NAME=\"ShowAudioControls\" VALUE=\"-1\">";
        strScript += "<PARAM NAME=\"ShowDisplay\" VALUE=\"0\">";
        strScript += "<PARAM NAME=\"ShowGotoBar\" VALUE=\"0\">";
        strScript += "<PARAM NAME=\"ShowPositionControls\" VALUE=\"-1\">";
        strScript += "<PARAM NAME=\"ShowStatusBar\" VALUE=\"0\">";
        strScript += "<PARAM NAME=\"ShowTracker\" VALUE=\"-1\">";
        strScript += "<PARAM NAME=\"TransparentAtStart\" VALUE=\"0\">";
        strScript += "<PARAM NAME=\"VideoBorderWidth\" VALUE=\"0\">";
        strScript += "<PARAM NAME=\"VideoBorderColor\" VALUE=\"0\">";
        strScript += "<PARAM NAME=\"VideoBorder3D\" VALUE=\"0\">";
        strScript += "<PARAM NAME=\"Volume\" VALUE=\"-600\">";
        strScript += "<PARAM NAME=\"WindowlessVideo\" VALUE=\"0\">";
        strScript += "</OBJECT>";
        strScript += "</TD>";
        strScript += "</TR>";
        strScript += "</TABLE>";
        return strScript;
    }
}

前臺網頁:

<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="多媒體播放.aspx.cs" Inherits="多媒體播放" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
    <style type="text/css">
        .TD
        {width: 20%;}
        .denglu-td
        {
        }
        </style>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    <table class="style2">
        <tr>
            <td>
                <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
            </td>
            <td>
                 </td>
        </tr>
        <tr>
            <td>
                 </td>
            <td>
                 </td>
        </tr>
    </table>
</asp:Content>

看看另一個播放器,播放網路視訊:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class 多媒體播放 : System.Web.UI.Page
{
    DateTime Date = System.DateTime.Now;
    TiShi_XinXi Ts_XinXi = new TiShi_XinXi();
    ShuJuKu_ChaoZuo SJK_CaoZuo = new ShuJuKu_ChaoZuo();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            Label2.Text = 網路視訊檔案("http%3A%2F%2Fwww%2Egdd%2Ero%2Fflvplayer%2Fexamples%2Fvideo%2Emp4&amp;splashscreen=http://www.gdd.ro/flvplayer/examples/fast-and-furious-1.jpg");
        }

    }
    public string 網路視訊檔案(string MediaFile)
    {
        string strScript = "";
        strScript += "<TABLE id=\"Table2\" align=\"center\" border=\"0\" runat=\"server\" width = '620' height ='450'>";
        strScript += "<TR>";
        strScript += "<TD align=\"center\">";
        strScript += "<embed src=\"http://www.gdd.ro/gdd/flvplayer/gddflvplayer.swf\"";
        strScript += "flashvars=\"?&autoplay=true&sound=70&buffer=2&";
        strScript += "vdo=" + MediaFile + "\""; /*網路視訊地址*/
        strScript += "width = \"468\"";
        strScript += "height=\"328\"";
        strScript += "allowFullScreen=\"true\"";
        strScript += "quality=\"best\"";
        strScript += "wmode=\"transparent\"";
        strScript += "allowScriptAccess=\"always\"";
        strScript += "pluginspage=\"http://www.macromedia.com/go/getflashplayer\"";
        strScript += "type = \"application/x-shockwave-flash\" >";
        strScript += "</embed>";
        strScript += "</TD>";
        strScript += "</TR>";
        strScript += "</TABLE>";
        return strScript;
    }
}

來自點選開啟連結 中的生成器生成的程式碼寫成以上播放器程式碼,一時找不到合適的網路視訊檔案地址,地址直接寫在裡面了,可以自己替換有註釋的語句. 播放本地視訊檔案測試把檔案放在一個資料夾中,這裡示例為網站工程資料夾Gong_Ju中放一個檔案:美女廣告.flv,把 http://localhost:1463/GuanLan_ZiYuan/Gong_Ju/美女廣告.flv 這個地址用生成器生成複製地址作傳遞引數:Label2.Text = 網路視訊檔案("http%3A%2F%2Flocalhost%3A1463%2FGuanLan%5FZiYuan%2FGong%5FJu%2F%E7%BE%8E%E5%A5%B3%E5%B9%BF%E5%91%8A%2Eflv");就可以播放本地檔案了.
Label2.Text = 網路視訊檔案(System.Uri.EscapeDataString("http://localhost:1463/GuanLan_ZiYuan/Gong_Ju/美女廣告.flv"));