我想通過HTTP流視頻到iphone沒有與.net的流媒體服務器。 經過一些測試後,我發現如果你只是上傳iphone兼容視頻到你的服務器,iis7工作得很好,iphone會在小緩衝時間後開始播放視頻,並繼續在後臺下載。c#通過http流到iphone
我的問題是,我無法使用.net。我曾嘗試與
public static void SmallFile(string filename, string filepath, string contentType)
{
try
{
FileStream MyFileStream = new FileStream(filepath, FileMode.Open, FileAccess.Read, FileShare.Read);
long FileSize;
FileSize = MyFileStream.Length;
byte[] Buffer = new byte[(int)FileSize];
MyFileStream.Read(Buffer, 0, (int)MyFileStream.Length);
MyFileStream.Close();
HttpContext.Current.Response.ContentType = contentType;
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(filename, System.Text.Encoding.UTF8));
HttpContext.Current.Response.BinaryWrite(Buffer);
}
catch
{
HttpContext.Current.Response.ContentType = "text/html";
HttpContext.Current.Response.Write("Downloading Error! " + filename + " not found!");
}
HttpContext.Current.Response.End();
}
或
public static void ResumableFile(string filename, string fullpath, string contentType)
{
try
{
FileStream myFile = new FileStream(fullpath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
BinaryReader br = new BinaryReader(myFile);
try
{
HttpContext.Current.Response.AddHeader("Accept-Ranges", "bytes");
HttpContext.Current.Response.Buffer = false;
long fileLength = myFile.Length;
long startBytes = 0;
//int pack = 10240; //10K bytes
int pack = 1048576; //1024K bytes
if (HttpContext.Current.Request.Headers["Range"] != null)
{
HttpContext.Current.Response.StatusCode = 206;
string[] range = HttpContext.Current.Request.Headers["Range"].Split(new char[] { '=', '-' });
startBytes = Convert.ToInt64(range[1]);
}
HttpContext.Current.Response.AddHeader("Content-Length", (fileLength - startBytes).ToString());
if (startBytes != 0)
{
HttpContext.Current.Response.AddHeader("Content-Range", string.Format(" bytes {0}-{1}/{2}", startBytes, fileLength - 1, fileLength));
}
HttpContext.Current.Response.AddHeader("Connection", "Keep-Alive");
HttpContext.Current.Response.ContentType = contentType;
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(filename, System.Text.Encoding.UTF8));
br.BaseStream.Seek(startBytes, SeekOrigin.Begin);
int maxCount = (int)Math.Floor((double)((fileLength - startBytes)/pack)) + 1;
for (int i = 0; i < maxCount; i++)
{
if (HttpContext.Current.Response.IsClientConnected)
{
HttpContext.Current.Response.BinaryWrite(br.ReadBytes(pack));
}
else
{
i = maxCount;
}
}
}
catch
{
HttpContext.Current.Response.ContentType = "text/html";
HttpContext.Current.Response.Write("Downloading Error! " + filename + " not found!");
}
finally
{
br.Close();
myFile.Close();
}
}
catch
{
HttpContext.Current.Response.ContentType = "text/html";
HttpContext.Current.Response.Write("Downloading Error!" + filename + " not found!");
}
}
在這兩個,我得到錯誤說服務器的配置不正確。我然後刪除
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(filename, System.Text.Encoding.UTF8));
部分,所以它不會觸發完整下載,但結果是相同的。
我檢查了服務器的響應,當我直接下載文件時,服務器沒有不同/額外的頭文件。
什麼,我想findout這將是使iphone緩衝並開始播放視頻的時候我直接下載視頻文件,我如何與.NET
implemnet它如果youwonder,爲什麼我就是不使用IIS ,由於帶寬限制,我需要提供某種水蛭保護
任何人都有任何經驗?
UPDATE
這裏是第一請求,並且響應從IIS
GET /test.mp4 HTTP/1.1
Host: 192.168.2.200
User-Agent: Mozilla/5.0 (iPhone; U; CPU iPhone OS 3_1_2 like Mac OS X; tr-tr) AppleWebKit/528.18 (KHTML, like Gecko) Version/4.0 Mobile/7D11 Safari/528.16
Cache-Control: max-age=0
Accept: application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5
Accept-Language: tr-tr
Accept-Encoding: gzip, deflate
Connection: keep-alive
HTTP/1.1 200 OK
Content-Type: video/mp4
Last-Modified: Wed, 23 Dec 2009 20:12:57 GMT
Accept-Ranges: bytes
ETag: "e6ad9151c84ca1:0"
Server: Microsoft-IIS/7.5
X-Powered-By: ASP.NET
Date: Wed, 23 Dec 2009 21:07:19 GMT
Content-Length: 2301438
和第二請求和響應
GET /test.mp4 HTTP/1.1
Host: 192.168.2.200
Range: bytes=0-2269183
Connection: close
User-Agent: Apple iPhone OS v3.1.2 CoreMedia v1.0.0.7D11
Accept: */*
Accept-Encoding: identity
HTTP/1.1 206 Partial Content
Content-Type: video/mp4
Last-Modified: Wed, 23 Dec 2009 20:12:57 GMT
Accept-Ranges: bytes
ETag: "e6ad9151c84ca1:0"
Server: Microsoft-IIS/7.5
X-Powered-By: ASP.NET
Date: Wed, 23 Dec 2009 21:11:33 GMT
Connection: close
Content-Length: 2269184
Content-Range: bytes 0-2269183/2301438
據我所知iphone請求上第二不同字節請求等。 有沒有辦法讓c#發送這些字節範圍到客戶端?
@nLL是否可以從服務器時,它顯示的更多信息說Content-Disposition頭設置時它沒有正確配置? – casperOne 2009-12-23 19:53:03
錯誤是來自iPhone。和它發生,無論內容處置組或不 – nLL 2009-12-23 19:55:44
http://developer.apple.com/iphone/library/documentation/NetworkingInternet/Conceptual/StreamingMediaGuide/Introduction/Introduction.html – SLaks 2009-12-23 20:12:18