我们正在尝试使用asp.net播放大型视频(30MB)。我们使用的代码是:
System.IO.Stream iStream = null;
byte[] buffer = new Byte[10000];
int length;
long dataToRead;
context.Response.Buffer = false;
context.Response.BufferOutput = false;
context.Response.Clear();
iStream = new System.IO.FileStream(myPath, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read);
dataToRead = iStream.Length;
context.Response.ContentType = "video/mp4";
context.Response.AddHeader("Content-Length", iStream.Length.ToString(System.Globalization.CultureInfo.InvariantCulture));
//Write the file.
while (dataToRead > 0)
{
// Verify that the client is connected.
if (context.Response.IsClientConnected)
{
// Read the data in buffer.
length = iStream.Read(buffer, 0, 10000);
// Write the data to the current output stream.
context.Response.OutputStream.Write(buffer, 0, length);
// Flush the data to the HTML output.
context.Response.Flush();
buffer = new Byte[10000];
dataToRead = dataToRead - length;
}
else
{
//prevent infinite loop if user disconnects
dataToRead = -1;
}`enter code here`
}
context.Response.OutputStream.Flush();
问题是,播放视频需要很长时间。反应不快。请帮助我们解决此问题。
答案 0 :(得分:0)
您是否使用ffmpeg,-movflags +faststart
在MP4文件的开头移动'moov atom'元数据?
也许它会更好。
答案 1 :(得分:0)
这很慢的一个原因是你没有重叠读取和发送。两者之间没有并发性,因此每次读取都必须等待发送完成,并且每个发送都必须等待读取。
您正在进行自己的流复制而不是(a)仅使用Stream.CopyTo
或(b)仅使用WebAPI和FileStreamResult
结果返回StreamContent
或(c) 。
请参阅What's the difference between the four File Results in ASP.NET MVC