Hello dear.
i need to play a stream a MP4 type with Asp.net or ASHX that user can tracking it video.
i found this code :
private void RangeDownload(string fullpath,HttpContext context)
{
long size,start,end,length,fp=0;
using (StreamReader reader = new StreamReader(fullpath))
{
size = reader.BaseStream.Length;
start = 0;
end = size - 1;
length = size;
context.Response.AddHeader("Accept-Ranges", "0-" + size);
if (!String.IsNullOrEmpty(context.Request.ServerVariables["HTTP_RANGE"]))
{
long anotherStart = start;
long anotherEnd = end;
string[] arr_split = context.Request.ServerVariables["HTTP_RANGE"].Split(new char[] { Convert.ToChar("=") });
string range = arr_split[1];
if (range.IndexOf(",") > -1)
{
context.Response.AddHeader("Content-Range", "bytes " + start + "-" + end + "/" + size);
throw new HttpException(416, "Requested Range Not Satisfiable");
}
if (range.StartsWith("-"))
{
anotherStart = size - Convert.ToInt64(range.Substring(1));
}
else
{
arr_split = range.Split(new char[] { Convert.ToChar("-") });
anotherStart = Convert.ToInt64(arr_split[0]);
long temp = 0;
anotherEnd = (arr_split.Length > 1 && Int64.TryParse(arr_split[1].ToString(), out temp)) ? Convert.ToInt64(arr_split[1]) : size;
}
anotherEnd = (anotherEnd > end) ? end : anotherEnd;
if (anotherStart > anotherEnd || anotherStart > size - 1 || anotherEnd >= size)
{
context.Response.AddHeader("Content-Range", "bytes " + start + "-" + end + "/" + size);
throw new HttpException(416, "Requested Range Not Satisfiable");
}
start = anotherStart;
end = anotherEnd;
length = end - start + 1;
fp = reader.BaseStream.Seek(start, SeekOrigin.Begin);
context.Response.StatusCode = 206;
}
}
context.Response.AddHeader("Content-Range", "bytes " + start + "-" + end + "/" + size);
context.Response.AddHeader("Content-Length", length.ToString());
using (FileStream Strm = new FileStream(fullpath, FileMode.Open, FileAccess.Read))
{
context.Response.Clear();
context.Response.ClearContent();
Strm.Seek(fp,SeekOrigin.Begin);
byte[] bt = new byte[Convert.ToInt32(length)];
Strm.Read(bt, 0, bt.Length);
context.Response.BinaryWrite(bt);
bt = new byte[0];
}
context.Response.Flush();
context.Response.Close();
context.Response.End();
}
public void ProcessRequest(HttpContext context)
{
string filename = context.Request["fn"];
string mimetype = "video/mp4";
string fullpath = @"E:\" + filename;
if (System.IO.File.Exists(fullpath))
{
context.Response.ContentType = mimetype;
if (!String.IsNullOrEmpty(context.Request.ServerVariables["HTTP_RANGE"]))
{
RangeDownload(fullpath,context);
}
else
{
System.IO.FileInfo fileInfo = new System.IO.FileInfo(fullpath);
long fileLength = fileInfo.Length;
context.Response.AddHeader("Content-Length", fileLength.ToString());
using (FileStream Strm = new FileStream(fullpath, FileMode.Open, FileAccess.Read))
{
context.Response.Clear();
context.Response.ClearContent();
context.Response.ClearHeaders();
context.Response.ContentType = "video/mp4";
byte[] bt = new byte[Strm.Length];
Strm.Read(bt, 0, bt.Length);
context.Response.BinaryWrite(bt);
bt = new byte[0];
}
context.Response.Flush();
context.Response.Close();
context.Response.End();
}
}
else
{
throw new HttpException(404, "Video Not Found Path:"+fullpath);
}
}
public bool IsReusable {
get {
return false;
}
}
But when user tracking video , after several moves , ram will full.
Where is my problem?
Thank you.