ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

c# – .NET Core MVC中的部分内容(用于视频/音频流)

2019-06-27 12:54:09  阅读:260  来源: 互联网

标签:c asp-net-mvc streaming asp-net-core asp-net-core-webapi


我正在尝试在我的网站上实现视频和音频流(以便在Chrome中搜索),我最近发现.NET Core 2.0显然提供了一种使用FileStreamResult实现此功能的相对简单和推荐的方法.

这是我返回FileStreamResult的Action的简化实现:

    public IActionResult GetFileDirect(string f)
    {
        var path = Path.Combine(Defaults.StorageLocation, f);
        return File(System.IO.File.OpenRead(path), "video/mp4");
    } 

File方法具有以下(缩写)描述:

Returns a file in the specified fileStream (Status200OK), with the specified contentType as the Content-Type. This supports range requests (Status206PartialContent or Status416RangeNotSatisfiable if the range is not satisfiable)

但由于某种原因,服务器仍然无法正确响应范围请求.

我错过了什么吗?

更新

Chrome发送的请求如下所示

GET https://myserver.com/viewer/GetFileDirect?f=myvideo.mp4 HTTP/1.1
Host: myserver.com
Connection: keep-alive
Accept-Encoding: identity;q=1, *;q=0
User-Agent: ...
Accept: */*
Accept-Language: ...
Cookie: ...
Range: bytes=0-

响应如下:

HTTP/1.1 200 OK
Server: nginx/1.10.3 (Ubuntu)
Date: Fri, 09 Feb 2018 17:57:45 GMT
Content-Type: video/mp4
Content-Length: 5418689
Connection: keep-alive

[... content ... ]

还尝试使用以下命令:
curl -H范围:bytes = 16- -I https://myserver.com/viewer/GetFileDirect?f=myvideo.mp4并返回相同的响应.

HTML也很简单.

<video controls autoplay>
    <source src="https://myserver.com/viewer/GetFileDirect?f=myvideo.mp4" type="video/mp4">
    Your browser does not support the video tag.
</video>

视频开始播放 – 用户无法搜索视频.

解决方法:

将在版本2.1中的File方法中添加enableRangeProcessing参数.目前,您需要设置一个开关.你可以这两种方式之一:

在runtimeconfig.json中:

{
  // Set the switch here to affect .NET Core apps
  "configProperties": {
    "Switch.Microsoft.AspNetCore.Mvc.EnableRangeProcessing": "true"
  }
}

要么:

 //Enable 206 Partial Content responses to enable Video Seeking from 
 //api/videos/{id}/file,
 //as per, https://github.com/aspnet/Mvc/pull/6895#issuecomment-356477675.
 //Should be able to remove this switch and use the enableRangeProcessing 
 //overload of File once 
 // ASP.NET Core 2.1 released

   AppContext.SetSwitch("Switch.Microsoft.AspNetCore.Mvc.EnableRangeProcessing", 
   true);

有关详情,请参见ASP.NET Core GitHub Repo.

标签:c,asp-net-mvc,streaming,asp-net-core,asp-net-core-webapi
来源: https://codeday.me/bug/20190627/1304988.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有