如何流在asp.net中的视频内容? [英] How to stream video content in asp.net?
本文介绍了如何流在asp.net中的视频内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下的code,这将下载的视频内容:
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?
推荐答案
您正在阅读的整个文件到一个缓冲区,然后立即发送整个字节数组。
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.
例如:
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);
}
这篇关于如何流在asp.net中的视频内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文