Hi i Make this ASHX class :
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());
long fileLength = length;
using (FileStream fileStream = new FileStream(fullpath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
try
{
fileStream.Seek(fp, SeekOrigin.Begin);
Byte[] buffer = new Byte[10240];
while (fileLength > 0)
{
if (context.Response.IsClientConnected)
{
int lengthBuff = fileStream.Read(buffer, 0, 10240);
context.Response.OutputStream.Write(buffer, 0, lengthBuff);
context.Response.Flush();
fileLength = fileLength - lengthBuff;
}
else
{
fileLength = -1;
}
}
}
catch (HttpException ex)
{
context.Response.StatusCode = ex.GetHttpCode();
}
finally
{
fileStream.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
{
using (FileStream fileStream = new FileStream(fullpath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)){
try
{
long fileLength = fileStream.Length;
context.Response.AddHeader("Content-Length", fileLength.ToString());
Byte[] buffer = new Byte[10240];
while (fileLength > 0)
{
if (context.Response.IsClientConnected)
{
int length = fileStream.Read(buffer, 0, 10240);
context.Response.OutputStream.Write(buffer, 0, length);
context.Response.Flush();
fileLength = fileLength - length;
}
else
{
fileLength = -1;
}
}
}
catch (HttpException ex)
{
context.Response.StatusCode = ex.GetHttpCode();
}
finally
{
fileStream.Close();
}
}
}
}
else
{
throw new HttpException(404, "Video Not Found Path:"+fullpath);
}
}
public bool IsReusable {
get {
return false;
}
}
and then i call it in mediaelement in WPF like this :
mediaElement.Source = new Uri("http://localhost:3402/VideoStreamer.ashx?fn=a.mp4");
mediaElement.Play();
But my problem is it not seekable and i can't change position my video.
Where is My Problem ?
Thank You.