首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> .NET 網頁程式設計 >> ASP.NET 基礎 >> 關於ASP.NET

ASP.NET 整合 FMS 建構視頻網站完整實作指南

     編輯:關於ASP.NET

本文記錄使用 ASP.NET 搭配 Adobe Flash Media Server (FMS) 開發視頻網站的完整流程,涵蓋伺服器安裝、Flash 採集控件製作、上傳邏輯、FLV 流式傳輸處理等關鍵環節。

開發四大步驟

  1. FMS 部署:下載並安裝配置 Flash Media Server,確保 RTMP 通訊協定正常運作。
  2. Flash 採集控件開發:使用 ActionScript 製作視頻採集元件,支援攝像頭存取、編碼推流至 FMS。
  3. ASPX 頁面整合上傳邏輯:嵌入 Flash 控件,處理錄製完成後的回調,將視頻檔案路徑寫入資料庫。
  4. FLV 串流播放實作:編寫 IHttpHandler 處理 FLV 檔案的範圍請求與標頭輸出,支援拖曳定位播放。

核心程式碼:FLVStreaming 處理器

public class FLVStreaming : IHttpHandler
{
    private static readonly byte[] _flvheader = HexToByte("464C5601010000000900000009");

    public void ProcessRequest(HttpContext context)
    {
        try
        {
            string filename = Path.GetFileName(context.Request.FilePath);
            using (FileStream fs = new FileStream(context.Server.MapPath(filename), FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                string qs = context.Request.Params["start"];
                int pos = string.IsNullOrEmpty(qs) ? 0 : Convert.ToInt32(qs);
                int length = string.IsNullOrEmpty(qs) ? (int)fs.Length : (int)(fs.Length - pos) + _flvheader.Length;

                context.Response.Cache.SetCacheability(HttpCacheability.Public);
                context.Response.AppendHeader("Content-Type", "video/x-flv");
                context.Response.AppendHeader("Content-Length", length.ToString());

                if (pos > 0)
                {
                    context.Response.OutputStream.Write(_flvheader, 0, _flvheader.Length);
                    fs.Position = pos;
                }

                const int buffersize = 16384;
                byte[] buffer = new byte[buffersize];
                int count = fs.Read(buffer, 0, buffersize);
                while (count > 0 && context.Response.IsClientConnected)
                {
                    context.Response.OutputStream.Write(buffer, 0, count);
                    count = fs.Read(buffer, 0, buffersize);
                }
            }
        }
        catch (Exception ex)
        {
            System.Diagnostics.Debug.WriteLine(ex.ToString());
        }
    }

    private static byte[] HexToByte(string hexString) { /* 實作略 */ }
}

關鍵技術點解析

  • 部分內容回應:透過 start 參數支援 HTTP Range 請求,實現視頻拖曳跳轉。
  • FLV 標頭注入:非完整檔案傳輸時,需手動寫入 FLV Header 確保播放器解碼正常。
  • 緩衝流式傳輸:16KB 緩衝區分塊輸出,配合 IsClientConnected 避免客戶端斷線後的資源浪費。
  • 快取策略:設定 Public 快取與 Last-Modified,優化重複播放效能。

部署與驗證

完成 web.config 處理器映射配置後,即可在頁面嵌入 Flash 播放器(如 JW Player、Flowplayer)指向 .flv 路徑測試。作者表示播放速度表現優異,提供了 ASP.NET 開發視頻網站的可行架構參考。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved