使用c#asp.net进行Mp4流媒体播放

时间:2017-07-10 07:28:11

标签: c# asp.net stream video-streaming mp4

我正在开发一个用c#asp.net制作mp4流的系统。基于Chromium的浏览器可以毫无错误地流式传输视频,但firefox和Internet Explorer无法流式传输视频。我用字节数组响应http请求(部分206)。这里是代码问题来自哪里 感谢。

    /// 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");
        }

1 个答案:

答案 0 :(得分:0)

如何设置请求标头'Range'。请检查一下。