Stream video content through Web API 2

前端 未结 2 1644
悲哀的现实
悲哀的现实 2021-01-30 18:39

I\'m in the process of working out what the best way is going to be to do the following:

I have a bunch of CCTV footage files (MP4 files, ranging from 4MB-50MB in size),

2条回答
  •  时光说笑
    2021-01-30 19:06

    Two things:

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

    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:

    https://devblogs.microsoft.com/aspnet/asp-net-web-api-and-http-byte-range-support/

    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;
    }
    

提交回复
热议问题