简体   繁体   中英

Mp4 streaming with c# asp.net

I am developing a system that makes mp4 stream with c # asp.net. Chromium-based browsers can stream videos without error, but firefox and internet explorer can not stream videos. I response http request with byte array (partial 206). here is the code Where does the problem come from thanks.

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

How are you setting the request header 'Range'. Please check that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM