We are trying to play large videos (30MB) using asp.net. The code we are using is:
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();
The issue is, it will take long time to play the video. The response is not quick. Please help us how to resolve this.