加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

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

发布时间:2020-12-16 06:31:36 所属栏目:asp.Net 来源:网络整理
导读:我一直在努力为视频提供一段代码,我遇到了一些问题.代码如下: public ResumingFileStreamResult GetMP4Video(string videoID) { if (User.Identity.IsAuthenticated) { string clipLocation = string.Format("{0}Completed{1}.mp4",ConfigurationManage
我一直在努力为视频提供一段代码,我遇到了一些问题.代码如下:

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",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",new { videoID = Model.VideoID })','autoPlay':false}]}" />
        </object>
</video>

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

是否有一些我需要做的特别的事情,我错过了命令让我的方法与iDevices兼容?任何有关这方面的帮助将不胜感激.

解决方法

要使您的视频在iOS设备上播放,您需要实现对字节范围(或部分)请求的支持.这些类型的请求不允许下载整个内容,而是部分地下载块(典型的流).这是iOS设备在页面上获取和播放视频的唯一方式.

部分请求使用Range标头告诉服务器下一个块位置和大小.另一方面的服务器响应206部分内容和请求的块内容.

您可以找到ASP.NET处理程序的几个实现,它们可以处理Internet中的部分请求.我建议使用StaticFileHandler:易于安装,并且还具有开箱即用的缓存功能.它也可以通过Nuget提供,但该包名为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’s article about partial requests以获取详细信息并使用其作者编写的处理程序:它对我有用,但它没有缓存功能.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读