如何在 ASP.NET 中流式传输视频内容?

发布于 2024-08-30 04:06:51 字数 699 浏览 4 评论 0原文

我有以下下载视频内容的代码:

WebRequest wreq = (HttpWebRequest)WebRequest.Create(url);
using (HttpWebResponse wresp = (HttpWebResponse)wreq.GetResponse())
using (Stream mystream = wresp.GetResponseStream())
{
  using (BinaryReader reader = new BinaryReader(mystream))
  {
    int length = Convert.ToInt32(wresp.ContentLength);
    byte[] buffer = new byte[length];
    buffer = reader.ReadBytes(length);

    Response.Clear();
    Response.Buffer = false;
    Response.ContentType = "video/mp4";
    //Response.BinaryWrite(buffer);
    Response.OutputStream.Write(buffer, 0, buffer.Length);
    Response.End();
  }
}

但问题是整个文件在播放之前下载。如何在仍在下载时使其流式传输和播放?或者这取决于客户端/接收方应用程序来管理?

I have the following code which downloads video content:

WebRequest wreq = (HttpWebRequest)WebRequest.Create(url);
using (HttpWebResponse wresp = (HttpWebResponse)wreq.GetResponse())
using (Stream mystream = wresp.GetResponseStream())
{
  using (BinaryReader reader = new BinaryReader(mystream))
  {
    int length = Convert.ToInt32(wresp.ContentLength);
    byte[] buffer = new byte[length];
    buffer = reader.ReadBytes(length);

    Response.Clear();
    Response.Buffer = false;
    Response.ContentType = "video/mp4";
    //Response.BinaryWrite(buffer);
    Response.OutputStream.Write(buffer, 0, buffer.Length);
    Response.End();
  }
}

But the problem is that the whole file downloads before being played. How can I make it stream and play as it's still downloading? Or is this up to the client/receiver application to manage?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

吾家有女初长成 2024-09-06 04:06:51

您将整个文件读入单个缓冲区,然后立即发送整个字节数组。

您应该在 while 循环中读入较小的缓冲区。

例如:

byte[] buffer = new byte[4096];

while(true) {
    int bytesRead = myStream.Read(buffer, 0, buffer.Length);
    if (bytesRead == 0) break;
    Response.OutputStream.Write(buffer, 0, bytesRead);
}

You're reading the entire file into a single buffer, then sending the entire byte array at once.

You should read into a smaller buffer in a while loop.

For example:

byte[] buffer = new byte[4096];

while(true) {
    int bytesRead = myStream.Read(buffer, 0, buffer.Length);
    if (bytesRead == 0) break;
    Response.OutputStream.Write(buffer, 0, bytesRead);
}
不弃不离 2024-09-06 04:06:51

这对您来说更有效,特别是如果您需要从服务器上的文件流式传输视频,或者甚至该文件托管在另一台服务器

上 文件在您的服务器上:

context.Response.BinaryWrite(File.ReadAllBytes(HTTPContext.Current.Server.MapPath(_video.Location)));

文件在外部服务器上:

var wc = new WebClient();
    context.Response.BinaryWrite(wc.DownloadData(new Uri("http://mysite/video.mp4")));

This is more efficient for you especially if you need to stream a video from a file on your server or even this file is hosted at another server

File On your server:

context.Response.BinaryWrite(File.ReadAllBytes(HTTPContext.Current.Server.MapPath(_video.Location)));

File on external server:

var wc = new WebClient();
    context.Response.BinaryWrite(wc.DownloadData(new Uri("http://mysite/video.mp4")));
朦胧时间 2024-09-06 04:06:51

您是否看过平滑流媒体

查看示例代码此处< /a>

Have you looked at Smooth Streaming?

Look at sample code here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文