.net MVC 将 MP4 流式传输到 iDevice 问题

2024-05-09

我一直在编写用于提供视频服务的一段代码,但遇到了一些问题。代码如下:

public ResumingFileStreamResult GetMP4Video(string videoID)
    {
        if (User.Identity.IsAuthenticated)
        {
            string clipLocation = string.Format("{0}\\Completed\\{1}.mp4", ConfigurationManager.AppSettings["VideoLocation"].ToString(), videoID);

            FileStream fs = new FileStream(clipLocation, FileMode.Open, FileAccess.Read);

            ResumingFileStreamResult fsr = new ResumingFileStreamResult(fs, "video/mp4");

            return fsr;
        }
        else
        {
            return null;
        }
    }

这是我的 HTML 代码:

<video controls preload poster="@Url.Content(string.Format("~/Videos/{0}_2.jpg", Model.VideoID))">
    <source src="@Url.Action("GetMP4Video", "Video", new { videoID = Model.VideoID })" type="video/mp4" />
    <source src="@Url.Action("GetWebMVideo", "Video", new { videoID = Model.VideoID })" type="video/webm" />

        <object id="flowplayer" data="@Url.Content("~/Scripts/FlowPlayer/flowplayer-3.2.14.swf")" type="application/x-shockwave-flash" width="640" height="360">
            <param name="movie" value="@Url.Content("~/Scripts/FlowPlayer/flowplayer-3.2.14.swf")" />
            <param name="allowfullscreen" value="true" />
            <param name="flashvars" value="config={'playlist':['@Url.Content(string.Format("~/Videos/{0}_2.jpg", Model.VideoID))',{'url':'@Url.Action("GetMP4Video", "Video", new { videoID = Model.VideoID })','autoPlay':false}]}" />
        </object>
</video>

我的问题是,这个设置似乎在我的桌面上的所有浏览器中都能正常工作,但当我尝试使用 iPad 或 iPhone 加载页面时,它只显示播放图标,中间有一条线表明它无法播放。我尝试将 mp4 视频的源更改为 mp4 视频的直接链接,iPad 立即开始播放它。

为了使我的方法与 iDevices 兼容,我是否需要做一些我错过的特殊事情?对此的任何帮助将不胜感激。


要使您的视频可以在 iOS 设备上播放,您需要实现对字节范围 (or partial) 要求。这些类型的请求允许不下载全部内容,而是部分地、逐块地下载(典型的流媒体)。这是iOS设备在页面上获取和播放视频的唯一方式。

部分请求使用Rangeheader 告诉服务器下一个 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有关详细信息并使用其作者编写的处理程序:它对我有用,但它没有缓存功能。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

.net MVC 将 MP4 流式传输到 iDevice 问题 的相关文章

随机推荐