title: Stream MP4 Video From Seek Position in ASP.NET
body: <p>I am facing problem while streaming mp4 video from seek position. Its streaming properly from start.</p>
  
  <p>First problem is while mp4 video is streaming via jw player flash player. When user click on time bar to start streaming mp4 video from any other part of video, jw player will send start param along with time information e.g</p>
  
  <pre><code>http://[url]/stream/mp4.ashx?file=Madagascar3-trailer-48861c.mp4&amp;start=53.71
  </code></pre>
  
  <p>So jwplayer send time interval to seek mp4 streaming from.</p>
  
  <p>I am using the following code to convert approx time interval to bytes as seek will start via bytes.</p>
  
  <pre><code> double total_duration = Convert.ToDouble(context.Request.Params["d"]);
   double startduration = Convert.ToDouble(context.Request.Params["start"]);
   double length_sec = (double)size / total_duration; // total length per second
   start = (long)(length_sec * startduration);
  </code></pre>
  
  <p>Here is complete sample code i am using to start streaming from seek position e.g 53.71</p>
  
  <pre><code>private void ChunkDownload(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;
  
          double total_duration = Convert.ToDouble(context.Request.Params["d"]);
          double startduration = Convert.ToDouble(context.Request.Params["start"]);
          double length_sec = (double)size / total_duration; // total length per second
          start = (long)(length_sec * startduration);
  
          context.Response.AddHeader("Accept-Ranges", "0-" + size);
          long anotherStart = start;
          long anotherEnd = end;
  
          // End bytes can not be larger than $end.
          anotherEnd = (anotherEnd &gt; end) ? end : anotherEnd;
          // Validate the requested range and return an error if it's not correct.
          if (anotherStart &gt; anotherEnd || anotherStart &gt; size - 1 || anotherEnd &gt;= 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; // Calculate new content length
          fp = reader.BaseStream.Seek(start, SeekOrigin.Begin);
          context.Response.StatusCode = 206;
  
      }
      // Notify the client the byte range we'll be outputting
      context.Response.AddHeader("Content-Range", "bytes " + start + "-" + end + "/" + size);
      context.Response.AddHeader("Content-Length", length.ToString());
  
      context.Response.WriteFile(fullpath, fp, length);
      context.Response.End();
  
  }
  </code></pre>
  
  <p>But it failed to recognize as valid mp4 stream either by player or checking stream url directly.</p>
  
  <p>Can anyone help me to fix this issue.</p>