MVC 视频流式传输到移动网络浏览器
MVC Video streaming to mobile web browsers
我想通过 ASP.NET MVC Web 应用程序将视频从我的 Azure blob 流式传输给桌面浏览器用户,当然还有移动浏览器用户。
到目前为止,我已经构建了一个 ASP.NET 为网站提供服务的 MVC 应用程序和一个将 PushStreamContent 的 WebApi 服务。这在桌面上的 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">
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;
}
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();
}
}
}
}
因此,经过一段时间尝试不同的东西后,我转储到 Microsoft 的这篇文章中,完美地解决了我的问题。 Link to article
这是对我有用的代码:
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;
}
我想通过 ASP.NET MVC Web 应用程序将视频从我的 Azure blob 流式传输给桌面浏览器用户,当然还有移动浏览器用户。
到目前为止,我已经构建了一个 ASP.NET 为网站提供服务的 MVC 应用程序和一个将 PushStreamContent 的 WebApi 服务。这在桌面上的 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">
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;
}
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();
}
}
}
}
因此,经过一段时间尝试不同的东西后,我转储到 Microsoft 的这篇文章中,完美地解决了我的问题。 Link to article
这是对我有用的代码:
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;
}