无法使用ASP.net处理程序

时间:2016-02-17 13:44:40

标签: asp.net ipad video-streaming html5-video httphandler

我试图通过asp.net处理程序使用HTML5播放视频。它在windows& mac chrome但不适用于iPad / iPhone和iPad mac safari。我尝试了大多数解决方案,比如请求块和更多,但没有用。我的代码如下。



<video controls="true">
  <source id="mp4" src="~/ImageHandler/Handler1.ashx?location=785f.mp4" type="video/mp4"/>
 </video>
&#13;
&#13;
&#13;

处理程序:

public void ProcessRequest(HttpContext context)
{

    ImageFileDao = NhibernateRepository.ImageFileDao;
    ImageStorageService = NhibernateRepository.ImageStorageService;
    string filename = context.Request["location"];
    string location = filename.Contains("\\") ? filename.Split('\\')[2] : filename;
    string firstDir = location.Substring(0, 2);
    string secondDir = location.Substring(2, 2);
    string fullpath = Path.Combine(Path.Combine(ImageStorageService.ImageStorageDir, firstDir), secondDir) + "\\" + location;
    string mimetype = "video/mp4";
    if (System.IO.File.Exists(fullpath))
    {

        context.Response.ContentType = mimetype;
        if (!String.IsNullOrEmpty(context.Request.ServerVariables["HTTP_RANGE"]))
        {
            RangeDownload(fullpath, context);
        }
        else
        {
            long fileLength = File.OpenRead(fullpath).Length;
            context.Response.AddHeader("Content-Length", fileLength.ToString());
            context.Response.WriteFile(fullpath);
        }
    }
    else
    {
        throw new HttpException(404, "Video Not Found Path:" + fullpath);
    }
}


private void RangeDownload(string fullpath, HttpContext context)
{
    long size, start, end, length, fp = 0;
    using (StreamReader reader = new StreamReader(fullpath))
    {

        size = reader.BaseStream.Length;
        start = 0;
        end = size - 1;
        length = size;
        context.Response.AddHeader("Accept-Ranges", "0-" + size);
        if (!String.IsNullOrEmpty(context.Request.ServerVariables["HTTP_RANGE"]))
        {
            long anotherStart = start;
            long anotherEnd = end;
            string[] arr_split = context.Request.ServerVariables["HTTP_RANGE"].Split(new char[] { Convert.ToChar("=") });
            string range = arr_split[1];

            if (range.IndexOf(",") > -1)
            {
                context.Response.AddHeader("Content-Range", "bytes " + start + "-" + end + "/" + size);
                throw new HttpException(416, "Requested Range Not Satisfiable");

            }

            if (range.StartsWith("-"))
            {
                anotherStart = size - Convert.ToInt64(range.Substring(1));
            }
            else
            {
                arr_split = range.Split(new char[] { Convert.ToChar("-") });
                anotherStart = Convert.ToInt64(arr_split[0]);
                long temp = 0;
                anotherEnd = (arr_split.Length > 1 && Int64.TryParse(arr_split[1].ToString(), out temp)) ? Convert.ToInt64(arr_split[1]) : size;
            }
            if (anotherStart > anotherEnd || anotherStart > size - 1 || anotherEnd >= size)
            {

                context.Response.AddHeader("Content-Range", "bytes " + start + "-" + end + "/" + size);
                throw new HttpException(416, "Requested Range Not Satisfiable");
            }
            start = anotherStart;
            end = anotherEnd;

            length = end - start + 1; 
            fp = reader.BaseStream.Seek(start, SeekOrigin.Begin);
            context.Response.StatusCode = 206;
        }
    }

    context.Response.AddHeader("Content-Range", "bytes " + start + "-" + end + "/" + size);
    context.Response.AddHeader("Content-Length", length.ToString());
    context.Response.WriteFile(fullpath, fp, length);
    context.Response.End();

}

注意:我正在获取&context 39.Request.ServerVariables [&#34; HTTP_RANGE&#34;]&#39;永远无法使用“设备”,也许这也是其中一个原因。

请提供宝贵的解决方案。公平的建议也很愉快地接受。提前致谢

0 个答案:

没有答案