c# – .NET Core MVC中的部分内容(用于视频/音频流)
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了c# – .NET Core MVC中的部分内容(用于视频/音频流),小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2126字,纯文字阅读大概需要4分钟。
内容图文
![c# – .NET Core MVC中的部分内容(用于视频/音频流)](/upload/InfoBanner/zyjiaocheng/789/2efab1056fa543c2bd1a6e5134ab68e4.jpg)
我正在尝试在我的网站上实现视频和音频流(以便在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# – .NET Core MVC中的部分内容(用于视频/音频流)全部内容,希望文章能够帮你解决c# – .NET Core MVC中的部分内容(用于视频/音频流)所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。