我是 asp.net 的新手,我尝试使用 ashx 流式传输 mp4 文件,它适用于桌面上的某些浏览器,但 idevices 可以播放它。我四处搜索,很多解决方案都提到了范围标头,我尝试了Martin-Brennan先生的解决方案,但它仍然不适用于我:(,这是我的代码。对不起我的英语,谢谢!
对了,我想问一下视频的rewrite url,我的video url是http://mydomain/video/test.mp4
可以重写的http://mydomain/a23sdsf21ds/test.mp4
,我不能用扩展重写url
//First, accept Range headers.
context.Response.AddHeader("Accept-Ranges", "bytes");
context.Response.ContentType = "video/mp4";
//Then, read all of the bytes from the file you are requesting.
var file_info = new System.IO.FileInfo(context.Server.MapPath("~/media/test.mp4"));
var bytearr = File.ReadAllBytes(file_info.FullName);
//Then, you will need to check for a range header, and then serve up a 206 Partial Content status code in your response.
var startbyte = 0;
if (!string.IsNullOrEmpty(context.Request.Headers["Range"]))
{
var cs = new Char[] { '=', '-' };
//Get the actual byte range from the range header string, and set the starting byte.
var range = context.Request.Headers["Range"].Split(cs);
startbyte = (int)Convert.ToInt64(range[1]);
//Set the status code of the response to 206 (Partial Content) and add a content range header.
context.Response.StatusCode = 206;
context.Response.AddHeader("Content-Range", String.Format(" bytes {0}-{1}/{2}", startbyte, bytearr.Length - 1, bytearr.Length));
}
//Finally, write the video file to the output stream, starting from the specified byte position.
context.Response.OutputStream.Write(bytearr, startbyte, bytearr.Length - startbyte);