2010-09-02 80 views
4

有沒有一種很好的方法通過asp.net將視頻流式傳輸到普通的網頁和移動設備上?我嘗試了以下方法,但在我的索尼愛立信K810i上無法使用。當我在瀏覽器中嘗試它時,我可以看到剪輯(不知道它是否是流式傳輸)。使用ASP.NET流媒體視頻

HTML:

<object type="video/3gpp" 
     data="handlers/FileHandler.ashx" 
     id="player" 
     width="176" 
     height="148" 
     autoplay="true"></object> 

FileHandler.ashx(Best way to stream files in ASP.NET):

public void ProcessRequest (HttpContext context) { 

    string path = "~/files/do.3gp"; 

    string localPath = context.Server.MapPath(path); 

    if (!File.Exists(localPath)) 
    { 
     return; 
    } 

    // get info about contenttype etc 
    FileInfo fileInfo = new FileInfo(localPath); 
    int len = (int)fileInfo.Length; 
    context.Response.AppendHeader("content-length", len.ToString()); 
    context.Response.ContentType = FileHelper.GetMimeType(fileInfo.Name); // returns video/3gpp 

    // stream file 
    byte[] buffer = new byte[1 << 16]; // 64kb 
    int bytesRead = 0; 
    using(var file = File.Open(localPath, FileMode.Open)) 
    { 
     while((bytesRead = file.Read(buffer, 0, buffer.Length)) != 0) 
     { 
      context.Response.OutputStream.Write(buffer, 0, bytesRead); 
     } 
    } 

    // finish 
    context.Response.Flush(); 
    context.Response.Close(); 
    context.Response.End(); 

} 

回答

6

什麼你得是不是 「技術上」 流。這是一個文件下載。您的客戶端(瀏覽器/電話)發送了一個HTTP請求,您的FileHandler.ashx打開了該文件並將這些字節寫入響應流。這與網頁請求的交互完全相同,除了數據是html文本而不是表示視頻的二進制數據。

如果手機不支持視頻,它可能是不兼容的編碼。如果您確定該視頻可以通過手機播放,請查看該手機是否需要漸進式下載支持(例如,iPhone/iPad/iPod Touch需要媒體播放器「流式播放」視頻)。如果是這樣,您會需要查看大量可用於處理字節範圍數據請求的解決方案,並使用指定範圍內的文件中的字節響應請求。

我寫了一個library for ASP.NET MVC來解決這個問題,我的工作主要是基於這個guidance and source code完成的。