Stream video content through Web API 2

好久不见. 提交于 2019-11-29 21:05:45

Two things:

  1. Use a video element in your HTML (this works in browsers AND iOS):

    <video src="http://yoursite.com/api/Media/GetVideo?videoId=42" /> 
    
  2. Support 206 PARTIAL CONTENT requests in you Web API code. This is crucial for both streaming and iOS support, and is mentioned in that thread you posted.

Just follow this example:

http://blogs.msdn.com/b/webdev/archive/2012/11/23/asp-net-web-api-and-http-byte-range-support.aspx

In a nutshell:

if (Request.Headers.Range != null)
{
    // Return part of the video
    HttpResponseMessage partialResponse = Request.CreateResponse(HttpStatusCode.PartialContent);
    partialResponse.Content = new ByteRangeStreamContent(stream, Request.Headers.Range, mediaType);
    return partialResponse;
}
else 
{
    // Return complete video
    HttpResponseMessage fullResponse = Request.CreateResponse(HttpStatusCode.OK);
    fullResponse.Content = new StreamContent(stream);
    fullResponse.Content.Headers.ContentType = mediaType;
    return fullResponse;
}

Place the static video files on a web server and reference them in a video player. You can use HTML's standard player (<video src="http://location/of/file.mp4">) or go for something fancier - just google for "html video player".

To make sure that the video files don't have to download completely before starting to play just run them beforehand through qt-faststart.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!