如何使用ASP.NET MVC流式传输视频文件? [英] How do I stream a video file using ASP.NET MVC?

查看:355
本文介绍了如何使用ASP.NET MVC流式传输视频文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个网站,人们可以在其中访问音频和视频文件.我有用于下载文件的代码,其中包含以下两种操作方法...

I am developing a web site where people can access audio and video files. I have the code for downloading the files, which consists of two action methods as follows...

public ActionResult GetAudioFile(int id) {
  return GetFile(id, true);
}

public ActionResult GetVideoFile(int id) {
  return GetFile(id, false);
}

private ActionResult GetFile(int id, bool isAudio) {
  // Code to get info about the file, etc omitted for clarity
  string downloadFileName = // full path to audio/video file
  byte[] bytes = GetFileBytes(fileName); // also omitted
  return File(bytes, (isAudio ? "audio/mpeg" : "video/mp4"), downloadFileName + (isAudio ? ".mp3" : ".mp4"));
}

这两个文件都可以正常工作,我可以下载任何一种文件.

These both work fine, and I can download either type of file.

我现在想添加两个Razor视图,一个用于收听音频文件,另一个用于查看视频.我在音频视图上执行了以下操作,并且效果很好...

I now want to add two Razor views, one for listening to the audio file, and one for viewing the video. I did the following on the audio view, and it works fine...

<audio src='@Url.Action("GetAudioFile", "Search", new {ID = @Model.ID})'
               controls preload="auto"></audio>

然后我尝试对视频视图进行同样的操作...

I then tried to do the same for the video view...

<video src='@Url.Action("GetVideoFile", "Search", new {ID = @Model.ID})' 
               controls preload="auto" width="640" height="368"></video>

但是,当我尝试运行它时,将给出System.OutOfMemoryException.每个视频平均约400-500Mb.

However, this gives a System.OutOfMemoryException when I try to run it. The videos average around 400-500Mb each.

然后我尝试如下使用Response.TransmitFile方法...

I then tried using the Response.TransmitFile method as follows...

  Response.ContentType = "video/mp4";
  Response.AddHeader("content-disposition", "attachment; filename=myvideo.mp4"));
  Response.TransmitFile(fileName);
  Response.End();
  return null;

...但这不起作用.在Chrome中,我可以在控制台中看到消息资源被解释为文档,但以MIME类型video/mp4传输了",而在FireFox中,我在视频控件中也得到了同样的消息.

...but that doesn't work. In Chrome, I can see the message "Resource interpreted as Document but transferred with MIME type video/mp4" in the console, and in FireFox, I get the same message in the video control.

有人知道我该如何解决吗?理想情况下,我想流式传输文件,以便它在第一个字节到达用户后立即开始播放,而不是让它们等到文件完全下载后.

Anyone any idea how I can fix this? Ideally, I would like to stream the file, so that it starts playing as soon as the first bytes reach the user, rather than having them wait until the file is completely downloaded.

我已经尝试了几个Javascript视频播放器,但无论哪种都取得了成功.

I've tried a couple of Javascript video players, but had any success with those either.

更新,我想知道根本不是控制器操作引起的,因为我尝试将Web浏览器直接指向视频文件,然后得到了看起来像音频播放器的图像,但没有视频窗格,当我单击播放"按钮时没有任何反应.不确定是否有帮助.

Update I'm wondering if it's not the controller action that's the problem at all, as I tried pointing my web browser directly at the video file, and I got what looked like an audio player, but without a video pane, and nothing happens when I click the play button. Not sure if that helps.

推荐答案

MvcControllerFile方法返回FileContentResult.后者不流式传输内容.据我所知,MvcController根本不支持流传输.

The File method of MvcController returns a FileContentResult. The latter does not stream the content. As far as I'm aware, MvcController does not support streaming at all.

您可能想尝试ApiControllerPushStreamContent.后者提供了通过回调方法异步写入输出流的可能性.为此,您需要使用解决方案中的实际流.使用byte[]始终会将整个文件内容加载到内存中.

You may want to try ApiController and PushStreamContent. The latter gives the possibility to asynchronously write to the output stream via a callback method. You'll need to work with actual streams in your solution for that. Working with byte[] will always load the whole file content into memory.

在此处查看详细的教程:使用ASP.NET Web API异步流传输视频

See a detailed tutorial here:Asynchronously streaming video with ASP.NET Web API

这篇关于如何使用ASP.NET MVC流式传输视频文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆