开发者

ASP.NET and HTML5 Video play time

开发者 https://www.devze.com 2023-02-24 22:01 出处:网络
Good afternoon stackoverflow! Currently I\'m working on a handler to serve videos from a database to a control that will display it using HTML5. Using Response.OutputStream.Write does indeed work, ho

Good afternoon stackoverflow!

Currently I'm working on a handler to serve videos from a database to a control that will display it using HTML5. Using Response.OutputStream.Write does indeed work, however, the video player in both Firefox 4.0 and Chrome cannot seem to determine开发者_运维百科 how long the video is. Firefox's counter just increases as the play time goes on, while Chrome shows a completely random time.

Is there some header that I need to set in order for this to work? I have set the content-length to the number of bytes, which I think is the correct value.

HTML Markup

<video controls="controls" poster="<%= ImageURL %>" width="<%= Width %>" height="<%= Height %>" preload="preload">
    <source src="<%= MP4URL %>" type='video/mp4; codecs="avc1.42E01E,mp4a.40.2"'/>
    <source src="<%= OggURL %>" type='video/ogg; codecs="theora,vorbis"'/>
    <img alt="<%= ImageAlt %>" src="<%= ImageURL %>" width="<%= Width %>" height="<%= Height %>" title="No video playback capabilities, please download the video below" />
</video>
</div>

Video Handler Code

        // Snip -- Company specific DB code

        // Set the correct headers
        context.Response.AppendHeader("content-length", video.Length.ToString());

        // Set the content type
        switch (extensionType.ToLower())
        {
            case "ogv":
                contentType = "video/ogg";
                break;

            case "mp4":
                contentType = "video/mp4";
                break;

            default:
                contentType = "";
                break;
        }
        context.Response.ContentType = contentType;
        context.Response.Charset = String.Empty;
        context.Response.Buffer = false;


        // Write the video to the stream
        context.Response.OutputStream.Write(video, 0, video.Length);

        // Close the repsonse
        context.Response.Close();
        context.Response.End();

Any help on this would be greatly appreciated.


So, after a couple of years away from this, we decided to have another look at a video handler. One plucky young apprentice, who has since become a full time developer with us, solved the issue.

As it turns out, you need to add a little more information to the header of your response. We were missing the Content-Range header, which looks like this: bytes_from-bytes_to/bytes_total. The Content-Length header needs to have the size of the chunk that is being sent back instead of the total size of the file. Finally, you need to respond with a code of 206 (partial) instead of 200 (OK).

Once we'd done that, everything started working nicely. We did a bit more to split the files into nice chunks efficiently instead of loading the whole file from the database again and again.

0

精彩评论

暂无评论...
验证码 换一张
取 消