Progressive streaming of video in asp.net
I want to do progressive streaming of video in asp.net. I have written a code for that but still its not loading properly. Does any have idea about it.
Please have look into my code or suggest me the best way to do it. I don't want player to wait for downloading whole video and then start playing. I want to start playing right now.
Following is my code.
string path = "Test.mp4";
string rootpath = Server.MapPath(Request.ApplicationPath);
string file = string.Format(@"{0}\\{1}", rootpath, path);
if (File.Exists(file) && file.Contains("mp4"))
{
FileStream filestream = new FileStream(file, FileMode.Open, FileAccess.Read);
long length = filestream.Length;
Response.AppendHeader("Content-Type", "video/mp4");
Response.AddHeader("Content-Disposition", "attachment; filename=" + path + "");
Response.AppendHeader("Content-Length", length.ToString());
const int buffersize = 16384;
byte[] buffer = new byte[buffersize];
int count = fi开发者_运维问答lestream.Read(buffer, 0, buffersize);
while (count > 0)
{
if (!Response.IsClientConnected)
count = -1;
else
{
Response.OutputStream.Write(buffer, 0, count);
Response.Flush();
count = filestream.Read(buffer, 0, buffersize);
}
}
}
Best Regards, Jalpesh
I had the same problem and ended up implementing 'Range-Specific Request'. Check this article on dotNetSlackers. It has sample code for streaming of MP4 files. This solution works great on iPad, iPhone and Chrome!
The Content-Disposition
header is used to instruct the browser to offer a Save As
dialog for the attached file. probably not the behavior you want. Did you try removing it?
精彩评论