MVC视频流到移动Web浏览器

Ivo*_*gel 5 c# asp.net model-view-controller video-streaming asp.net-web-api

嗨,大家好,我想通过ASP.NET MVC Web应用程序将视频从我的Azure blob流式传输到桌面浏览器上的用户,当然还有移动浏览器.

到目前为止我构建的是一个ASP.NET MVC应用程序,它为网站提供服务,而WebApi服务则是PushStreamContent.这在桌面上的Chrome和Edge上非常棒.但是,我试图在移动设备Chrome,Safari,Firefox上打开它,它就不会玩了.

我的代码到目前为止:

关于MVC项目的观点

<video id="vPlayer" class="video-js" autoplay controls playsinline preload="auto" data-setup='{"fluid": true}'poster="@ImgManager.GetVideoImgUrl(Model.ThumbnailUrl)">
<source src="//xyz.nl/api/Videos/Get?filename=340a85a3-ccea-4a2a-bab6-74def07e416c.webm&type=video%2Fwebm" type="video/webm">
<source src="//xyz.nl/api/Videos/Get?filename=340a85a3-ccea-4a2a-bab6-74def07e416c.mp4&type=video%2Fmp4" type="video/mp4">
Run Code Online (Sandbox Code Playgroud)

WebApi

public HttpResponseMessage Get(string filename, string type)
        {
            var video = new VideoStream(filename);
            var response = Request.CreateResponse();

            response.Content = new PushStreamContent(video.WriteToStream, new MediaTypeHeaderValue(type));

            return response;
        }
Run Code Online (Sandbox Code Playgroud)

webAPI上的助手

public class VideoStream
    {
        private readonly string _filename;

        public VideoStream(string fileName)
        {
            _filename = fileName;
        }



        public async Task WriteToStream(Stream outputStream, HttpContent content, TransportContext context)
        {
            try
            {
                var storage = new AzureMainStorage("avideo");
                byte[] file = await storage.GetFileAsync(_filename);

                var buffer = new byte[65536];

                using (var video = new MemoryStream(file))
                {
                    var length = (int)video.Length;
                    var bytesRead = 1;

                    while (length > 0 && bytesRead > 0)
                    {
                        bytesRead = video.Read(buffer, 0, Math.Min(length, buffer.Length));
                        await outputStream.WriteAsync(buffer, 0, bytesRead);

                        length -= bytesRead;
                    }
                }
            }
            catch (HttpException ex)
            {
                Utilities.LogErrorToDb(ex);
                return;
            }
            finally
            {
                outputStream.Close();
            }

        }

    }
}
Run Code Online (Sandbox Code Playgroud)

Ivo*_*gel 2

因此,经过一段时间的尝试后,我将微软的这篇文章转储到这篇文章中,完美地解决了我的问题。文章链接

这是对我有用的代码:

public HttpResponseMessage Get(string filename, string type)
{
     MemoryStream stream = new MemoryStream(new AzureMainStorage("avideo").GetFile(filename));

     HttpResponseMessage partialResponse = Request.CreateResponse(HttpStatusCode.PartialContent);
     partialResponse.Content = new ByteRangeStreamContent(stream, Request.Headers.Range, "video/mp4");
     return partialResponse;  
}
Run Code Online (Sandbox Code Playgroud)