ASP.NET 和 HTML5 视频播放时间
下午好,stackoverflow!
目前,我正在开发一个处理程序,将数据库中的视频提供给使用 HTML5 显示视频的控件。使用 Response.OutputStream.Write 确实有效,但是,Firefox 4.0 和 Chrome 中的视频播放器似乎无法确定视频的长度。 Firefox 的计数器只会随着游戏时间的延长而增加,而 Chrome 则显示完全随机的时间。
我需要设置一些标头才能使其正常工作吗?我已将内容长度设置为字节数,我认为这是正确的值。
HTML 标记
<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>
视频处理程序代码
// 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();
对此的任何帮助将不胜感激。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
因此,几年后,我们决定重新审视视频处理程序。一位勇敢的年轻学徒解决了这个问题,后来他成为了我们的全职开发人员。
事实证明,您需要在响应的标题中添加更多信息。我们缺少 Content-Range 标头,它看起来像这样:bytes_from-bytes_to/bytes_total。 Content-Length 标头需要具有发送回的块的大小,而不是文件的总大小。最后,您需要使用代码 206(部分)而不是 200(正常)进行响应。
一旦我们做到了这一点,一切就开始顺利进行。我们做了更多工作来有效地将文件分割成漂亮的块,而不是一次又一次地从数据库加载整个文件。
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.