要使您的视频可以在 iOS 设备上播放,您需要实现对字节范围 (or partial) 要求。这些类型的请求允许不下载全部内容,而是部分地、逐块地下载(典型的流媒体)。这是iOS设备在页面上获取和播放视频的唯一方式。
部分请求使用Range
header 告诉服务器下一个 chunk 的位置和大小。另一端的服务器响应206 Partial Content
并请求块内容。
您可以找到 ASP.NET 处理程序的多种实现,它们可以处理 Internet 中的部分请求。我建议使用静态文件处理程序 http://code.google.com/p/talifun-web/wiki/StaticFileHandler为此:易于安装并且还具有开箱即用的缓存功能。也可以通过 Nuget 交付,但包名为塔里趣网 http://nuget.org/packages/Talifun.Web.
要配置 StaticFileHandler,请在 web.config 中注册该处理程序mp4文件并在单独的配置部分中配置它:
<configuration>
<configSections>
<section name="StaticFileHandler" type="Talifun.Web.StaticFile.Config.StaticFileHandlerSection, Talifun.Web" requirePermission="false" allowDefinition="MachineToApplication"/>
</configSections>
<StaticFileHandler webServerType="NotSet">
<!-- The defaults to use when an extension is found that does not have a specific rule -->
<fileExtensionDefault name="Default" serveFromMemory="true" maxMemorySize="100000" compress="true"/>
<!-- Specific rules for extension types -->
<fileExtensions>
<fileExtension name="VideoStaticContent" extension="3gp, 3g2, asf, avi, dv, flv, mov, mp4, mpg, mpeg, wmv" serveFromMemory="true" maxMemorySize="100000" compress="false"/>
</fileExtensions>
</StaticFileHandler>
<system.webServer>
<handlers>
<add name="StaticContentHandler" verb="GET,HEAD" path="*.mp4" type="Talifun.Web.StaticFile.StaticFileHandler, Talifun.Web"/>
</handlers>
</system.webServer>
</configuration>
通过创建 ASP.NET 处理程序并调用,还可以轻松应用您的自定义逻辑,例如授权或自定义视频文件源StaticFileManager
直接地。
public class MyOwnVideoHandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
// Authorization or any other stuff.
...
// Get file from your storage.
FileInfo file = ...;
// Serve the file with StaticFileHandler.
StaticFileManager.Instance.ProcessRequest(new HttpContextWrapper(context), file);
}
}
另外,您还可以看看Scott Mitchell 关于部分请求的文章 http://dotnetslackers.com/articles/aspnet/Range-Specific-Requests-in-ASP-NET.aspx有关详细信息并使用其作者编写的处理程序:它对我有用,但它没有缓存功能。