【发布时间】:2017-07-10 07:28:10
【问题描述】:
我正在开发一个使用 c#asp.net 制作 mp4 流的系统。基于 Chromium 的浏览器可以毫无错误地流式传输视频,但 Firefox 和 Internet Explorer 无法流式传输视频。我用字节数组(部分 206)响应 http 请求。这是代码问题出在哪里 谢谢。
/// source code
/// Base on
/// http://videostreamer.codeplex.com/
int KBps;
switch (res)
{
case NResolution.HD1080p:
KBps = 1250000;
break;
case NResolution.HD720p:
KBps = 750000; break;
case NResolution.HD480p:
KBps = 750000; break;
case NResolution.HD320p:
KBps = 400000; break;
case NResolution.HD144p:
KBps = 200000; break;
default:
KBps = 200000; break;
}
using (var reader = new FileStream(fullpath,FileMode.Open,FileAccess.Read,FileShare.Read,KBps,FileOptions.SequentialScan))
{
var size = reader.Length;
var end = size - 1;
var start = 0;
Int64 anotherStart = 0;
Int64 anotherEnd = 0;
resp.AddHeader("Accept-Ranges","0-"+size.ToString());
resp.StatusCode = 206;
var rng = req.Headers.Get("Range");
if (!string.IsNullOrEmpty(rng))
{
if (rng.IndexOf(",") > -1)
{
resp.AddHeader("Content-Range","bytes " + start + "-" + end +"/" + size);
throw new HttpException(416,"Error...");
}
if (rng.StartsWith("-"))
{
anotherStart = Int64.Parse(rng.Substring(1));
}
else
{
anotherStart = Int64.Parse(rng.Split('=')[1].Split('-')[0]);
}
if (anotherStart + KBps <= end)
{
anotherEnd = anotherStart + KBps;
}
else
{
anotherEnd = end;
}
var length = anotherEnd - anotherStart + 1 ;
resp.AddHeader("Content-Range","bytes " + anotherStart + "-" +anotherEnd + "/" + size);
resp.AddHeader("Content-Length",length.ToString());
var buf = new byte[length];
reader.Seek(anotherStart, SeekOrigin.Begin);
reader.Read(buf, 0, buf.Length);
resp.OutputStream.Write(buf,0,buf.Length);
resp.Flush();
resp.End();
}
resp.AddHeader("Content-Range", "bytes " + start + "-" + end + "/" + size);
resp.AddHeader("Content-Length", "0");
}
【问题讨论】:
标签: c# asp.net stream video-streaming mp4